Inside TFS

Coded UI Tests (Part 4): Tips and Tricks

Mickey Gousset finishes out his series on coded UI testing with some useful tips and tricks.

So far in this series of columns on Coded UI Testing, we have covered the basics of how to create a coded UI test, how to run and edit the test, as well as how to make the test data-driven. In this column, we are going to look at some tips and tricks to help make your coded UI tests better.

Things to Consider When Recording Your Test
Keep in mind that once you press the record button on the Coded UI Test Builder window, it is going to record every action you perform until you pause the recording. Which means if, while recording a test, you decide to open Microsoft Outlook and check your email, that will be recorded as well. As you can imagine, this will clutter your test with unnecessary actions, and cause unexpected results. Moral of the story: while recording a test, do not interact with anything that is not related to the test.

If you must interact with a non-test object, make sure to pause the recording by clicking the pause button on the Coded UI Test window. However, be very careful not to interact with the application you are testing while the recording is paused. If you pause a recording, interact with the application being tested, then continue recording the test, you have changed the state of the application, which could cause the test to fail on playback.

Adding New Assertions to an Existing Test
Next, let's see how we can add a new assertion to an existing coded UI test. For this scenario, we will take our sample application, and add an assertion to check the value of the First Word textbox. In Visual Studio 2010, in the CodedUITest1.cs file, in the CodedUITestMethod1() method, add a new line after this.UIMap.TypeValueAndClickCreate(). Right-click on this blank line, and select Generate Code for Coded UI Test | Used Coded UI Test Builder from the context menu. Then start the Hello World application.

From the Coded UI Test Builder window, drag and drop the crosshairs icon onto the First Word textbox. This will open the properties window for that textbox. Select the Text property and click Add Assertion from the toolbar. Take the defaults in the Add Assertion window (which are "AreEqual", with a comparison value of blank), and click OK. Finally, click the Generate Code button on the Coded UI Test Builder window to generate the assertion code. Give the generated method a name (such as "AssertFirstWord") and click the Add and Generate button to generate the code. Close the Coded UI Test Builder window to return to Visual Studio.

The code should now look similar to Figure 1.


[Click on image for larger view.]
Figure 1. Code After Adding New Assertion.

At this point, since this is a data-driven test, we need to connect the data file to this assertion, to feed the appropriate value into the assertion. Add the following line of code above the AssertFirstWord() method:

this.UIMap.AssertFirstWordExpectedValues.UITextBoxFirstWordEditText = TestContext.DataRow["FirstValue"].ToString();

Now when you run the test, it will use the appropriate value from the FirstValue column in the comma-delimited file.

How To Change Time Between Two Actions
Coded UI tests can execute very quickly. There may be times when you need to implement a pause or a delay between actions. This is accomplished by using the Playback object. For example, to create a five second pause after an action, add the following code just after the action method call:

Playback.PlaybackSettings.DelayBetweenActions = 5000;

You may also have an instance where you need to wait for a process to finish before continuing with the test. You can use the following line of code to wait until the UI thread is finished:

Playback.PlaybackSettings.WaitForReadLevel = WaitForReadyLevel.UIThreadyOnly;

Conclusion
As this series of columns has shown, the coded UI test is a great addition to the testing functionality of Visual Studio 2010. One thing we did not show in this series, but that you should be aware of, is that you can take the action recording from a manual test and turn it into a coded UI test. This allows you to initially run a test as a manual test, and then turn it into an automated process for regression testing.

About the Author

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

comments powered by Disqus

Reader Comments:

Wed, Sep 5, 2012 Soumyageet Pune

In case , there is any enhancement in GUI , suppose a new TEXTBOX is introduced , can i modify existing CODEDUI to accomodate the change w/o doing any recording ?

Mon, Oct 17, 2011 Naresh India

can you please provide more information on codedUITest for flash projects

Wed, Oct 5, 2011 Julie Xie US

Have you tried to use this tool to explore within Outlook 2010. I found that it fails to access the indivudual emails.

Mon, Feb 14, 2011 Robert US

Hi, I am also having trouble with the time between actions. To try to save time I've removed all the "hover-over" and other needless actions, also minimized searches to only the controls id (we're using javascript with IE). But the test seems to pause for 10's of seconds between many steps. I have a lot of experience with Selenium (which runs much quicker) and I've got to believe there's something wrong with the default MSTest configuration, and it must be capable of running many times faster than it is now.

Fri, Feb 11, 2011 ket

Hi, i have a trouble with the Time Between Two Actions during test running - when i run test some actions play very quikly, but some - very slow. For example, i click submit and redirect to another page and quikly click Close button (without postback) there. Then i run test, it stands on the last page at about 40sek and do nothing,then he close window. What for do the test are waiting for on that page? Why there are delay? (i didn`t insert any delay in designer)

Tue, Jan 18, 2011 TerryC

I have created a coded UI test against a website that works great. However, using a recorded Web Performance test I have the ability to parameterize the URL so that I can test against different servers and run the tests from a command line. Are these features available with coded UI tests without extensive hand coding?

Tue, Jan 18, 2011

When a webtest is run in VSTS 2010 (after linking a csv file as data source), the test fails and the following error message is displayed: "Request failed: Context parameter 'users.Username' not found in test context". Please help.

Wed, Jan 12, 2011 newbie India

Got VS2010 professional didn't realize that Coded UI Test is not included in it. How can I obtain just this Coded UI or Testing essentials like all kind of Test Projects

Add Your Comments Now:

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