top of page
  • Writer's picture9to5 Technologies

How to achieve parameterization using @DataProvider TestNG.

Updated: Dec 9, 2019

Based on the testing requirement you many need to repeat the test with multiple data sets. In this post I will explain how to achieve this (parameterization) using TestNG.

There are two ways of sending parameters to the selenium test

  1. Using testng.xml (Explained)

  2. Programmatically with array of objects.

Please copy and change code accordingly:


package Testng_Package;

import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;

public class Sample_Login {

WebDriver driver = new FirefoxDriver();

@BeforeTest public void setup() throws Exception { driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.get(“paste url”); }

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

@DataProvider public Object[][] LoginCredentials() { //Created two dimensional array with 4 rows and 2 columns. //4 rows represents test has to run 4 times. //2 columns represents 2 data parameters. Object[][] drop = new Object[4][2];

drop [0][0] = “UserId1”; drop [0][1] = “Pass1”;

drop [1][0] = “UserId2”; drop [1][1] = “Pass2”;

drop [2][0] = “UserId3”; drop [2][1] = “Pass3”;

drop [3][0] = “UserId4”; drop [3][1] = “Pass4″; return drop; }

@Test(dataProvider=”Credentials”) public void LogIn_Test(String Userid, String Password) { driver.findElement(By.xpath(“//input[@name=’Userid’]”)).clear(); driver.findElement(By.xpath(“//input[@name=’Password’]”)).clear(); driver.findElement(By.xpath(“//input[@name=’Userid’]”)).sendKeys(Userid); driver.findElement(By.xpath(“//input[@name=’Password’]”)).sendKeys(Password); driver.findElement(By.xpath(“//input[@value=’Login’]”)).click(); String alrt = driver.switchTo().alert().getText(); driver.switchTo().alert().accept(); System.out.println(alrt); } }

0 views0 comments
bottom of page