Skip to content

Instantly share code, notes, and snippets.

@yaromir
Created December 9, 2012 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaromir/4246674 to your computer and use it in GitHub Desktop.
Save yaromir/4246674 to your computer and use it in GitHub Desktop.
TestNG Run all class methods multiple times preliminary doing @BeforeClass with supplied data
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Encapsulates tests for Calendar module.
*
* Dependent properties:
* * siteURL: URL of the webapp
*/
@Test(dataProvider="getUsersIdAndName")
public class CalendarTest {
static private WebDriver driver;
static private Properties props;
@BeforeClass
public static void setUpClass() throws Exception {
// Loading properties
props = new Properties();
try {
// Load a properties file
props.load(new FileInputStream("config.properties"));
} catch (IOException ex) {
// ToDo Implement proper logging
System.err.println(ex.getMessage());
}
// Initializing webdriver
driver = new FirefoxDriver();
}
@AfterClass
public static void tearDownClass() throws Exception {
// Close the browser
driver.quit();
}
/**
* Data provider method that supplies IDs and names of all users in the system.
*
* @return array of int (ID) and String (userName)
*/
@DataProvider
public static Object[][] getUsersIdAndName() {
// ToDo Replace with exctraction from DB
return new Object[][] {
{1, "admin"},
{5, "user"}
};
}
/**
* Tests that authentication with given credentials succeeded.
*
* Dependent properties:
* * siteURL: URL of the webapp
*
* @param userID user ID in database
* @param userName login of the user
*/
@Test
public static void isAuthenticated(int userID, String userName) {
// Logging in
driver.get( props.getProperty("siteURL") );
WebElement loginField = driver.findElement(By.name("user_name"));
WebElement passwordField = driver.findElement(By.name("user_password"));
loginField.sendKeys( userName );
// For testing purposes all passwords match logins
passwordField.sendKeys( userName );
loginField.submit();
/*
* Checking that we've authenticated successfully.
* There is a cookie holding the ID of authenticated user.
*/
Cookie loginID = driver.manage().getCookieNamed("ck_login_id");
Assert.assertEquals(Integer.parseInt( loginID.getValue() ), userID);
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Encapsulates tests for Calendar module.
*
* Dependent properties:
* * siteURL: URL of the webapp
*/
@Test(dataProvider="getUsersIdAndName")
public class CalendarTest {
static private WebDriver driver;
static private Properties props;
@BeforeClass
public static void setUpClass() throws Exception {
// Loading properties
props = new Properties();
try {
// Load a properties file
props.load(new FileInputStream("config.properties"));
} catch (IOException ex) {
// ToDo Implement proper logging
System.err.println(ex.getMessage());
}
// Initializing webdriver
driver = new FirefoxDriver();
}
@AfterClass
public static void tearDownClass() throws Exception {
// Close the browser
driver.quit();
}
/**
* Data provider method that supplies IDs and names of all users in the system.
*
* @return array of int (ID) and String (userName)
*/
@DataProvider
public static Object[][] getUsersIdAndName() {
// ToDo Replace with exctraction from DB
return new Object[][] {
{1, "admin"},
{5, "user"}
};
}
/**
* Tests that authentication with given credentials succeeded.
*
* Dependent properties:
* * siteURL: URL of the webapp
*
* @param userID user ID in database
* @param userName login of the user
*/
@Test(dataProvider="getUsersIdAndName")
public static void isAuthenticated(int userID, String userName) {
// Logging in
driver.get( props.getProperty("siteURL") );
WebElement loginField = driver.findElement(By.name("user_name"));
WebElement passwordField = driver.findElement(By.name("user_password"));
loginField.sendKeys( userName );
// For testing purposes all passwords match logins
passwordField.sendKeys( userName );
loginField.submit();
/*
* Checking that we've authenticated successfully.
* There is a cookie holding the ID of authenticated user.
*/
Cookie loginID = driver.manage().getCookieNamed("ck_login_id");
Assert.assertEquals(Integer.parseInt( loginID.getValue() ), userID);
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Encapsulates tests for Calendar module.
*
* Dependent properties:
* * siteURL: URL of the webapp
*/
@Test(dataProvider="getUsersIdAndName")
public class CalendarTest {
static private WebDriver driver;
static private Properties props;
/**
* Tests that authentication with given credentials succeeded.
*
* Dependent properties:
* * siteURL: URL of the webapp
*
* @param userID user ID in database
* @param userName login of the user
*/
@BeforeClass
@Test(dataProvider="getUsersIdAndName")
public static void isAuthenticated(int userID, String userName) throws Exception {
// Loading properties
props = new Properties();
try {
// Load a properties file
props.load(new FileInputStream("config.properties"));
} catch (IOException ex) {
// ToDo Implement proper logging
System.err.println(ex.getMessage());
}
// Initializing webdriver
driver = new FirefoxDriver();
// Logging in
driver.get( props.getProperty("siteURL") );
WebElement loginField = driver.findElement(By.name("user_name"));
WebElement passwordField = driver.findElement(By.name("user_password"));
loginField.sendKeys( userName );
// For testing purposes all passwords match logins
passwordField.sendKeys( userName );
loginField.submit();
/*
* Checking that we've authenticated successfully.
* There is a cookie holding the ID of authenticated user.
*/
Cookie loginID = driver.manage().getCookieNamed("ck_login_id");
Assert.assertEquals(Integer.parseInt( loginID.getValue() ), userID);
}
@AfterClass
public static void tearDownClass() throws Exception {
// Close the browser
driver.quit();
}
/**
* Data provider method that supplies IDs and names of all users in the system.
*
* @return array of int (ID) and String (userName)
*/
@DataProvider
public Object[][] getUsersIdAndName() {
// ToDo Replace with exctraction from DB
return new Object[][] {
{1, "admin"},
{5, "user"}
};
}
}
@yaromir
Copy link
Author

yaromir commented Dec 11, 2012

Final solution is here:
https://gist.github.com/4262704

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment