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

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

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

d. This brings up the New Data source Wizard

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.

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

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’

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

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.