To create a data-driven test in MSTEST, the unit testing framework provided by Microsoft Visual Studio is the simplest way to parameterize the test scripts with .NET bindings.
MSTEST has in-built features to support data-driven testing, which can be configured very easily. In this recipe, we will use MSTEST to create a data-driven Selenium test by reading test data from an Excel spreadsheet.
To begin, follow these steps:
BMICalculator
You can parameterize a test in MSTEST by adding the Excel spreadsheet to deployment items of the test project, using the following steps:
BMICalculatorTests
. Copy the following code to this class:using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Support; using OpenQA.Selenium.Support.UI; using System.Data; namespace BMICalculator { [TestClass] public class BMICalculatorTests { IWebDriver driver; [TestInitialize] public void TestSetup() { // Create a instance of the Firefox driver using IWebDriver Interface driver = new FirefoxDriver(); } private TestContext testContextInstance; /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } [TestMethod] [DeploymentItem("Data.xls")] [DataSource("System.Data.OleDb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data.xls;Persist Security Info=False;Extended Properties='Excel 12.0;HDR=Yes'", "Data$", DataAccessMethod.Sequential)] public void TestBMICalculator() { driver.Navigate().GoToUrl("http://dl.dropbox.com/u/55228056/bmicalculator.html"); IWebElement height = driver.FindElement(By.Name("heightCMS")); height.SendKeys(TestContext.DataRow["Height"].ToString()); IWebElement weight = driver.FindElement(By.Name("weightKg")); weight.SendKeys(TestContext.DataRow["Weight"].ToString()); IWebElement calculateButton = driver.FindElement(By.Id("Calculate")); calculateButton.Click(); IWebElement bmi = driver.FindElement(By.Name("bmi")); Assert.AreEqual(TestContext.DataRow["Bmi"].ToString(), bmi.GetAttribute("value")); IWebElement bmiCategory = driver.FindElement(By.Name("bmi_category")); Assert.AreEqual(TestContext.DataRow["Category"].ToString(), bmiCategory.GetAttribute("value")); } [TestCleanup] public void TestCleanUp() { // Close the browser driver.Quit(); } } }
When we add the DataSource
attribute to a test in MSTEST, it provides data source-specific information for data-driven testing to the framework:
[TestMethod] [DeploymentItem("Data.xls")] [DataSource("System.Data.OleDb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data.xls;Persist Security Info=False;Extended Properties='Excel 12.0;HDR=Yes'", "Data$", DataAccessMethod.Sequential)] public void TestBMICalculator()
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:
IWebElement height = driver.FindElement(By.Name("heightCMS")); height.SendKeys(TestContext.DataRow["Height"].ToString());
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.
13.58.51.36