2 out of 3 rated this helpful - Rate this topic

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.

NoteNote

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.

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

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

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
When adding a control to map you still need to generate code
If you add a control to your map, you still need to click the generate button from the UI test builder (unless you had add an asserts on to it)
Seems to be too much tool centric
Having worked with QTP and other open source tools lisk WATIN, I feel forcing the automation developers to store their objects at design time is not such a good idea. It is generally a road block when it comes to script maintenance, since the objects stored in the repository - UIMAP in this case are tightly coupled with the underlying script.  Any change in the UI name needs a change at two places UP map and the script.  Also bigger the app messier the UIMAP can get, it is one gigantic aspect of the automation which completely overshadows the scripting part.
Also addition assertions at the time of scripting looks very novice to me, I would rather prefer a model where assertions can be maintained outside the tool. Why not go the XAML way, allow automation developers to keep their objects definition in simple to maintain excel files or XML and allow them to create the objects at run time. This will allow automation developers to create reusable functions that can work on the metadata stored in the external files. 
Inconvenience customer experience caused by Over laid stress on Code reuse and custom
It do make my work hard at first
Yh..The most puzzle one is locate the proper control and parameterize it. Even more inconvenient when the object is deeply nesting.
The objects regnized by crosshair is too fussy even than the HTML language , by the way Large application with huge data and fields is what I works on.Most of the time,it costs me much more time to generate a reusable automatic test case than program a page .
I think QTP is little better working at the object spy . I only see the table/cell/row/name/value/index... etc propertis.
Also , the search / specific / common / filter ... kinds of properties which is what I need...
Microsoft.VisualStudio.TestTools.UITesting controls API should be more helpful to us which I hardly seen , I would rather use the IE Developer bar instead
Sometimes I have to decompile the dll file to seek a function...
Coded UI Test would be quite a issue to hardcode-inexperience tester..
Opening "UI Location of UIMap"
This took me forever to find and I almost gave up.