The NUnit framework has been widely used by the Selenium WebDriver community to create test scripts with .NET bindings.
Similar to the JUnit framework, the NUnit framework also supports data-driven testing in the simplest manner. In this recipe, we will create a Selenium WebDriver test using NUnit. We will read the test data from an XML file used in the first recipe.
To begin, follow these steps:
<testdata> <vars height="160" weight="45" bmi="17.6" bmi_category="Underweight" /> <vars height="168" weight="70" bmi="24.8" bmi_category="Normal" /> <vars height="181" weight="89" bmi="27.2" bmi_category="Overweight" /> <vars height="178" weight="100" bmi="31.6" bmi_category="Obesity" /> </testdata>
BMICalculator
System.Xml.Linq
Let's create a parameterized test in NUnit. Create a new C# class item with the name BMICalculatorNUnitTest
, as shown in the following code example, copy the following code to the newly created class by replacing its contents:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using System.Collections; using System.Xml.Linq; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Support; using OpenQA.Selenium.Support.UI; namespace BMICalculator { [TestFixture] public class BMICalculatorNUnitTest { IWebDriver driver; [SetUp] public void TestSetup() { // Create a instance of the Firefox driver using IWebDriver Interface driver = new FirefoxDriver(); } [TestCaseSource("BmiTestData")] public void TestBmiCalculator(string height, string weight, string expectedBmi, string expectedCategory) { driver.Navigate().GoToUrl("http://cookbook.seleniumacademy.com/bmicalculator.html"); IWebElement heightElement = driver.FindElement(By.Name("heightCMS")); heightElement.SendKeys(height); IWebElement weightElement = driver.FindElement(By.Name("weightKg")); weightElement.SendKeys(weight); IWebElement calculateButton = driver.FindElement(By.Id("Calculate")); calculateButton.Click(); IWebElement bmiElement = driver.FindElement(By.Name("bmi")); Assert.AreEqual(expectedBmi, bmiElement.GetAttribute("value")); IWebElement bmiCatElement = driver.FindElement(By.Name("bmi_category")); Assert.AreEqual(expectedCategory, bmiCatElement.GetAttribute("value")); } [TearDown] public void TestCleanUp() { // Close the browser driver.Quit(); } private IEnumerable BmiTestData { get { return GetBmiTestData(); } } private IEnumerable GetBmiTestData() { var doc = XDocument.Load(@"c:data.xml"); return from vars in doc.Descendants("vars") let height = vars.Attribute("height").Value let weight = vars.Attribute("weight").Value let expectedBmi = vars.Attribute("bmi").Value let expectedCategory = vars.Attribute("bmi_category").Value select new object[] { height, weight, expectedBmi, expectedCategory }; } } }
While creating a data-driven test in NUnit, we use the TestCaseSource
attribute. We will specify the name of the IEnumerable
property that will provide test data to this test case with the TestCaseSource
attribute, as shown in the following code:
[TestCaseSource("BmiTestData")] public void TestBmiCalculator(string height, string weight, string expected_bmi, string expected_category)
When we execute the test, NUnit framework will generate test cases by calling the BmiTestData
property. This will return an array of arguments as IEnumerable
by calling the GetBmiTestData()
method. An array of arguments is created by reading an XML file using a LINQ Query in the GetBmiTestData()
method. When we open the test in the NUnit GUI, it shows the test cases for all the test data combinations provided in the input XML file, as shown in the following screenshot:
With this, we can get the test data from any source, such as CSV, Excel spreadsheet, or a database. The NUnit GUI will show the results when all the tests are executed, as shown in the following screenshot:
3.145.36.221