Created
September 26, 2012 19:06
-
-
Save rolfvreijdenberger/3789910 to your computer and use it in GitHub Desktop.
selenium fixture in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.vreijdenberger.tests.fixtures; | |
import java.math.BigInteger; | |
import java.net.URL; | |
import java.security.SecureRandom; | |
import java.util.Set; | |
import java.util.concurrent.TimeUnit; | |
import nl.vreijdenberger.model.Registry; | |
import org.openqa.selenium.Dimension; | |
import org.openqa.selenium.Point; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.firefox.FirefoxProfile; | |
import org.openqa.selenium.htmlunit.HtmlUnitDriver; | |
import org.openqa.selenium.ie.InternetExplorerDriver; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.AfterSuite; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.BeforeSuite; | |
import org.testng.annotations.Optional; | |
import org.testng.annotations.Parameters; | |
import com.opera.core.systems.OperaDriver; | |
/** | |
* generic base class with the common fixture for all tests for the System Under | |
* Test (SUT). It provides the minimal, fresh fixture for selenium tests. All | |
* selenium tests will be run with the testng testing framework | |
* | |
* <b>All tests should extend this class or one of it's subclasses that provide | |
* a fixture.<b> the reason for this is that this class is parameterized to | |
* provide the data for all the tests to run correctly on different | |
* platforms/environments. | |
* | |
* This class provides an implementation of the (Xunit Test Patterns) patterns | |
* <ul> | |
* <li>Minimal Fixture</li> | |
* <li>Standard Fixture</li> | |
* <li>TestCase Superclass</li> | |
* <li>TestCase class per Fixture</li> | |
* </ul> | |
* Subclasses will automatically have the right fixture and can use common | |
* functionality from this base class. The variables come in via the testng.xml | |
* file. Depending on these values, the tests will be run with differing | |
* behaviour. | |
* | |
* TODO: implement this???: | |
* http://stackoverflow.com/questions/6563725/new-webdriver | |
* -instance-per-test-method | |
* | |
* @author rolf vreijdenberger | |
* @link http://www.testng.org | |
* @link http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html | |
* @see http | |
* ://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium | |
* -the-rosetta-stone/ | |
*/ | |
public class MinimalFixture { | |
/** | |
* the driver we are using to run the full test suite we will always need a | |
* driver for a webbrowser when running these selenium based tests. | |
*/ | |
private WebDriver driver; | |
private static final String ENVIRONMENT_LOCAL = "local"; | |
private static final String ENVIRONMENT_REMOTE = "remote"; | |
private static final String BROWSER_FIREFOX = "firefox"; | |
private static final String BROWSER_CHROME = "chrome"; | |
private static final String BROWSER_IE = "ie"; | |
private static final String BROWSER_OPERA = "opera"; | |
private static final String BROWSER_GENERIC = "generic"; | |
private static final String HUB_DEFAULT = "http://hub.vreijdenberger.nl/wd/hub"; | |
private static final String URL_DEFAULT = "http://www.vreijdenberger.nl/"; | |
private static final String URL_JENKINS = "http://jenkins.vreijdenberger.nl/"; | |
private static final String URL_LIVE = "http://www.vreijdenberger.nl.nl/"; | |
private static final String URL_DEV = "http://rolf.dev.vreijdenberger.nl/app_dev.php/"; | |
/** | |
* the base url of the site we are testing | |
*/ | |
private String baseUrl; | |
/** | |
* the testing environment we are in at the moment | |
*/ | |
private String environment = ENVIRONMENT_LOCAL; | |
/** | |
* the browser we are using for our tests | |
*/ | |
private String browser = BROWSER_FIREFOX; | |
/** | |
* the original driver handle name, used to be able to switch to the right window and back if necessary. | |
*/ | |
private String handle; | |
/** | |
* the selenium grid hub | |
*/ | |
private String hub = HUB_DEFAULT; | |
private Point position; | |
private Dimension size; | |
@BeforeSuite | |
public void setupSuite() { | |
debug("setup suite"); | |
} | |
@AfterSuite | |
public void tearDownSuite() { | |
// TODO: find out if we can do some ultimate cleanup | |
debug("tear down suite"); | |
} | |
/** | |
* sets up functionalities for the whole class always runs as first item in | |
* the class It's a template method and provides a hook for subclasses to | |
* override/alter the setup | |
* | |
* | |
* @param browser | |
* @param baseUrl | |
* @param environment | |
* @param hub | |
* @throws Exception | |
*/ | |
@BeforeClass(alwaysRun = true) | |
@Parameters({ "browser", "baseUrl", "environment", "seleniumGridHub" }) | |
public void setUpClass(@Optional(BROWSER_FIREFOX) String browser, | |
@Optional(URL_DEV) String baseUrl, | |
@Optional(ENVIRONMENT_LOCAL) String environment, | |
@Optional(HUB_DEFAULT) String hub) throws Exception { | |
try { | |
setBaseUrl(baseUrl); | |
setHub(hub); | |
setBrowser(browser); | |
setEnvironment(environment); | |
DesiredCapabilities capabilities; | |
debug("setupClass: ".concat(getEnvironment()) | |
+ ", ".concat(getBrowser()) | |
+ ", ".concat(getBaseUrl())); | |
// TODO, we need to have a good foolproof mechanism to setup the | |
// tests | |
// this should be in conjunction with the testng.xml file | |
// firefox is the only driver working at the moment. | |
if (environment.equals(ENVIRONMENT_REMOTE)) { | |
if (browser.equals(BROWSER_CHROME)) { | |
capabilities = DesiredCapabilities.chrome(); | |
setDriver(new RemoteWebDriver(new URL(getHub()), | |
capabilities)); | |
} else if (browser.equals(BROWSER_FIREFOX)) { | |
capabilities = DesiredCapabilities.firefox(); | |
setDriver(new RemoteWebDriver(new URL(getHub()), | |
capabilities)); | |
} else if (browser.equals(BROWSER_IE)) { | |
capabilities = DesiredCapabilities.internetExplorer(); | |
setDriver(new RemoteWebDriver(new URL(getHub()), | |
capabilities)); | |
} else if (browser.equals(BROWSER_OPERA)) { | |
capabilities = DesiredCapabilities.opera(); | |
setDriver(new RemoteWebDriver(new URL(getHub()), | |
capabilities)); | |
} else if (browser.equals(BROWSER_GENERIC)) { | |
capabilities = DesiredCapabilities.htmlUnit(); | |
HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(true); | |
setDriver(htmlUnitDriver); | |
// System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", | |
// "fatal"); | |
} else { | |
capabilities = DesiredCapabilities.firefox(); | |
setDriver(new RemoteWebDriver(new URL(getHub()), | |
capabilities)); | |
} | |
} else { | |
if (browser.equals(BROWSER_CHROME)) { | |
System.setProperty("webdriver.chrome.driver", | |
"/Applications/chromedriver"); | |
setDriver(new ChromeDriver()); | |
debug("switching to local environment setup with chrome browser"); | |
} else if (browser.equals(BROWSER_FIREFOX)) { | |
FirefoxProfile profile = new FirefoxProfile(); | |
SecureRandom random = new SecureRandom(); | |
String value = new BigInteger(130, random).toString(32); | |
profile.setPreference("some.preference", value); | |
profile.setPreference("browser.display.background_color", "#ff9900"); | |
debug(profile.layoutOnDisk().toString()); | |
setDriver(new FirefoxDriver(profile)); | |
debug("switching to local environment setup with firefox browser"); | |
} else if (browser.equals(BROWSER_IE)) { | |
setDriver(new InternetExplorerDriver()); | |
debug("switching to local environment setup with ie browser"); | |
} else if (browser.equals(BROWSER_OPERA)) { | |
setDriver(new OperaDriver()); | |
debug("switching to local environment setup with opera browser"); | |
} else if (browser.equals(BROWSER_GENERIC)) { | |
setDriver(new HtmlUnitDriver(true)); | |
debug("switching to local environment setup with htmlunit browser"); | |
} else { | |
debug("switching to local environment setup with firefox browser"); | |
FirefoxProfile profile = new FirefoxProfile(); | |
SecureRandom random = new SecureRandom(); | |
profile.setPreference("browser.display.background_color", "#ff9900"); | |
debug(profile.layoutOnDisk().toString()); | |
setDriver(new FirefoxDriver(profile)); | |
} | |
} | |
// each component we initialize will have a reference to the base | |
// url of the SUT | |
Registry.setBaseUrl(getBaseUrl()); | |
// default waiting time if an element we search for is not (yet) | |
// present. | |
//http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html | |
setImplicitWait(Registry.getDefaultTimeOut()); | |
//now that we have a window, store a reference to the drivers' original window handle | |
setHandle(getDriver().getWindowHandle()); | |
// hook for subclasses | |
setupClassHook(); | |
} catch (Exception e) { | |
debug(e.getMessage()); | |
} finally { | |
position = new Point(0,0); | |
size = new Dimension(1024, 800); | |
setWindow(position, size); | |
} | |
} | |
/** | |
* can be overriden by subclasses to implement setup logic | |
*/ | |
protected void setupClassHook() { | |
} | |
/** | |
* always run after the class so we can teardown some stuff it's a template | |
* method so subclasses can override | |
*/ | |
@AfterClass(alwaysRun = true) | |
public final void tearDownClass() { | |
// suppress potential errors coming from subclass so we can always | |
// cleanup our driver | |
debug("Teardown"); | |
try { | |
// do any class specific teardown via this hook | |
tearDownClassHook(); | |
} catch (Exception e) { | |
debug(e.getMessage()); | |
} finally { | |
if (driver != null) { | |
getDriver().close(); | |
getDriver().quit(); | |
driver = null; | |
} | |
} | |
} | |
/** | |
* can be overriden by subclasses to implement teardown logic | |
*/ | |
protected void tearDownClassHook() { | |
} | |
/** | |
* helper method to get the baseurl of the site we are testing | |
* | |
* @return | |
*/ | |
protected String getBaseUrl() { | |
return this.baseUrl; | |
} | |
/** | |
* helper method to set/override the baseurl of the site we are testing | |
* | |
* @param baseUrl | |
*/ | |
private void setBaseUrl(String baseUrl) { | |
this.baseUrl = baseUrl; | |
} | |
/** | |
* utility method to get the full url with path | |
* | |
* @param path | |
* @return String the baseurl with the path appended | |
*/ | |
protected String getFullUrl(String path) { | |
return getBaseUrl() + path; | |
} | |
/** | |
* used to output debug messages to the shell | |
* | |
* @param message | |
* @return | |
*/ | |
protected void debug(String message) { | |
System.out.println(this.getClass().getSimpleName() + ": " + message); | |
} | |
/** | |
* @return the environment | |
*/ | |
public String getEnvironment() { | |
return environment; | |
} | |
/** | |
* @param environment | |
* the environment to set | |
*/ | |
private void setEnvironment(String environment) { | |
this.environment = environment; | |
} | |
/** | |
* @return the browser | |
*/ | |
protected String getBrowser() { | |
return browser; | |
} | |
/** | |
* @param browser | |
* the browser to set | |
*/ | |
private void setBrowser(String browser) { | |
this.browser = browser; | |
} | |
/** | |
* @return the hub | |
*/ | |
protected String getHub() { | |
return hub; | |
} | |
/** | |
* @param hub | |
* the hub to set | |
*/ | |
private void setHub(String hub) { | |
this.hub = hub; | |
} | |
/** | |
* @return the driver | |
*/ | |
protected WebDriver getDriver() { | |
return driver; | |
} | |
/** | |
* @param driver | |
* the driver to set | |
*/ | |
private void setDriver(WebDriver driver) { | |
this.driver = driver; | |
} | |
/** | |
* set the waiting time to use when polling for an element that is not (yet) | |
* present. This means that when an element is not present and we search for | |
* it, the tests will stop for the specified time which might make testing | |
* slow! | |
* | |
* @link http://seleniumhq.org/docs/ | |
* 04_webdriver_advanced.html#explicit-and-implicit-waits | |
* @param seconds | |
*/ | |
protected void setImplicitWait(int seconds) { | |
getDriver().manage().timeouts() | |
.implicitlyWait(seconds, TimeUnit.SECONDS); | |
} | |
/** | |
* gets an assertion message to append to each assertion as debug | |
* information | |
* | |
* @param message | |
* @param expected | |
* @param got | |
* @return | |
*/ | |
public String getAssertionMessage(String message, String expected, | |
String got) { | |
return message.concat(". ").concat("got: ").concat(got) | |
.concat(", expected: ").concat(expected); | |
} | |
/** | |
* gets an assertion message to append to each assertion as debug | |
* information | |
* | |
* @param message | |
* @param got | |
* @return | |
*/ | |
public String getAssertionMessage(String message, String got) { | |
return message.concat(" ").concat("got: ").concat(got); | |
} | |
/** | |
* gets an assertion message to append to each assertion as debug | |
* information | |
* | |
* @param message | |
* @return | |
*/ | |
public String getAssertionMessage(String message) { | |
return message; | |
} | |
/** | |
* waits for a number of seconds | |
* | |
* @param seconds | |
*/ | |
public void wait(int seconds) { | |
try { | |
Thread.sleep(seconds * 1000); | |
} catch (Exception e) { | |
} | |
} | |
/** | |
* @return the handle | |
*/ | |
public String getHandle() { | |
return handle; | |
} | |
/** | |
* @param handle the handle to set | |
*/ | |
private void setHandle(String handle) { | |
this.handle = handle; | |
} | |
/** | |
* if a new window has been opened we can easily switch to the latest opened window | |
*/ | |
public WebDriver switchToNewWindow(){ | |
getDriver().switchTo().window(getLastOpenedWindownHandle()); | |
return getDriver(); | |
} | |
/** | |
* switch back to original window | |
* @see switchToNewWindow() | |
*/ | |
public WebDriver switchToOriginalWindow(){ | |
getDriver().switchTo().window(getHandle()); | |
return getDriver(); | |
} | |
/** | |
* gets the handle to the last opened window | |
* @return | |
*/ | |
public String getLastOpenedWindownHandle() { | |
String output = getHandle(); | |
//the new window will be the last one in the set of open windows (even though a set should not have order) | |
Set<String> handles = getDriver().getWindowHandles(); | |
for(String handle :handles){ | |
output = handle; | |
} | |
return output; | |
} | |
/** | |
* sets the window size and position | |
* can be used to get elements into or out the viewport for testing | |
* @param x | |
* @param y | |
* @param width | |
* @param height | |
*/ | |
public void setWindow(int x, int y, int width, int height){ | |
setWindow(new Point(x,y), new Dimension(width, height)); | |
} | |
/** | |
* sets the window size and position | |
* can be used to get elements into or out the viewport for testing | |
* @param position | |
* @param size | |
*/ | |
private void setWindow(Point p, Dimension d) { | |
try{ | |
getDriver().manage().window().setPosition(p); | |
getDriver().manage().window().setSize(d); | |
}catch(Exception e){ | |
debug(e.getMessage()); | |
} | |
} | |
/** | |
* restores the window to it's originally defined sizes and position | |
*/ | |
public void restoreWindow() { | |
setWindow(position, size); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment