July 28, 2022

Using XML File as Data Source for Visual Studio + Selenium Data Driven Testing

  July 28, 2022

 Data Driven Testing is a built-in Functionality of MsTest. 

We can use Microsoft.VisualStudio.TestTools.DataSource reference to implement this.

Here we are taking an example of XML file as the test data source. We can add this file in a Testdata folder in the solution itself, so maintenance of test data will be easy.

In order to refer to this test data source, we can use the below method format.

Using TestContext we can iterate through each row of test data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[TestMethod]
[TestCategory("GetSetAutomate")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", @"GetSetAutomate\Test Data Files\GetSetAutomateDemo.xml", "TestData", DataAccessMethod.Sequential)]
public void GetSetAutomateTestDrivenDemo()
{
	driver.FindElement("<By.Locator>").SendKeys(TestContext.DataRow[0].ToString());
	driver.FindElement("<By.Locator>").SendKeys(TestContext.DataRow[1].ToString());
	driver.FindElement("<By.Locator>").SendKeys(TestContext.DataRow[2].ToString());

}

Here as part of the test method, we need to mention the path of XML Test Data file and the tag of the test data in XML also should be mentioned.

A Sample XML Test source file is given below

<?xml version="1.0" encoding="utf-8" ?>
<MyData>
  <Testdata>
    <EMAILID>test1@123.com</EMAILID>
    <FIRSTNAME>First1</FIRSTNAME>
    <LASTNAME>last1</LASTNAME>
  </Testdata>
  
  <Testdata>
    <EMAILID>test2@123.com</EMAILID>
    <FIRSTNAME>First2</FIRSTNAME>
    <LASTNAME>last2</LASTNAME>
  </Testdata>
</MyData>

In the above example, each <testdata> tag will be considered as a different set of data and you can see that many test runs when we execute the test script.

logoblog

Thanks for reading Using XML File as Data Source for Visual Studio + Selenium Data Driven Testing

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Steps to get Tricentis Tosca Trial license activated (60 Days)

TOSCA is an automation tool that can do functional and regression testing for software products. It also includes GUI, command line interfac...