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

You can use the Coded UI Test Builder to add a user interface (UI) control to the UIMap for your test, or to generate code for a validation method that uses an assertion for a UI control.

Note

The Coded UI Test Editor lets you easily modify your coded UI tests. Using the Coded UI Test Editor, you can locate, view, and edit your test methods. You can also edit UI actions and their associated controls in the UI control map. The Coded UI Test Editor is included in the Microsoft Visual Studio 2010 Feature Pack 2. To download the feature pack, you must have either Visual Studio 2010 Ultimate, Visual Studio 2010 Premium or Test Professional 2010 with an MSDN subscription, Microsoft BizSpark, or MSDN Academic Alliance. For more information, see Editing Coded UI Tests Using the Coded UI Test Editor and Microsoft Visual Studio 2010 Feature Pack 2.

Add UI Controls to an Existing Coded UI Test

You can add a UI control for the application under test to an existing UI map and generate code to add the control to your coded UI test.

To add UI a control to a coded UI Test

  1. In Solution Explorer, open a coded UI test source file from a test project in your solution. The file name is CodedUITest1.cs). This is the same as the test name.

  2. To start the Coded UI Test Builder, right-click in the CodedUITestMethod1() method, select the Generate Code for Coded UI Test command. In the shortcut menu, select the Use Coded UI Test Builder command.

    The Coded UI Test Builder dialog box appears.

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

  4. Drag the crosshairs button in the Coded UI Test Builder to the UI control in your application that you want to test. When the box outlines your control, release the mouse. The control class code is immediately created in the UIMap.Designer.cs file.

    Coded UI test targeting element

    The properties for this control are now listed in the Coded UI Test Builder - Add Assertions dialog box. To see the UI controls in the map, click the arrow (<<) to expand the view for the UI control map. To find a parent, sibling, or child control, you can click on the map and use the arrow keys to move around the list of controls.

    Coded UI test properties

    Note

    You can also locate a UI control by pressing the Windows logo key + I. This is useful for testing a shortcut menu where the control is dismissed when you change the focus to the Coded UI Test Builder. For more information, see How to: Select a Control Using the Keyboard in Coded UI Tests.

  5. Close the Coded UI Test Builder or go to the next procedure to add validation assertions.

  6. To run the test, right-click in the test method, and then click Run Tests.

For more information about how to use UI control actions and set properties, see How to: Use UI Control Actions and Properties in Your Coded UI Tests.

Verify UI Controls in a Coded UI Test

After you add a UI control for the application under test to the UI map in your coded UI test, then you can use the Coded UI Test Builder to create a validation method to validate properties of the added UI control.

To add and verify controls in coded UI Test

  1. In Solution Explorer, open a coded UI test source file from a test project in your solution.

  2. To start the Coded UI Test Builder, right-click in the CodedUITestMethod1() method, select the Generate Code for Coded UI Test command. In the shortcut menu, select the Use Coded UI Test Builder command.

    The Coded UI Test Builder dialog box appears.

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

  4. To add a UI control to the UI map, drag the crosshairs to the UI control in your application that you want to test. When the box outlines your control, release the mouse.

    The properties for this control are now listed in Coded UI Test Builder - Add Assertions dialog box. To locate the UI control in the map, click the arrow to view the UI control map.

    Note

    You can also locate a UI control by pressing the Windows key + I. This is useful for testing a shortcut menu where the control is dismissed when you change the focus to the Coded UI Test Builder. To find a parent, sibling, or child control, you can use the arrows in the dialog box.

  5. Click the Add control to UI Control Map button in the toolbar to add this control to the UI map.

  6. Right-click the property that you want to verify, and then right-click and select the Add Assertion command.

    The Add Assertion dialog box appears.

    Coded UI test assertions

  7. Select the Comparator for your assertion: AreEqual.

  8. Type the value for your assertion in Comparison Value and click OK to add it .

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

  10. To generate the code for your assertions and add the control to the UI map, click the Generate Code icon.

    The Coded UI Test Builder - Generate Code dialog box appears.

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

    For more information about the anatomy of the generated code, see Anatomy of a Coded UI Test.

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

    Note

    For more information about how to use UI control actions and set properties, see How to: Use UI Control Actions and Properties in Your Coded UI Tests.

  13. 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 several 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.

See Also

Tasks

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

How to: Select a Control Using the Keyboard in Coded UI Tests

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

How to: Create a Coded UI Test

How to: Create a Data-Driven Coded UI Test

How to: Use UI Control Actions and Properties in Your Coded UI Tests

Reference

Assert

Concepts

Testing the User Interface with Automated UI Tests

Best Practices for Coded UI Tests

Supported Configurations and Platforms for Coded UI Tests and Action Recordings

Anatomy of a Coded UI Test