Skip to content

Instantly share code, notes, and snippets.

@yaromir
Created December 11, 2012 22:04
Show Gist options
  • Save yaromir/4262704 to your computer and use it in GitHub Desktop.
Save yaromir/4262704 to your computer and use it in GitHub Desktop.
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.Factory;
import org.testng.annotations.Test;
/**
* Encapsulates tests for Calendar module.
*
* Dependent properties:
* * siteURL: URL of the webapp
*/
public class CalendarTest {
static private Properties props;
private WebDriver driver;
private int userID; // ID of the authenticated user
private String loginName; // User login
/**
* Sets up web driver and logs in with credentials being held in class fields.
* Thus for the purity of experiment
* each time test activities are repeated for another user
* new clean web driver is created.
*
* Dependent properties:
* config.properties
* * siteURL
*/
@BeforeClass
public void setUpClass() {
/*
* Initializing webdriver.
* Haven't done this in constructor because
* it's more graceful to set it up here and to tear it down in @AfterClass.
*/
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(loginName);
// For testing purposes all passwords match logins
passwordField.sendKeys(loginName);
loginField.submit();
}
/**
* Tears down web driver.
* Thus for the purity of experiment
* each time test activities are repeated for another user
* new clean web driver is created.
*/
@AfterClass
public void tearDownClass() {
// Close the browser
driver.quit();
}
/**
* Creates object of the test class with specified user credentials.
* All tests are performed on behalf of the specified user.
*
* @param userID user ID in database
* @param loginName user login
*/
@Factory(dataProvider = "getUsersIdAndLogin")
public CalendarTest(int userID, String loginName) {
// Loading properties
props = new Properties();
try {
// Load a properties file
props.load(new FileInputStream("config.properties"));
} catch (IOException ex) {
// ToDo Implement logging
System.err.println(ex.getMessage());
}
// Test is performed on behalf of the user with specified credentials
this.userID = userID;
this.loginName = loginName;
// Log in itself takes place in @BeforeClass
}
/**
* Data provider method that supplies database IDs and login names of all users in the system.
*
* @return array of int (user ID) and String (login name of user)
*/
@DataProvider
public static Object[][] getUsersIdAndLogin() {
// ToDo Replace with exctraction from DB
return new Object[][] {
{1, "admin"},
{5, "user"}
};
}
/**
* Tests that authentication with given credentials succeeded.
*
* @param userID user ID in database
* @param userName login of the user
*/
@Test
public void isAuthenticated() {
/*
* 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);
}
}
@yaromir
Copy link
Author

yaromir commented Dec 11, 2012

Solution to my problem with multiple invocation of all test methods of the class with different authentications.

@paulpablo
Copy link

Thanks for posting, that was helpful.

@nikolmarku
Copy link

Anybody made this work for more than one test case in a class?
My test steps:

  1. open url
  2. Run test1
  3. Run test2

I would like to run test1 and test2 on may different urls and I would like to have tes1 and test2 as separated test and not merged into one test. Using testNG and DataProvider/factory does not offer this facility as DataProvider will repeat one test many times but not the second test and Factory will pass different parameter on second test

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