Creating a data-driven test using TestNG

TestNG is another widely used testing framework with Selenium WebDriver. It is very similar to JUnit. TestNG has rich features for testing, such as parameterization, parallel test execution, and so on.

TestNG provides the DataProvider feature to create data-driven tests. In this recipe, we will use the DataProvider feature to create a simple test. Creating data-driven tests in TestNG is fairly easy when compared with JUnit.

Getting ready

The following are the steps we need to begin with:

  • Add TestNG dependency to the Maven pom.xml file:
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.4</version>
      <scope>test</scope>
    </dependency>
    
  • Identify the set of values that we need to test.

How to do it...

Let's create a new test and parameterize a TestNG test using the following steps:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import org.testng.annotations.*;
import static org.testng.Assert.*;

public class TestNGDDT {

  private WebDriver driver;

  @DataProvider
  public Object[][] testData() {
    return new Object[][] {
        new Object[] { "160", "45", "17.6", "Underweight" },
        new Object[] { "168", "70", "24.8", "Normal" },
        new Object[] { "181", "89", "27.2", "Overweight" },
        new Object[] { "178", "100", "31.6", "Obesity" }, };
  }

  @BeforeTest
  public void setUp() {
    // Create a new instance of the Firefox driver
    driver = new FirefoxDriver();
    driver.get("http://cookbook.seleniumacademy.com/bmicalculator.html");

  }

  @Test(dataProvider = "testData")
  public void testBMICalculator(String height, String weight, String bmi,
      String category) {
    WebElement heightField = driver.findElement(By.name("heightCMS"));
    heightField.clear();
    heightField.sendKeys(height);

    WebElement weightField = driver.findElement(By.name("weightKg"));
    weightField.clear();
    weightField.sendKeys(weight);

    WebElement calculateButton = driver.findElement(By.id("Calculate"));
    calculateButton.click();

    WebElement bmiLabel = driver.findElement(By.name("bmi"));
    assertEquals(bmiLabel.getAttribute("value"), bmi);

    WebElement bmiCategoryLabel = driver.findElement(By.name("bmi_category"));
    assertEquals(bmiCategoryLabel.getAttribute("value"), category);
  }

  @AfterTest
  public void tearDown() {
    driver.quit();
  }
}

How it works...

Unlike the JUnit, where parameterization is done on a class level, TestNG supports parameterization at the test level.

Tip

In TestNG, we do not need a constructor and instance variable for the test case class to pass the parameter values. TestNG does the mapping automatically.

When a method is annotated with @DataProvider, it becomes a data feeder method by passing the test data to the test case. In this example, the testData() method will become the data feeder method, and TestNG will pass the array of data rows to the test method one by one:

@DataProvider
public Object[][] testData() {
  return new Object[][] {
    new Object[] {"160","45","17.6","Underweight"},
    new Object[] {"168","70","24.8","Normal"},
    new Object[] {"181","89","27.2","Overweight"},
    new Object[] {"178","100","31.6","Obesity"},
  };
}

The test case method is linked to the data feeder method by passing the name of the dataProvider method to the @Test annotation:

@Test(dataProvider = "testData")
public void testBMICalculator(String height, String weight, String bmi, String category)

TestNG will execute the test four times with different test combinations. TestNG also generates a well-formatted report at the end of the test execution.

There's more...

To run Selenium tests in parallel, the TestNG parameterization feature comes in very handy. TestNG supports running Selenium tests parallel in a multithreading safe environment.

See also

  • The Reading test data from a CSV file using JUnit recipe
  • The Reading test data from an Excel file using JUnit and Apache POI recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.105.159