Data driven tests using MSTest & Selenium

There are a lot of literature including the ones on Microsoft KB’s which talks about how we can make our tests data driven. These employ the use of additional app.config file. After many headaches, I have figured out a way to accomplish the same without the use of app.config.

Getting ready

1. Create a new C# unit test project in Microsoft Visual Studio 2012 and name it SeleniumDataDrivenTest

2. Add a reference to the WebDriver .NET binding

3. Create a excel sheet from where you want to read the data from. Let’s call that data sheet as Data.xlsx

You can parameterize a test in MSTEST by adding the Excel spreadsheet to deployment items of the test project, using the following steps:

1. Click on solution and choose to add a Test Setting and continue as shown in the following screenshot:

image

image

2. The Test Settings dialog will appear. We need to add the test data file by clicking the Add File button in the Deployment section, as shown in the following screenshot:

image

3. Once you add the file to the Deployment section, it will appear in the list, as shown in the following screenshot: Let's ensure that we create an excel sheet from where you want to read the data and call it as as Data.xlsx.

image

image

4. In the solution explorer, use the Show all files and your file "Data.xlsx" should now show up. Once it does , right click on it and choose include in project. Your solution show now resemble something like the screenshot below

image

5. Now right click on the "Data.xlsx" file and choose to modify it's properties. Set the Build Action to Content and Copy to output directory as either to "Copy always" or "Copy if newer". I always tend to leave it as "Copy always"

image

6. Now copy the below code to the class

How it works :

When we add the DataSource attribute to a test in MSTEST, it provides data source-specific information for data-driven testing to the framework.

image

It reads the test data from the source. In this example, the source is an Excel spreadsheet. The framework internally creates a DataTable object to store the values from the source.The TestContext test method provides a collection of data rows for parameterization. We can access a field by specifying its name, as follows:

image
With the DataSource attribute, we can specify the connection string or a configuration file to read data from a variety of sources including CSV File, Excel spreadsheets, XML files,or databases.

author

Vinay Jagtap

A hard core Technocrat with over a decade of extensive experience in heading complex test projects coupled with real time experience of project management and thought leadership. Extensive experience in Performance, Security and Automation Testing and development of automation frameworks and ability to setup and execute Global service centers and Center of Excellences for testing.

Get Free Email Updates to your Inbox!

www.CodeNirvana.in

Powered by Blogger.

Translate

Total Pageviews

Copyright © T R I A G E D T E S T E R