In my previous articles, we have discussed about Coded UI basics and Object Identification Mechanism in Coded UI .
In this article we will look into Data Driven approach in Coded UI. What dataSources it supports for providing the test data and how to use these datasources while working with CodedUI.
To test the functionality of any application, we test it with different identified sets of data to make sure the functionality does not break while iterating through all these data sets.
Coded UI supports 3 different datasources that can be used for providing the test data.
1. CSV files
2. Xml files
In the following sections we will discuss about CSV files as datasources in detail. We will discuss about XML files and Databases in detail in the next articles.
Steps to perform before configuring the DataSource
Before we start discussing about the datasources we will setup the recording of a scenario of opening the Internet Explorer, type http://www.bing.com/ in address bar, click on enter. Once the Search page gets opened, type ‘Visual Studio 2010’ in the search text boa and click on the search button. The Search results will come up.
I have already used the same example while we discussed about the record and playback in the article Coded UI basics .
Now we will see how we can provide various test data for Search instead of hardcoding ‘Visual Studio 2010’ as the test data. We will use each DataSource one by one for demonstration.
We will follow the following steps for opening the DataSource selection wizard:
1. Open the Test List Editor and you will find the recorded method ‘CodedUITestMethod1’ present in the editor.
2. Right click on the Test Name ‘CodedUITestMethod1’ and click on the Properties.
. Select the Data Connection String property from the Property Browser and open it. You will see the ‘New Test DataSource Wizard’ as shown below:
Configuring the Different DataSources
1. Select the CSV File as the datasource and click on the ‘Next’ button.
2. Choose the CSV files from the next screen.
You will see the data present in CSV file as below:
3. Click on the Finish button to complete the configuration. Clicking on Finish will show a pop up to copy the database file into the current project.
4. Click on ‘Yes’ button and it will copy the csv file in the current project.
5. Open the CodedUITestMethod1 and notice the attribute on top of the method:
DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData.csv", "TestData#csv",
DataAccessMethod.Sequential), DeploymentItem("SampleCodedUIProject\\TestData.csv"), TestMethod]
public void CodedUITestMethod1()
The information related to the TestData Source get added on top of the method.
6. Now, modify the CodedUITestMethod1 a bit to parameterize the test using TestContext. Add the following line in the test method:
string testData = TestContext.DataRow ["SearchData"].ToString();
7. As we have changed RecordedMethod1 to take one input parameter ‘testData’, we will change its implementation a bit. Go to the method definition and supply the testData where we were taking the ‘Visual Studio 2010’ as a hardcoded input. See the below highlighted line, we will change this line to take ‘testData’ as the value.
public void RecordedMethod1(string testData)
HtmlEdit uIEnteryoursearchtermEdit = this.UIExercise3DataDrivenDWindow.UIBingDocument.UIEnteryoursearchtermEdit;
HtmlDiv uIItemPane = this.UIExercise3DataDrivenDWindow.UIBingDocument.UISb_formCustom.UIItemPane;
HtmlInputButton uISearchButton = this.UIExercise3DataDrivenDWindow.UIBingDocument.UISearchButton;
WinButton uICloseButton = this.UIExercise3DataDrivenDWindow.UIVisualstudio2010BingTitleBar.UICloseButton;
// Go to web page
'http://www.bing.com/' using new browser instance
// Set flag to
allow play back to continue if non-essential actions fail. (For example, if a
mouse hover action fails.)
Playback.PlaybackSettings.ContinueOnError = true;
// Mouse hover
'Enter your search term' text box at (213, 5)
Mouse.Hover(uIEnteryoursearchtermEdit, new Point(213, 5));
// Reset flag to
ensure that play back stops if there is an error.
Playback.PlaybackSettings.ContinueOnError = false;
// Click 'Unknown
Mouse.Click(uIItemPane, new Point(151, 34));
// Type 'visual
studio 2010' in 'Unknown Name' pane
Keyboard.SendKeys(uIItemPane, this.RecordedMethod3Params.UIItemPaneSendKeys, ModifierKeys.None);
// Type 'visual
studio2010' in 'Enter your search term' text box
uIEnteryoursearchtermEdit.Text = testData;
// Click 'Search'
Mouse.Click(uISearchButton, new Point(21, 20));
// Click 'Close'
Mouse.Click(uICloseButton, new Point(18, 8));
8. Now we are ready to run our test with different testData values as supplied from the CSV file. Run your test case, the test will run as many times the data present in csv file.
See the result of the sample run below. The test run for two iterations as we have provided two values in the CSV file.
Coded UI provides 3 different datasources support. Based on the project requirement we can use any of these datasources. In this article we discussed about how to use CSV files as the source of TestData. In next articles we will talk about XML files and Databases in detail.