forked from Ardesco/Selenium-Maven-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to the most recent revision of code
- Loading branch information
Showing
13 changed files
with
442 additions
and
228 deletions.
There are no files selected for viewing
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
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
181 changes: 28 additions & 153 deletions
181
src/test/java/com/lazerycode/selenium/DriverFactory.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
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
22
src/test/java/com/lazerycode/selenium/config/BrowserType.java
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
src/test/java/com/lazerycode/selenium/config/DriverBinaryContext.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
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; | ||
} | ||
} |
Oops, something went wrong.