DataDriven Testing Using DataProvider
Testing with multiple sets of data is called data driven Testing.If you have to login with multiple username and password then we have to perform data driven testing.By using DataProvider annotation provided by TestNG we can perform Data Driven Testing.
DataProvider is one such feature in testng; it allows a test method to be executed with multiple sets of data.
DataProvider methods return two dimension object array.
@DataProvider
public Object[][] getData(){
Object data[][] = new Object [2][2];
//We have just initilize the data and this new Object [3][2]; means 3 rows two colouns.
data[0][0] = "Username1";
//data at 0 row and 0 colomn.
data[0][1] = "password1";
data at 0 row and 1 colomn.
data[1][0] = "username2";
data at 1 row and 0 colomn.
data[1][1] = "password2";
data at 0 row and 1 colomn.
data[2][0] = "username2";
data at 2 row and 0 colomn.
data[2][1] = "password2";
data at 2 row and 1 colomn.
return data;
}
By using these code we can perform data driven Testing.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class datadriven {
@Test(dataProvider = "getData")
public void loginTest(String username,String password) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("https://gmail.com");
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys(username);
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='next']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(password);
driver.findElement(By.xpath("//*[@id='signIn']")).click();
Thread.sleep(5000);
}
@DataProvider
public Object[][] getData(){
Object data[][] = new Object [3][2];
data[0][0] = "Username1";
data[0][1] = "password1";
data[1][0] = "username2";
data[1][1] = "password2";
data[2][0] = "username3";
data[2][1] = "password3";
return data;
}
}



