Exercise 2: Introduction to Code Generation using Coded UI Test Builder
In this exercise, you will learn how to use the Coded UI Test Builder to generate test code for the Tailspin Toys Web application and modify the generated code in order to enable data driven testing.
Open Internet Explorer and click on the Tailspin Toys button from the favorites bar.
Figure 11
Tailspin Toys link
- Select the Model Airplanes link.
- Select the Fourth Coffee Flyer link.
- Select the Add To Cart link to load the shopping cart.
Return to Visual Studio, locate the CodedUITestMethod1 method in the CodedUITest1.cs file, and add a blank line after the call to the “this.UIMap.Clickonwhitespaceinwebsite” method.
Figure 12
Adding blank line to test source
Right-click at the location of the blank line and select Generate Code for Coded UI Test | Use Coded UI Test Builder… from the context menu. This will load the Coded UI Test Builder window (which is always displayed over other windows) and the Internet Explorer instance that we previously left open.
Figure 13
Starting the Coded UI Test Builder
The Coded UI Test Builder is used to record actions and assertions within the user interface which are then converted to code.
Now we will add an assertion to verify that the Quantity textbox is equal to 1. Drag and drop the crosshair from the Coded UI Test Builder tool window onto the Quantity textbox in Internet Explorer. This action will load a window showing the control properties.
Figure 14
Selecting an element to use for assertion
- Select the Text property and click the Add Assertion button. This will load a dialog to finalize the assertion options to use.
Verify that the Comparator to use is AreEqual and that the comparison value is ‘1’. Select the OK button to continue.
Figure 15
Selecting the comparator type and value
Verify that a checkbox has been added to the Text property row in the Coded UI Test Builder window.
Figure 16
Text property showing assertion checkbox
Click on the Generate Code button in the Coded UI Test Builder tool window.
Figure 17
Generate Code button location
In the Generate Code window, use QuantityEqualsOne for the Method Name and select the Add and Generate button to generate the validation code.
Figure 18
Generating assertion code
- Remove the “Fourth Coffee Flyer” item from the shopping cart.
- Close the Coded UI Test Builder.
- Close the Internet Explorer window and return to Visual Studio.
Note that the assertion code generation has added the new validation step.
Figure 19
Newly created assertion step for coded UI test
- Right-click and select Run Tests to run the tests with the new validation steps.
Next Step
Exercise 3: Data Driven Demonstration for Coded UI Test