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.
Introduction
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 http://go.microsoft.com/fwlink/?LinkId=179463
string testData = TestContext.DataRow["TestData"].ToString();
this.UIMap.RecordedMethod1(testData);
}
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;
#endregion
// Go to web page about:blank' using new browser instance
this.UIBlankPageWindowsInteWindow.LaunchUrl(new System.Uri(this.RecordedMethod1Params.UIBlankPageWindowsInteWindowUrl));
// Type 'www.bing.com/' in 'Address and search using Bing' text box
uIAddressandsearchusinEdit.Text =
his.RecordedMethod1Params.UIAddressandsearchusinEditText;
// Click 'Go to "www.bing.com/" (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.

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