Data Driven Approach in Coded UI Test

Posted by in Visual Studio category on for Beginner level | Points: 250 | Views : 30387 red flag

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

3.       Databases

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




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

CSV Files

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)



Variable Declarations

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 '' using new browser instance

this.UIExercise3DataDrivenDWindow.LaunchUrl(new System.Uri(this.RecordedMethod3Params.UIExercise3DataDrivenDWindowUrl));

// 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 Name' pane

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

Mouse.Click(uISearchButton, new Point(21, 20));

// Click 'Close' button

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.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Ruchi Srivastava
Member Level:
Member Status: Member
Member Since: 3/2/2011 1:00:59 AM
Country: India

Login to vote for this post.

Comments or Responses

Posted by: Tripati_tutu on: 3/16/2011 | Points: 25
Nice one, but you have placed as an image for codes. It is not clearly visible.

So if its possible, then try to attach your project in this article or place the codes in place of code images, then I think it will helpful for the viewers.

Posted by: Dynamicruchi on: 3/19/2011 | Points: 25
Hi Tripati

I have put the code instead of code images. It should be clearly visible now.
Posted by: Tripati_tutu on: 3/19/2011 | Points: 25
Thanks for doing this...
Posted by: Smsbvt on: 6/19/2012 | Points: 25
Thanks for the post Ruchi. I don't mean any dis-respect, but there seem to be about a dozen web blogs where people are talking about data-driven CodedUI. Most of them show the simple work of attaching to a CSV, XML or dataset from a db, and this is great. I like the way you show the code page with the changes in place and the images of setting up the db connection.

I just have a few problems with the information provided by most of them:

1. The samples uses a data connector, what if the data connection is in the web.config or uses a DAL to collect the data?

2. Most samples show direct modification in a single method within the test methods, why not change the value at the field itself? This way the data is consistent across all the test case code and you do not have to track down every method using the field to make changes.

3. What if the data content for the test sets is different from shall we say navigational data? Example, we have a seperate database that handles our navigational structure, which is where we have to derrive what page to load, and verify that we have arrived at the expected page. Meanwhile, we have another data source where we compare our tests data on the pages, such as the content values from a search post.

4. Which brings up the final question, this method of "Data Driven CodedUI" is great for iterrating through test sets, but What about "Data Driven Methods". This is to say, the code itself from a click through is pretty static. The first thing a QA dev wants to do is to make it more dynamic to reduce redundancy in code, improve effiency and handle dynamic linkages. No one talks about these things. Maybe when a web site loads on one www server and jumps to another based on user navigation events, what about handling these things. No samples out there for this. Although the answer is similar, it would create a new test result if this data was tied to the provided data sources this "Data Driven CodedUI" process would utilize (potentially).

Along with #4 above come other issues to resolve when using data navigation content.

Login to post response

Comment using Facebook(Author doesn't get notification)