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

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube