Data driven approach in Coded UI Test - Part 2

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

In my previous article on data driven approach in coded UI, we have discussed about how to use the CSV files as the dataSource.
In this article we will look into Data Driven approach in Coded UI using XML files 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 XML file as the datasource

1.       Select the XML File as the datasource and click on the ‘Next’ button.

 

2.       Choose the XML file from the next screen.


You will see the data present in XML file as below:


3.       Clicking Next button will show up a screen with different data tables.


 
If you have a look at the xml file which I used as the source of test data, it contains two different data tables named ‘Search’ and ‘Results’ as shown below:

 


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

 
5.       Click on ‘Yes’ button and it will copy the XML file in the current project.


 

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

6.       Open the CodedUITestMethod1 and notice the attribute on top of the method:

 


[DeploymentItem("SampleCodedUIProject\\XMLFile1.xml"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\XMLFile1.xml", "Search", DataAccessMethod.Sequential), TestMethod]

public void CodedUITestMethod1()

{

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

this.UIMap.RecordedMethod1(testData);

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

}

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

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



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

{

#region 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;

#endregion

// Go to web page 'http://www.bing.com/' 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));

} 

Change the highlighted line as follows:

uIEnteryoursearchtermEdit.Text = testData;
 
9.       Now we are ready to run our test with different testData values as supplied from the XML file for the ‘Search’ table.  Run your test case, the test will run as many times the data present in XML file.
See the result of the sample run below. The test run for two iterations as we have provided two values in the XML file for the ‘Search’ 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 XML files as the source of TestData. In next articles we will talk about Databases in detail.
Page copy protected against web site content infringement by Copyscape

About the Author

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

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Karthikanbarasan on: 4/7/2011 | Points: 25
Good article
Posted by: gauravkejriwal20-16338 on: 6/19/2012 | Points: 25
Hi Ruchi,

Is their any way i can change the tag to be chosen based on some condition. I am using xml file as a data source to my tests.I have say two tags in the xml file,my xml somehow luks like below:

Data.xml file luks like below:

<GraphicalTopUp>
<!--MOVISTAR Spain Prepaid-->
<TOP_TC_449S>
<Carrier>MOVISTAR Spain Prepaid</Carrier>
<Amount>5 €</Amount>
<PhoneNo>649818912</PhoneNo>
</TOP_TC_449S>

<!--VODAFONE Spain Prepaid-->

<TOP_TC_449>
<Carrier>VODAFONE Spain Prepaid</Carrier>
<Amount>15 €</Amount>
<PhoneNo>610654940</PhoneNo>
</TOP_TC_449>

</GraphicalTopUp>

One testmethod is binded with this data file as below:

[DeploymentItem("FxOnline_TopUp\\DataSheet\\Data.xml"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\Data.xml", "TOP_TC_449", DataAccessMethod.Sequential), TestMethod]
public void TOP_TC_449()
{

//Now inside my test method their are two coditions,based on which the dataset of particular tag shold be picked.

if(codition 1)

{

we can use dataset under tag TOP_TC_449S to pass as a testcontext

}

if (condition 2)

{

we can use dataset under tag TOP_TC_449 to pass as a testcontext

}

}

Please help

Login to post response

Comment using Facebook(Author doesn't get notification)