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. |
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
-
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.
-
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.
-
Start your application under test and navigate to the UI control that you want to add.
-
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.
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.
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.
-
Close the Coded UI Test Builder or go to the next procedure to add validation assertions.
-
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
-
In Solution Explorer, open a coded UI test source file from a test project in your solution.
-
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.
-
Start your application under test and navigate to the UI control that you want to add.
-
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.
-
Click the Add control to UI Control Map button in the toolbar to add this control to the UI map.
-
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.
-
Select the Comparator for your assertion: AreEqual.
-
Type the value for your assertion in Comparison Value and click OK to add it .
-
When you have added all your assertions for your test, close the Coded UI Test Builder - Add Assertions dialog box.
-
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.
-
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.
-
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.
-
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.
- 9/28/2011
- B. Huard i
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.
- 2/4/2011
- Deepankar Bandopadhya
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..
- 12/24/2010
- 咸蛋达人
- 12/3/2010
- charlierlee