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.
The following are the steps we need to begin with:
pom.xml
file:<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
<scope>test</scope>
</dependency>
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(); } }
Unlike the JUnit, where parameterization is done on a class level, TestNG supports parameterization at the test level.
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.
3.139.105.159