Welcome to MSDN Blogs Sign in | Join | Help

Data Driving Coded UI Tests

Frequently, we have to repeat a test with different data values. This ‘data-driving’ is made very easy in Coded UI Test. In the Tutorial, we created a Coded UI Test to verify the addition of two numbers in the calculator. Let us now see how we can convert it into a data-driven test.

Step 1:- Create the Coded UI Test – See Tutorial

 

Step 2:- Create the data sets. Coded UI Test supports multiple data sources. The data sets may be defined in a CSV (comma separated values) file, an Excel worksheet, an XML file, database table or from a test case on TFS. For this walkthrough, we will use a CSV file with the following data.

Add1

Add2

Sum

7

2

9

5

2

7

3

2

5

 

Step 3:- Add the Data Source binding in Coded UI Test.

a. Open the Test View window (from Test -> Windows -> Test View

clip_image002

b. Choose the Coded UI Test that we created and from the context menu click Properties.

clip_image004

c. In the Properties Window click on the button in Data Connection String property to create a new Data Connection.

clip_image006

d. This brings up the New Data source Wizard

clip_image008

e. Choose CSV file and click Next

f. Select the CSV file that we created in Step 2. A preview of the contents is shown in the Wizard.

clip_image010

g. Click Finish. A prompt comes up to add the Data file into the project.

clip_image012

h. Click Yes.

i. A Data Source attribute is added to the Coded UI Test.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]

Step 4:- Use the data in the Coded UI Test.

a. Open the Recorded methods file. The following snippet of code clicks on Button 7 in calculator.

// Click '7' button

WinWindow item7Window = new WinWindow(calculatorWindow);

#region Search Criteria

item7Window.SearchProperties.Add("ControlId", "131");

#endregion

WinButton item7Button = new WinButton(item7Window);

#region Search Criteria

item7Button.SearchProperties.Add("Name", "7");

#endregion

Mouse.Click(item7Button, new Point(26, 12));

b. Change the highlighted line to

item7Button.SearchProperties.Add("Name", testContext.DataRow["Add1"].ToString());

NOTE: Test Context object contains a handle to all the Data that is present in the Data Source. We can reference it with the column name (e.g:- “Add1”)

c. Similarly the following code snippet clicks Button 2 in calculator

// Click '2' button

WinWindow item2Window = new WinWindow(calculatorWindow);

#region Search Criteria

item2Window.SearchProperties.Add("ControlId", "126");

#endregion

WinButton item2Button = new WinButton(item2Window);

#region Search Criteria

item2Button.SearchProperties.Add("Name", "2");

#endregion

Mouse.Click(item2Button, new Point(23, 11));

d. Change the highlighted line to

item2Button.SearchProperties.Add("Name", testContext.DataRow["Add2"].ToString());

e. Open the Coded UI Test file. The following line verifies the result.

// Validate UIItemEdit.Text AreEqual '9. '

Assert.AreEqual("9. ", UIMap.UICalculatorWindow.UIItemWindow.UIItemEdit.Text);

f. Modify this line to

StringAssert.StartsWith(UIMap.UICalculatorWindow.UIItemWindow.UIItemEdit.Text, TestContext.DataRow["Sum"].ToString());

NOTE:- We changed the Assert to a StringAssert, the comparator to StartsWith & reversed the order of the arguments.

Step 5:- Run the data driven test.

Right click inside the Coded UI Test Method and choose ‘Run Tests’

clip_image014

The test will run 3 times (as many iterations as there rows in the Data Source). The Test Results will show each iteration details.

clip_image016

You have seen in this walkthrough how to create a data driven tests. If the Test Case is authored in Camano, there is an even simpler way to make it data-driven. More about this workflow in the next article.

Published Tuesday, March 17, 2009 12:47 AM by Mathew Aniyan
Filed under:

Comments

# infoblog » Data Driving Coded UI Tests

Monday, March 16, 2009 10:29 PM by infoblog » Data Driving Coded UI Tests

# Data Driving Coded UI Tests - Mathew Aniyan's Blog

Thank you for submitting this cool story - Trackback from DotNetShoutout

Tuesday, March 17, 2009 4:17 AM by DotNetShoutout
Anonymous comments are disabled
 
Page view tracker