Handling Dynamic Values

Handling Dynamic Values

In this application, BlogEngine.NET (and most blogging tools), the URLs are constructed based on the date that the pages are created. In looking at the posts created in the previous set of tests, the URLs all follow the following pattern: http://[server]:[port]/post/[year]/[month]/[day]/[post name].aspx. You need to pull these values out to use as search criteria for the right link to click.

Handling Dynamic Value s 173

Listing 6-10: Initial Add New Post and Read Test [TestMethod]

public void CodedUITestMethod1() {

this.UIMap.LogonasJeffEditor(); this.UIMap.ClickAddEntry(); this.UIMap.EnterTestBodyforthebodyandclickSavePost(); this.UIMap.ClicktheLogoffbutton(); this.UIMap.ClicktheLogOnlink(); this.UIMap.EnterthepasswordasPssw0rdandclickLogin(); this.UIMap.ClicktheNewTestPostlink();

The third method is the one that creates the URL that needs to be con- sumed by the last method. However, this URL is never returned in code, so you need to figure out how to fix it so you can actually click the link in the last

174 Chapter 6: Automating Te st Case s So, to do that, comment out all the lines after the first three lines and exe-

cute the test. (You want the browser to be open at the point that the new post has been created.) Open the Coded UI Test Builder, and select the link for the new post using the crosshairs. Next, expand the Coded UI Test Builder win- dow using the double arrow in the upper-left corner to show the control sec- tion. Click the Add Control to UI Map button in the upper-left corner. A dark red check mark displays next to the control, and its parent controls and a mes- sage display at the bottom of the window indicating the controls have been added to the UI Map. Next, select the Generate Code button (Alt+G is the shortcut key); you will see a message stating a new method is required and only code related to the UI control map will be generated. Click Generate and close the Coded UI Test Builder.

To all outward appearances no changes were made, but some underlying

Handling Dynamic Value s 175

Listing 6-11: Continued {

if ((this.mUIPost0Pane == null)) {

this.mUIPost0Pane = new UIPost0Pane2(this); } return this.mUIPost0Pane;

The search criteria are the first critical element because that helps you find the pane, and the second critical item is the Post Pane that contains the actual hyperlink that you want to click. Drilling down into the UIPost0Pane2 class gives you access to the actual hyperlink. Now, the search criteria for this con- trol look similar to the actual information you need to find the hyperlink. In

176 Chapter 6: Automating Te st Case s Listing 6-12: Continued

10 string href = string.Format(“http://tfs2010:8001{0}”, absolute Path);

11 this.UIMap.ClicktheLogoffbutton(); 12 this.UIMap.ClicktheLogOnlink(); 13 this.UIMap.EnterthepasswordasPssw0rdandclickLogin();

//Replace the generated search properties with our own 14 HtmlHyperlink actualLink =

this.UIMap.UIInternetExplorerEnhaWindow. UINameoftheblogShortdeDocument. UIPost0Pane1.UINewTestPostHyperlink;

15 actualLink.FilterProperties [HtmlHyperlink.PropertyNames.AbsolutePath] = absolutePath; 16 actualLink.FilterProperties[HtmlHyperlink.PropertyNames.Href] = href; 17 actualLink.FilterProperties [HtmlHyperlink.PropertyNames.ControlDefinition] =

Handling Dynamic Value s 177

on the first call, when the code in line 18 calls it, it uses the already created instance with the overridden search criteria and finds the link on whatever day you run this Test Case on.

This might be more complicated than it seems, but consider the alterna- tive: If you did not have access to any generated code, you could not create tests that handle dynamic situations like this. So, although it is a bit of work to get right, when you do you can run the tests regardless of any dynamic val- ues, and you can tailor it to any application and any scenario. Hopefully, this real-world type of example can make it easier for you to handle the dynamic values in your own applications.

Other Tips In the previous example, the URL was constructed because the assumption

178 Chapter 6: Automating Te st Case s You can find more information about these and other methods of the UITest-

Control class at http://msdn.microsoft.com/en-us/library/dd434055.aspx. In addition to these settings, there is a static class called Playback that you can call any member on during the testing process. This class contains a

method called PlaybackSettings that enables you to call methods such as DelayBetweenActions that set an overall wait period between steps or the SearchTimeout method. These two classes are key classes for manipulating the

generated Coded UI test. More information is available at http://msdn. microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting. playback.aspx and http://msdn.microsoft.com/en-us/library/microsoft. visualstudio.testtools.uitesting.playbacksettings_members.aspx. You can find the complete list of classes in the UITesting namespace at http://msdn. microsoft.com/en-us/library/dd405972.aspx. Get to know these classes and

A ssociating Coded UI Te sts and Te st Case s 179

and do nothing while the test executes. This is, as you may guess, an incred- ible waste of time. To get the full benefit of the automated test, you need to associate it with a Test Case. (This also lights up many of the reports.)

To associate a Test Case with an automated test, open the Test Case you want to make the association with and select the Associated Automation tab (Figure 6-13).

180 Chapter 6: Automating Te st Case s Automation tab. Save the work item and you are ready to proceed. As a crit-

ical side note, the code that you associate to the Test Case must be stored in TFS or any attempt to execute the automated test will fail.

Another option, as mentioned previously, is in the Test List Editor: Right- click the test, and select Associate Test to Test Case; then find the Test Case and select it (Figure 6-14).

A ssociating Coded UI Te sts and Te st Case s 181

After the first set of Test Case work items is generated, when you run this command again it merely updates the Test Case work items and suites with new information rather than duplicating the Test Cases. You can also use this tool to add these Test Cases directly to an existing suite; if this suite happens to be a requirements-based suite, you have succeeded in linking the Unit Tests to requirements with no additional work required.

ASSOCIATING UNIT TESTS Use the same process to associate a Unit Test with a Test Case work item. For Unit Tests you generally need to take one additional step. After the Test Cases have been generated for a Unit Test, you need to associate the Test Cases with a requirement; this is not done for you (unless you use the /sync-

182 Chapter 6: Automating Te st Case s

SUMMARY This chapter taught you how to create and manipulate automated Test Cases.

This includes the ability to add validation to the automated test using the Coded UI Test Builder and then to modify any generated code. At this point, you should handle more complex situations involving dynamic data values. Even though this chapter focused on dealing with Web Applications, the principles apply to WinForms and WPF applications equally.

You also know that you can associate Unit Tests with Test Cases and then with requirements to get requirements coverage information from Unit Tests using the tcm.exe command-line tool. Using this tool, testers can also execute Unit Tests as part of their automated test runs.