Inside TFS

Coded UI Tests: Creating a Coded UI Test in Visual Studio 2010

Visual Studio 2010 has added a lot of support for testing, especially for manual testing. In the past, when wanting to perform any sort of UI testing with Visual Studio, a manual test had to be used. The problem with manual testing is just that, it is manual, which requires human interaction. To solve this problem, Visual Studio 2010 has introduced a new type of test, called a Coded UI test, to help automate functional UI testing.

A Coded UI Test is not an automated test in the same vein as say, a unit test. Instead, a coded UI test actually runs and navigates through the application being tested, while validating UI elements to confirm the application is performing as expected. In essence, when a coded UI test runs, it is simulating a real human being running the application.

From a technical perspective, coded UI tests are created using either C# or Visual Basic. While Visual Studio 2010 does provide tools to help automate the process, at some point you may still have to write some code, which means anyone creating coded UI tests may need some development skills. One other requirement for creating Coded UI tests is that the application being tested must have been built using a supported technology, such as Windows Forms or HTML. The Visual Studio 2010 documentation has a list of current supported technologies, and this list is expected to grow over time.

Creating A Coded UI Test Using the Coded UI Test Builder
For our first introduction to coded UI testing, we will use a simple Hello World application, shown in Figure 1.


[Click on image for larger view.]
Figure 1. Simple Hello World Application.

This application takes the data entered into the First Word textbox, concatenates it with the data from the Second Word textbox, including a space between the words, and displays the result in the Result textbox.

Before we can create our Coded UI Test, we need to also add a test project to our Visual Studio solution. Once we have our test project, we can begin creating our coded UI test.

To add a new coded UI test to the test project, right-click on the test project in Solution Explorer, and select Add | New Test. This opens the Add New Test window. Select Coded UI Test, and name it, as shown in Figure 2. Click OK to close this window.


[Click on image for larger view.]
Figure 2. Add New Test Window.

Click OK, and a new windows opens, shown in Figure 3.


[Click on image for larger view.]
Figure 3. Generate Code for Coded UI Test Window.

This dialog box gives you options to help generate the code related to test. You have the ability to use an existing action recording, which is a recording made during a manual test run. You also have the ability to record your actions now, and then add validation to the recording. Select "Record actions, edit UI MAP or add assertions" and click OK to start the recording process. Visual Studio will minimize, and the Coded UI Test Builder will open in the lower right corner of your screen (Figure 4).


[Click on image for larger view.]
Figure 4. Coded UI Test Builder.

This window is responsible for recording your tests, generating source code from your recording, and selecting and identifying properties of controls that need validation. Click the red Record button to begin recording the test. Once recording starts, you can pause the recording at any point by clicking the Pause button that will appear on the Coded UI Test Builder window.

Once the recording has started, run the simple Hello World application (I did this by creating a shortcut to the app on my desktop, and double-clicking the shortcut). Enter a value in each field, and click the Create button to concatenate the two values together and display the result in the Results field. Click the Pause button to pause the recording.

To see the steps that have been recorded, click the Show Recorded Steps button in the Coded UI Test Builder window, or press Alt+S. The results are shown in a popup window as shown in Figure 5.


[Click on image for larger view.]
Figure 5. Recorded Actions.

Now we want to add the validation/assertion logic to ensure that we are getting the correct result. First we need to take the recording and turn it into source code. In the Coded UI Test Builder window, click the Generate Code button (or press Alt+G). You will be prompted to give the generated code method name a name (Figure 6).


[Click on image for larger view.]
Figure 6. Name the Method.

Click the Add and Generate button to convert the recording into source code, using the method name specified.

Next we want to add some validation logic. Notice on the Coded UI Test Builder window, that the crosshair icon is now enabled. You use this icon to add validation/assertion logic to your test. For this scenario, we want to validate the data in the Results textbox. Click and hold to select the crosshair icon, then drag it over the Results textbox in the application. A blue box will appear around the text box, highlighting it and indicating it is the target. Release the mouse button to select the control as the target. The properties for the selected control will be displayed in the lower right corner of the screen (Figure 7).


[Click on image for larger view.]
Figure 7. Properties of the selected control.

For this validation test, we want to make sure the results textbox is displaying the correct value. In the Properties window, select the Text property, then click the Add Assertion button at the top of the window. A dialog window will appear to help build the assertion (Figure 8).


[Click on image for larger view.]
Figure 8. Add Assertion dialog box.

For this scenario, the default values are fine. We could select a different comparison option from the drop down list box, or change the comparison value, if required. Click the OK button to create the assertion. Notice in the Property window (Figure 9) there is a check mark next to the Text field, indicating an assertion targeting that field. Hovering the mouse over the checkmark will display information about the assertion.


[Click on image for larger view.]
Figure 9. Property Windows.

The validation code that was just added now needs to be turned into source code. Click the Generate Code button again to do this. We will be prompted to give your assertion a method name (such as AssertHelloWorld). Click the Add and Generate button to generate the source code related to our assertion.

At this point, we can click the record button and record more tests and validations. Once we are finished, simply close the Coded UI Test Builder to return to Visual Studio.

In this column, we have discussed what coded UI tests are, and shown how to record a simple coded UI test and add validation to it. In our next column, we'll examine the code created by the Coded UI Test Builder, and show how to run the tests.

About the Author

Mickey Gousset spends his days as a principal consultant for Infront Consulting Group. Gousset is lead author of "Professional Application Lifecycle Management with Visual Studio 2012" (Wrox, 2012) and frequents the speaker circuit singing the praises of ALM and DevOps. He also blogs at ALM Rocks!. Gousset is one of the original Team System/ALM MVPs and has held the award since 2005.

comments powered by Disqus

Reader Comments:

Sun, Jan 15, 2012 Vikram Rawal Mumbai

Very useful technique for Validation...

Thu, Apr 21, 2011

How reliable is this UI testing framework. I have worked with testing frameworks, Heck I build my own. There are a lot of issues with UI testing (Timeouts, 3rd party controls, async operations, different processes, etc). Is VS2010 offering a complete and extensible solution or just a solution for simple Hello World apps ?

Fri, Feb 18, 2011

trivial but useful Thanks!

Fri, Jan 14, 2011

how about if I want to create a Code UI test for an executable especially if I can't afford to migrate our VS2008 codes yet to VS2010 for the meantime?

Wed, Jan 12, 2011 Newbie India

So people who didn't realize and bought VS2010 professional can't use Coded UI at all. There should be a way to get these as add-ins just thinking out loud. Please help me how I can use Coded UI within my installed VS2010 Professionalthanks in advance,

Sat, Dec 11, 2010

sorry, but quite trivial article...

Tue, Dec 7, 2010 Tod Bookless Kent, WA

Hi - I don't see anywhere that you indicate which version of Visual Studio 2010 that a person needs to have in order to make a Coded UI Test, because it is not part of Test Professional.

Mon, Nov 8, 2010 Priya CA

This article is very usuful and easy to understand. I am a starter with TFS automation tool. Looking forward for further posts.

Thu, Nov 4, 2010

This is only availabel with Visual Studio Premium or Ultimate.

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.