Exaplain about '@DataProvider'?
Ans: A DataProvider is data feeder method defined in your class that supplies a test method with data .
The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method.
Example Script:
@DataProvider(name = "DP1")
public Object[][] createData() {
Object[][] retObjArr={{"001","Jack","London"},
{"002","John","New York"},
{"003","Mary","Miami"},
{"004","George","california"}};
return(retObjArr);
}
@Test (dataProvider = "DP1")
public void testEmployeeData(String empid, String empName, String city){
selenium.type("id", empid);
selenium.type("name", empName);
selenium.click("submit_button");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent(city));
}
++++++++Sample code using @DataProvider+++++
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Data_Provider {
private WebDriver driver;
private String baseUrl;
// String baseUrl, nodeUrl;
@DataProvider(name = "myTest")
public Object[][] createData1() {
return new Object[][] {
{ "India"},
{ "Brazil"},
{ "Canada"},
{ "Sri Lanka"},
{ "England"},
{ "UK"},
{ "United States"},
};
}
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider = "myTest")
public void testSearchCountry(String country) throws Exception {
driver.get(baseUrl + "/wiki/Main_Page");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys(country);
driver.findElement(By.id("searchButton")).click();
String str = driver.findElement(By.cssSelector("span")).getText();
Assert.assertTrue(country.equals(str.trim()));
System.out.println(str);
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
++++++++Sample code using @DataProvider+++++
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Data_Provider {
private WebDriver driver;
private String baseUrl;
// String baseUrl, nodeUrl;
@DataProvider(name = "myTest")
public Object[][] createData1() {
return new Object[][] {
{ "India"},
{ "Brazil"},
{ "Canada"},
{ "Sri Lanka"},
{ "England"},
{ "UK"},
{ "United States"},
};
}
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider = "myTest")
public void testSearchCountry(String country) throws Exception {
driver.get(baseUrl + "/wiki/Main_Page");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys(country);
driver.findElement(By.id("searchButton")).click();
String str = driver.findElement(By.cssSelector("span")).getText();
Assert.assertTrue(country.equals(str.trim()));
System.out.println(str);
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
No comments:
Post a Comment