Dela via


How to: Generate a Coded UI Test from an Action Recording

Using an existing action recording is an efficient way to create coded UI tests. For more information about how to create action recordings, see Recording and Playing Back Manual Tests.

Generate a Coded UI Test from an Action Recording

To generate a coded UI test from an action recording

  1. In Solution Explorer, right-click a test project, point to Add, and then click Coded UI Test.

    - or -

    In the Test List Editor or the Test View window, right-click the surface of the window and then click New Test. In the Add New Test dialog box, click Coded UI Test and then click OK.

    - or -

    On the Test menu, click New Test. In the Add New Test dialog box, click Coded UI Test and then click OK.

    The Coded UI Test dialog box appears.

  2. Click Use an existing action recording.

    The Connect to Team Project dialog box appears.

  3. Find the work item with an attached action recording:

    1. Select your project in the Project drop-down list and click Connect.

    2. Choose a method to find a work item with an attached action recording: Execute a saved query, query by ID, or search by the title of the work item.

    3. Click Find to execute the query.

  4. Select the work item with the action recording you want to use to create a coded UI test, and then click OK.

    This adds the UI controls to the UI map, and actions to your coded UI test. A method is created for each step in the test case for the action recording.

  5. To generate assertions for your UI controls, right-click in the coded UI test source file, point to Generate Code and then point to Use Coded UI Test Builder.

    The Coded UI Test Builder dialog box appears.

  6. Start your application under test and navigate to the UI control that you want to add.

  7. Click the crosshairs icon in the Coded UI Test Builder and drag it to the control that you want to verify is correct.

    The Coded UI Test Builder - Add Assertions dialog box is displayed.

  8. Click the vertical bar to view the UI map.

    The UI control that you want to verify should be highlighted.

  9. Right-click the property for the UI control that you want to verify, and then point to Add Assertion.

    The Add assertion dialog box is displayed.

  10. Select the Comparator for your assertion.

  11. Type the value for your assertion in Comparison Value.

  12. To add the assertion, click OK.

  13. When you have added all your assertions for your test, close the Coded UI Test Builder - Add Assertions dialog box.

  14. To generate the code for your assertions, click the Generate Code icon.

    The Coded UI Test Builder - Generate Code dialog box is displayed.

  15. Type a name for your coded UI test method in Method Name, and then click Add and Generate.

    This generates code as follows if the name that you entered is, for example, AssertForAddTwoNumbers:

    • Adds a method called AssertForAddTwoNumbers to the UIMap.Designer.cs file. This method performs the assert statements that you added.

      public void AssertForAddTwoNumbers()
      {
          #region Variable Declarations
          WinEdit textAnswerEdit = 
              this.DemoCalculatorWindowWindow.AnswerWindow.TextAnswerEdit;
          #endregion
      
          // Verify that 'textAnswer' text box's Text property = '40'
          Assert.AreEqual(
              this.AssertForAddTwoNumbersExpectedValues.TextAnswerEditText, 
              textAnswerEdit.Text);
      }
      
  • Adds a call to the assert method AssertForAddTwoNumbers to the test method in your coded UI test file

    [TestMethod]
    public void CodedUITestMethod1()
    {
        // To generate code for this test, select "Generate Code" 
        // from the shortcut menu and select one of the menu items.
        this.UIMap.AddTwoNumbers();
        this.UIMap.AssertForAddTwoNumbers();
    }
    

When you choose a name for the method that has your assert statements, choose a name that is descriptive for these assertions that you created.

  1. Click the Close icon to close the Coded UI Test Builder.

  2. (Optional) To add code to start your application when the coded UI test runs, record the startup sequence for your application and save that to a method. You can call that method at the start of your test.

    Note

    You can add a test initialize method, identified by a [TestInitialize] attribute, which runs code at the start of each test method. For example, the method to start the application could be called from the TestInitialize method.

  3. (Optional) To add code to close your application when the coded UI test runs, record the closing sequence for your application and save that to a method.

    If you do not close the browser or application, it will remain open after your test is finished.

    Note

    You can add a test cleanup method, identified by a [TestCleanup] attribute, which runs code at the end of each test method. For example, the method to close the application could be called from the TestCleanup method.

  4. To run the test, right-click in the test method, and then click Run Tests. For more information about how to run coded UI tests, see Running Automated Tests.

    Note

    After you create your coded UI test with specific data, you might want to run your coded UI test multiple times with different sets of data to test different conditions. To do this you can add parameters from a data source to your coded UI test to create a data-driven coded UI test. For more information, see How to: Create a Data-Driven Coded UI Test.

For more information about action recordings, see How to: Create an Action Recording.

See Also

Tasks

How to: Generate a Coded UI Test by Recording the Application Under Test

How to: Add UI Controls and Validation Code Using the Coded UI Test Builder

How to: Create a Coded UI Test

How to: Create a Data-Driven Coded UI Test

Reference

UIMap

Assert

Concepts

Testing the User Interface with Automated UI Tests

Supported Configurations and Platforms for Coded UI Tests and Action Recordings