In some situations, we might need to execute the scenario with different data; at those times, we cannot write a test case for each data.
public Object[][] searchGoogle()import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderClass
{
WebDriver driver=new FirefoxDriver();
@DataProvider(name="searchData")
public Object[][] searchData()
{
Object[][] data=new Object[3][1];
data[0][0]="https://google.com";
data[1][0]="https://yahoo.com";
data[2][0]="https://bing.com";
return data;
}
@Test(dataProvider="searchData")
public void doSearch(String site)
{
driver.get(site);
}
}
But in most of the case, we do not require a single argument from the table, the below example shows how to get the two arguments from the table.
In the below program, we are getting the username and password to enter into Gmail.
We can get any number of arguments.
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderClass
{
WebDriver driver=new FirefoxDriver();
@DataProvider(name="searchData")
public Object[][] searchData()
{
Object[][] data=new Object[3][2];
data[0][0]="userkumar";
data[0][1]="userkumar's password";
data[1][0]="vikaskumar";
data[1][1]="vikaskumar's password";
data[2][0]="admin";
data[2][1]="admin's password";
return data;
}
@Test(dataProvider="searchData")
public void doSearch(String username,String password) throws InterruptedException
{
//opens the gmail.com
driver.get("https://gmail.com");
//enter username from table data
driver.findElement(By.id("Email")).sendKeys(username);
//enter password from table
driver.findElement(By.id("Passwd")).sendKeys(password);
//submit the form
driver.findElement(By.id("Passwd")).submit();
}
}
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.