Data driven approach in Coded UI-Part 3

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

In my previous article on data driven approach in coded UI, we have discussed about how to use the CSV files and Xml files as the dataSource.


In this article we will look into Data Driven approach in Coded UI using Databases for providing the test data and how to use this dataSource while working with CodedUI.

Steps to perform before configuring the DataSource

Please refer to my previous article data driven approach in coded UI to see the steps to perform before configuring the datasource.

Configuring the Database as the datasource

1. Select the Database as the datasource and click on the 'Next' button.


2. Select the related datasource from the next screen. See the below screenshot.

3. Select the Server name from the ‘Server Name’ dropdown in the next screen.




 Also, select the database name from ‘Connect to a database’ section in the same screen.




4. Click on the ‘Test Connection’ button present on the same screen.



If the connection is successful, you will get a message that ‘Test connection succeeded’. Click on OK button on the message pop up as well as the main screen.

5. Next screen will show you the connection string. Click on the ‘Next’ button.



6. From the next screen select the table from which you want to fetch the data and click on ‘Finish’ button.




Next all steps will be same as discussed in my pervious article. I am reiterating the steps again.

7. Open the CodedUITestMethohod1 and notice the attribute on top of the method:

[DataSource("System.Data.SqlClient", "Data Source=MyMachine\\SQLEXPRESS;Initial Catalog=CodedUI;Integrated Security=True", "BingSearch", DataAccessMethod.Sequential), TestMethod]

public void CodedUITestMethod1()


// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.

//For more information on generated code, see

string testData = TestContext.DataRow["TestData"].ToString();




The information related to the DataSource get added on top of the method.

8. 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();

       this.UIMap.RecordedMethod1 (testData);

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

/// <summary>

/// RecordedMethod1 - Use 'RecordedMethod1Params' to pass parameters into this method.

/// </summary>

public void RecordedMethod1(string testData)


#region Variable Declarations

WinEdit uIAddressandsearchusinEdit =this.UIBlankPageWindowsInteWindow.UIItemWindow.UIAddressandsearchusinEdit;

WinButton uIGotowwwbingcomAltEntButton = this.UIBlankPageWindowsInteWindow.UIPageControlToolBar.UIGotowwwbingcomAltEntButton;

HtmlEdit uIEnteryoursearchtermEdit = this.UIBlankPageWindowsInteWindow.UIBingDocument.UIEnteryoursearchtermEdit;

HtmlDiv uIItemPane = this.UIBlankPageWindowsInteWindow.UIBingDocument.UISb_formCustom.UIItemPane;

HtmlInputButton uISearchButton = this.UIBlankPageWindowsInteWindow.UIBingDocument.UISearchButton;

WinClient uIVisualStudioBingWindClient = this.UIBlankPageWindowsInteWindow.UIItemWindow1.UIVisualStudioBingWindClient;


// Go to web page about:blank' using new browser instance

this.UIBlankPageWindowsInteWindow.LaunchUrl(new System.Uri(this.RecordedMethod1Params.UIBlankPageWindowsInteWindowUrl));

// Type '' in 'Address and search using Bing' text box

uIAddressandsearchusinEdit.Text =


// Click 'Go to "" (Alt+Enter to open in a new ...' button

Mouse.Click(uIGotowwwbingcomAltEntButton, new Point(13, 9));

//Type 'VisualStudio' in 'Enter your search term' text box

uIEnteryoursearchtermEdit.Text = testData;

// Click 'Unknown Name' pane

Mouse.Click(uIItemPane, new Point(403, 3));

// Click 'Search' button

Mouse.Click(uISearchButton, new Point(16, 10));

// Click 'VisualStudio - Bing - Windows Internet Explorer' client

Mouse.Click(uIVisualStudioBingWindClient, new Point(1427, 16));


Change the highlighted line as follows:

uIEnteryoursearchtermEdit.Text = testData;

 Now we are ready to run our test with different testData values as supplied from the database ‘CodedUI’ for the ‘BingSearch’ table. Run your test case, the test will run as many times the data present in table.

See the result of the sample run below. The test run for two iterations as we have provided two values in the ‘BingSearch’ table.



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 Databases as the source of TestData.

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: Pramodkmenon on: 6/24/2011 | Points: 25
Halo Ruchi,

Thanks a lot for sharing information. I have tried Data Driven method in Coded UI. I am still in the learning process. I successfully connected to my DB and the first field (User Name) is reading info from the DB column i mentioned correctly, but the second field which is a password field is throwing error. Part of the error message says "The following error occurred while attempting to decode encrypted test:Bad Data."

Could you suggest a work around for this? Is there a way to pull data from encrypted (password) fields ? I need your valuable advice to go further. Waiting for your response.
Thank you very much.

Login to post response

Comment using Facebook(Author doesn't get notification)