17
Jul

Creating a datadriven tests with TestNG

Following function can be used to create a data driven tests using “Dataprovider” annotation of TestNG

import java.util.HashMap;
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.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {
private static WebDriver driver;

@DataProvider(name = “Authentication”)

public static HashMap credentials() {

HashMap data = readcsvdatafileasCSV(“path to file”);
return data;

}

// Here we are calling the Data Provider object with its Name

@Test(dataProvider = “Authentication”)

public void test(HashMap map) {

driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(“http://www.store.demoqa.com”);

driver.findElement(By.xpath(“.//*[@id=’account’]/a”)).click();

driver.findElement(By.id(“log”)).sendKeys(map.get(“UserName”));

driver.findElement(By.id(“pwd”)).sendKeys(map.get(“Password”));

driver.findElement(By.id(“login”)).click();

driver.findElement(By.xpath(“.//*[@id=’account_logout’]/a”)).click();

driver.quit();

}

}