Skip to content

Commit

Permalink
Update to the most recent revision of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardesco committed Oct 20, 2014
1 parent 12592b5 commit 8e7a387
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 228 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ A maven template for Selenium that has the latest dependencies so that you can j
1. Open a terminal window/command prompt
2. Clone this project.
3. CD into project directory
4. mvn clean install -U -Pselenium-tests
5. mvn verify -Pselenium-tests
4. mvn clean verify

All dependencies should now be downloaded and the example google cheese test will have run successfully (Assuming you have Firefox installed in the default location)

### What should I know?

- To run any unit tests that test your Selenium framework you just need to ensure that all unit test file names end, or start with "test" and they will be run by step 4.
- The maven surefire plugin has been used to create a profile with the id "selenium-tests" that configures surefire to pick up any java files that ends with the text "ST". This means that as long as all of your selenium test file names end with ST.java they will get picked up and run when you perform step 5.
- The maven surefire plugin has been used to create a profile with the id "selenium-tests" that configures surefire to pick up any java files that ends with the text "ST". This means that as long as all of your selenium test file names end with WebDriver.java they will get picked up and run when you perform step 5.

### Anything else?

Expand All @@ -26,8 +25,14 @@ Yes you can specify which browser to use by using one of the following switches:
- -Dbrowser=ie
- -Dbrowser=opera
- -Dbrowser=htmlunit
- -Dbrowser=ghostdriver
- -Dbrowser=phantomjs

You don't need to worry about downloading the IEDriverServer, or chromedriver binaries, this project will do that for you automatically.

Not got PhantomJS? Don't worry that will be automatically downloaded for you as well!
Not got PhantomJS? Don't worry that will be automatically downloaded for you as well!

You can specify multiple threads:

- -Dthreads=2

You can even specify a grid to connect to:
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<env.config>local</env.config>
<browser>firefox</browser>
<threads>1</threads>
<standalone.binary.root.folder>${project.build.testResources}/selenium_standalone_binaries</standalone.binary.root.folder>
<standalone.binary.root.folder>${project.basedir}/src/test/resources/selenium_standalone_binaries</standalone.binary.root.folder>
<remote>false</remote>
<seleniumGridURL/>
<platform/>
Expand Down Expand Up @@ -101,20 +101,15 @@
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.lazerycode.selenium</groupId>
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
<version>1.0.3</version>
<configuration>
<rootStandaloneServerDirectory>${standalone.binary.root.folder}</rootStandaloneServerDirectory>
<downloadedZipFileDirectory>${project.build.testResources}/selenium_standalone_zips</downloadedZipFileDirectory>
<customRepositoryMap>${project.build.testResources}/RepositoryMap.xml</customRepositoryMap>
<downloadedZipFileDirectory>${project.basedir}/src/test/resources/selenium_standalone_zips</downloadedZipFileDirectory>
<customRepositoryMap>${project.basedir}/src/test/resources/RepositoryMap.xml</customRepositoryMap>
</configuration>
<executions>
<execution>
Expand Down
181 changes: 28 additions & 153 deletions src/test/java/com/lazerycode/selenium/DriverFactory.java
Original file line number Diff line number Diff line change
@@ -1,178 +1,53 @@
package com.lazerycode.selenium;

import com.lazerycode.selenium.config.BrowserType;
import com.opera.core.systems.OperaDriver;
import com.lazerycode.selenium.config.DriverType;
import com.lazerycode.selenium.listeners.ScreenshotListener;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.lazerycode.selenium.config.BrowserType.FIREFOX;
import static com.lazerycode.selenium.config.DriverType.determineEffectiveDriverType;

@Listeners(ScreenshotListener.class)
public class DriverFactory {

private static ResourceBundle _prop = ResourceBundle.getBundle("dev");
private static BrowserType browserType;
private static List<WebDriver> webDrivers = Collections.synchronizedList(new ArrayList<WebDriver>());
private static ThreadLocal<WebDriver> driverForThread = new ThreadLocal<WebDriver>() {

@Override
protected WebDriver initialValue() {
WebDriver driver = loadWebDriver();
webDrivers.add(driver);
return driver;
}
};
private static List<WebDriver> webDriverPool = Collections.synchronizedList(new ArrayList<WebDriver>());
private static ThreadLocal<WebDriver> driverThread;

@BeforeSuite
public static void setUpTest() {
for (BrowserType browser : BrowserType.values()) {
if (browser.toString().toLowerCase().equals(_prop.getString("browser").toLowerCase())) {
browserType = browser;
public static void instantiateDriverObject() {

final DriverType desiredDriver = determineEffectiveDriverType(System.getProperty("browser"));

driverThread = new ThreadLocal<WebDriver>() {
@Override
protected WebDriver initialValue() {
final WebDriver webDriver = desiredDriver.configureDriverBinaryAndInstantiateWebDriver();
webDriverPool.add(webDriver);
return webDriver;
}
}
if (browserType == null) {
System.err.println("Unknown browser specified, defaulting to 'Firefox'...");
browserType = FIREFOX;
}
};
}

@AfterSuite
public static void tearDown() {
for (WebDriver driver : webDrivers) {
driver.quit();
}
public static WebDriver getDriver() {
return driverThread.get();
}

@AfterMethod
public static void clearCookies() {
getDriver().manage().deleteAllCookies();
}

public static WebDriver getDriver() {
return driverForThread.get();
}

private static DesiredCapabilities generateDesiredCapabilities(BrowserType capabilityType) {
DesiredCapabilities capabilities;

switch (capabilityType) {
case IE:
capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
capabilities.setCapability("requireWindowFocus", true);
break;
case SAFARI:
capabilities = DesiredCapabilities.safari();
capabilities.setCapability("safari.cleanSession", true);
break;
case OPERA:
capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.arguments", "-nowin -nomail");
break;
case GHOSTDRIVER:
capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability("takesScreenshot", true);
if (System.getProperties().getProperty("os.arch").toLowerCase().equals("x86_64") || System.getProperties().getProperty("os.arch").toLowerCase().equals("amd64")) {
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, _prop.getString("binaryRootFolder") + "/windows/phantomjs/64bit/1.9.7/phantomjs.exe");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, _prop.getString("binaryRootFolder") + "/osx/phantomjs/64bit/1.9.7/phantomjs");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, _prop.getString("binaryRootFolder") + "/linux/phantomjs/64bit/1.9.7/phantomjs");
}
} else {
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, _prop.getString("binaryRootFolder") + "/windows/phantomjs/32bit/1.9.7/phantomjs.exe");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, _prop.getString("binaryRootFolder") + "/osx/phantomjs/32bit/1.9.7/phantomjs");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, _prop.getString("binaryRootFolder") + "/linux/phantomjs/32bit/1.9.7/phantomjs");
}
}
break;
case CHROME:
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));
HashMap<String, String> chromePreferences = new HashMap<String, String>();
chromePreferences.put("profile.password_manager_enabled", "false");
capabilities.setCapability("chrome.prefs", chromePreferences);
break;
case FIREFOX:
capabilities = DesiredCapabilities.firefox();
break;
case HTMLUNIT:
default:
capabilities = DesiredCapabilities.htmlUnit();
capabilities.setCapability("javascriptEnabled", "true");
}

return capabilities;
}

private static WebDriver loadWebDriver() {
System.out.println("Current Operating System: " + System.getProperties().getProperty("os.name"));
System.out.println("Current Architecture: " + System.getProperties().getProperty("os.arch"));
System.out.println("Current Browser Selection: " + browserType);

//Load standalone executable if required
switch (browserType) {
case CHROME:
if (System.getProperties().getProperty("os.arch").toLowerCase().equals("x86_64") || System.getProperties().getProperty("os.arch").toLowerCase().equals("amd64")) {
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/windows/googlechrome/64bit/2.9/chromedriver.exe");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/osx/googlechrome/64bit/2.9/chromedriver");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/linux/googlechrome/64bit/2.9/chromedriver");
}
} else {
if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/windows/googlechrome/32bit/2.9/chromedriver.exe");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/osx/googlechrome/32bit/2.9/chromedriver");
} else if (System.getProperties().getProperty("os.name").toLowerCase().contains("linux")) {
System.setProperty("webdriver.chrome.driver", _prop.getString("binaryRootFolder") + "/linux/googlechrome/32bit/2.9/chromedriver");
}
}
break;
case IE:
if (System.getProperties().getProperty("os.arch").toLowerCase().equals("x86_64") || System.getProperties().getProperty("os.arch").toLowerCase().equals("amd64")) {
System.setProperty("webdriver.ie.driver", _prop.getString("binaryRootFolder") + "/windows/internetexplorer/64bit/2.41.0/IEDriverServer.exe");
} else {
System.setProperty("webdriver.ie.driver", _prop.getString("binaryRootFolder") + "/windows/internetexplorer/32bit/2.41.0/IEDriverServer.exe");
}
break;
}

//Instantiate driver object
switch (browserType) {
case FIREFOX:
return new FirefoxDriver(generateDesiredCapabilities(browserType));
case CHROME:
return new ChromeDriver(generateDesiredCapabilities(browserType));
case IE:
return new InternetExplorerDriver(generateDesiredCapabilities(browserType));
case SAFARI:
return new SafariDriver(generateDesiredCapabilities(browserType));
case OPERA:
return new OperaDriver(generateDesiredCapabilities(browserType));
case GHOSTDRIVER:
return new PhantomJSDriver(generateDesiredCapabilities(browserType));
default:
return new HtmlUnitDriver(generateDesiredCapabilities(browserType));
@AfterSuite
public static void closeDriverObject() {
for (WebDriver driver : webDriverPool) {
driver.quit();
}
}
}
}
22 changes: 0 additions & 22 deletions src/test/java/com/lazerycode/selenium/config/BrowserType.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.lazerycode.selenium.config;

class DriverBinaryContext {

private static final String ROOT_FOLDER = System.getProperty("binaryRootFolder");
private final DriverType driverType;
private final SystemArchitecture systemArchitecture;
private final OperatingSystem operatingSystem;

private DriverBinaryContext(DriverType driverType, OperatingSystem operatingSystem, SystemArchitecture systemArchitecture) {
this.operatingSystem = operatingSystem;
this.driverType = driverType;
this.systemArchitecture = systemArchitecture;
}

static DriverBinaryContext binaryFor(DriverType browserType, OperatingSystem osName, SystemArchitecture architecture) {
return new DriverBinaryContext(browserType, osName, architecture);
}

static String binaryPath(String relativePath) {
return ROOT_FOLDER + relativePath;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

DriverBinaryContext that = (DriverBinaryContext) o;

if (driverType != that.driverType) return false;
if (operatingSystem != that.operatingSystem) return false;
if (systemArchitecture != that.systemArchitecture) return false;

return true;
}

@Override
public int hashCode() {
int result = driverType.hashCode();
result = 31 * result + systemArchitecture.hashCode();
result = 31 * result + operatingSystem.hashCode();
return result;
}
}
Loading

0 comments on commit 8e7a387

Please sign in to comment.