import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ParamTestWithDataProvider1 { private PrimeNumberChecker primeNumberChecker; @BeforeMethod public void initialize() { primeNumberChecker = new PrimeNumberChecker(); } @DataProvider(name = "test1") public static Object[][] primeNumbers() { return new Object[][] { { 2, true }, { 6, false }, { 19, true }, { 22, false }, { 23, true } }; } // This test will run 4 times since we have 5 parameters defined @Test(dataProvider = "test1") public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) { System.out.println(inputNumber + " " + expectedResult); Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber)); } }
Monday, February 10, 2014
TestNG Parameters with Dataproviders
TestNG dependsOnMethods example
Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.
TestNG allows you to specify dependencies either with:
- Using attributes dependsOnMethods in @Test annotations OR
- Using attributes dependsOnGroups in @Test annotations.
public class MessageUtil { private String message; // Constructor // @param message to be printed public MessageUtil(String message) { this.message = message; } // prints the message public String printMessage() { System.out.println(message); return message; } // add "Hi!" to the message public String salutationMessage() { message = "Hi!" + message; System.out.println(message); return message; } }
Parameterization through testNG for selenium WebDriver
import java.util.Arrays;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParameterizationExp {
static WebDriver driver;
@DataProvider
public Object[][] getData(){
Xls_Reader Data = new Xls_Reader("D://WebDriverTest//JunitProject//src//Data.xlsx");
int rowCount = Data.getRowCount("Data");
//System.out.println(rowCount);
int colCount = Data.getColumnCount("Data");
Object sampleData[][] = new Object[rowCount-1][colCount];
for(int i=2;i<=rowCount;i++) {
for(int j=0; j<colCount;j++) {
sampleData[i-2][j] = Data.getCellData("Data", j, i);
}
}
return sampleData;
}
@BeforeClass
public static void initBrowser(){
driver = new FirefoxDriver();
}
@Test(dataProvider="getData")
public void SubmitForm(String name, String question, String color, String comment) {
driver.navigate().to("http://google.com");
driver.findElement(By.xpath("html/body/form/input")).sendKeys(name);
driver.findElement(By.xpath("html/body/form/p[1]/input")).sendKeys(question);
driver.findElement(By.xpath("html/body/form/p[2]/select")).sendKeys(color);
driver.findElement(By.xpath("html/body/form/p[4]/textarea")).sendKeys(comment);
driver.findElement(By.xpath("html/body/form/p[5]/input")).click();
}
@AfterClass
public void closeBroser() {
driver.quit();
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParameterizationExp {
static WebDriver driver;
@DataProvider
public Object[][] getData(){
Xls_Reader Data = new Xls_Reader("D://WebDriverTest//JunitProject//src//Data.xlsx");
int rowCount = Data.getRowCount("Data");
//System.out.println(rowCount);
int colCount = Data.getColumnCount("Data");
Object sampleData[][] = new Object[rowCount-1][colCount];
for(int i=2;i<=rowCount;i++) {
for(int j=0; j<colCount;j++) {
sampleData[i-2][j] = Data.getCellData("Data", j, i);
}
}
return sampleData;
}
@BeforeClass
public static void initBrowser(){
driver = new FirefoxDriver();
}
@Test(dataProvider="getData")
public void SubmitForm(String name, String question, String color, String comment) {
driver.navigate().to("http://google.com");
driver.findElement(By.xpath("html/body/form/input")).sendKeys(name);
driver.findElement(By.xpath("html/body/form/p[1]/input")).sendKeys(question);
driver.findElement(By.xpath("html/body/form/p[2]/select")).sendKeys(color);
driver.findElement(By.xpath("html/body/form/p[4]/textarea")).sendKeys(comment);
driver.findElement(By.xpath("html/body/form/p[5]/input")).click();
}
@AfterClass
public void closeBroser() {
driver.quit();
}
}
Selenium WebDriver with chrome issue(org.openqa.selenium.remote.UnreachableBrowserException) solution
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_40'
Driver info: driver.version: ChromeDriver
if you are getting above problem, go to the chromedriver.exe location and try to execute the exe. if you are able to execute the exe then below code will work..othere wise it will be permission issue to the chromedriver folder. chnage the folder location or provide the permission to the folder and double click on chromedriver.exe.
Solution:-
System.setProperty("webdriver.chrome.driver", "C:/Driver/chromedriver.exe");
System.out.println(System.getProperty("webdriver.chrome.driver"));
WebDriver driver3 = new ChromeDriver();
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_40'
Driver info: driver.version: ChromeDriver
if you are getting above problem, go to the chromedriver.exe location and try to execute the exe. if you are able to execute the exe then below code will work..othere wise it will be permission issue to the chromedriver folder. chnage the folder location or provide the permission to the folder and double click on chromedriver.exe.
Solution:-
System.setProperty("webdriver.chrome.driver", "C:/Driver/chromedriver.exe");
System.out.println(System.getProperty("webdriver.chrome.driver"));
WebDriver driver3 = new ChromeDriver();
Subscribe to:
Comments (Atom)