Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Fix WebDriver for Firefox.
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 8d7dc26
Author: Damian Jansen <djansen@redhat.com>
Date:   Mon May 18 22:25:19 2015 +1000

    Clean up WebDriverFactory

commit dc1ea7c
Author: Damian Jansen <djansen@redhat.com>
Date:   Mon May 18 14:50:48 2015 +1000

    Fix cannot cast FirefoxDriver to EventFiringWebDriver error
  • Loading branch information
djansen-redhat committed Jul 6, 2015
1 parent 46f7b22 commit 362e8d7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 84 deletions.
1 change: 1 addition & 0 deletions functional-test/pom.xml
Expand Up @@ -842,6 +842,7 @@
<echo>-Dwebdriver.chrome.bin=full path to chrome binary.</echo>
<echo>-Dwebdriver.chrome.driver=full path to chromedriver binary. Default is chromedriver on $PATH</echo>
<echo>-Dwebdriver.wait=global wait time in seconds for element searches. Default is 10.</echo>
<echo>-Dwebdriver.screenshot.dir=location to store screenshots during test execution.Default is ${project.build.directory}/screenshots</echo>
<echo>NB: set env var DISPLAY to run test browser in alternative display, for Xnest/Xvfb/Xvnc. eg: xvfb-run -e mvn verify -Dappserver=wildfly8</echo>
<echo>==========================================================</echo>
<echo>to ask cargo to start up then wait so that tests can be run manually: mvn clean package cargo:run -Dappserver=wildfly8 -Dmysql.port=13306</echo>
Expand Down
107 changes: 42 additions & 65 deletions functional-test/src/main/java/org/zanata/page/WebDriverFactory.java
Expand Up @@ -22,10 +22,8 @@

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxBinary;
Expand All @@ -35,7 +33,6 @@
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
Expand All @@ -50,8 +47,6 @@
import org.zanata.util.ScreenshotDirForTest;
import org.zanata.util.TestEventForScreenshotListener;

import static org.zanata.util.Constants.chrome;
import static org.zanata.util.Constants.firefox;
import static org.zanata.util.Constants.webDriverType;
import static org.zanata.util.Constants.webDriverWait;
import static org.zanata.util.Constants.zanataInstance;
Expand All @@ -60,33 +55,47 @@
public enum WebDriverFactory {
INSTANCE;

private volatile WebDriver driver = createDriver();
private volatile EventFiringWebDriver driver = createDriver();
private DriverService driverService;
private TestEventForScreenshotListener eventListener;
private int webdriverWait;

public WebDriver getDriver() {
return driver;
}

private WebDriver createDriver() {
WebDriver driver = createPlainDriver();
private EventFiringWebDriver createDriver() {
String driverType = PropertiesHolder.getProperty(webDriverType.value());
EventFiringWebDriver newDriver;
switch (driverType.toLowerCase()) {
case "chrome":
newDriver = configureChromeDriver();
break;
case "firefox":
newDriver = configureFirefoxDriver();
break;
default:
throw new UnsupportedOperationException("only support chrome " +
"and firefox driver");
}
webdriverWait = Integer.parseInt(PropertiesHolder
.getProperty(webDriverWait.value()));
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
return driver;
return newDriver;
}

/**
* List the WebDriver log types
*
* LogType.CLIENT doesn't seem to log anything
* LogType.DRIVER, LogType.PERFORMANCE are too verbose
* LogType PROFILER and LogType.SERVER don't seem to work
*
* @return String array of log types
*/
private String[] getLogTypes() {
return new String[]{
LogType.BROWSER,
// CLIENT doesn't seem to log anything
// LogType.CLIENT,
// useful, but verbose:
// LogType.DRIVER,
// LogType.PERFORMANCE,
// PROFILER and SERVER don't seem to work
// LogType.PROFILER,
// LogType.SERVER
LogType.BROWSER
};
}

Expand All @@ -96,9 +105,7 @@ private String[] getLogTypes() {
* (but they don't all seem to work)
*/
public LogEntries getLogs(String type) {
Logs logs = getDriver().manage().logs();
LogEntries logEntries = logs.get(type);
return logEntries;
return getDriver().manage().logs().get(type);
}

/**
Expand Down Expand Up @@ -139,42 +146,26 @@ public int getWebDriverWait() {
return webdriverWait;
}

public void updateListenerTestName(String testName) {
public void registerScreenshotListener(String testName) {
log.info("Enabling screenshot module...");
if (eventListener == null && ScreenshotDirForTest.isScreenshotEnabled()) {
eventListener = new TestEventForScreenshotListener(driver);
}
enableScreenshots();
driver.register(eventListener);
eventListener.updateTestID(testName);
}

private WebDriver enableScreenshots() {
log.debug("Enabling screenshot module...");
return EventFiringWebDriver.class.cast(driver).register(eventListener);
}

public void unregisterScreenshot() {
EventFiringWebDriver.class.cast(driver).unregister(eventListener);
public void unregisterScreenshotListener() {
log.info("Deregistering screenshot module...");
driver.unregister(eventListener);
}

private WebDriver createPlainDriver() {
String driverType =
PropertiesHolder.getProperty(webDriverType.value(), chrome.value());
if (driverType.equalsIgnoreCase(chrome.value())) {
return configureChromeDriver();
} else if (driverType.equalsIgnoreCase(firefox.value())) {
return configureFirefoxDriver();
} else {
throw new UnsupportedOperationException("only support chrome and firefox driver");
}
}

private WebDriver configureChromeDriver() {
private EventFiringWebDriver configureChromeDriver() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
PropertiesHolder.getProperty("webdriver.log"));
driverService = ChromeDriverService.createDefaultService();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities
.setCapability("chrome.binary", PropertiesHolder.properties
capabilities.setCapability("chrome.binary", PropertiesHolder.properties
.getProperty("webdriver.chrome.bin"));
enableLogging(capabilities);

Expand All @@ -183,31 +174,25 @@ private WebDriver configureChromeDriver() {
} catch (IOException e) {
throw new RuntimeException("fail to start chrome driver service");
}
return new EventFiringWebDriver(
new Augmenter().augment(new RemoteWebDriver(driverService
.getUrl(),
capabilities)));
return new EventFiringWebDriver(new Augmenter().augment(
new RemoteWebDriver(driverService.getUrl(), capabilities)));
}

private WebDriver configureFirefoxDriver() {
private EventFiringWebDriver configureFirefoxDriver() {
final String pathToFirefox =
Strings.emptyToNull(PropertiesHolder.properties
.getProperty("firefox.path"));

FirefoxBinary firefoxBinary = null;
FirefoxBinary firefoxBinary;
if (pathToFirefox != null) {
firefoxBinary = new FirefoxBinary(new File(pathToFirefox));
} else {
firefoxBinary = new FirefoxBinary();
}
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
enableLogging(capabilities);

/*
* TODO: Evaluate current timeout Timeout the connection in 30 seconds
* firefoxBinary.setTimeout(TimeUnit.SECONDS.toMillis(30));
*/
return new FirefoxDriver(firefoxBinary, makeFirefoxProfile(), capabilities);
return new EventFiringWebDriver(new FirefoxDriver(firefoxBinary,
makeFirefoxProfile(), capabilities));
}

private void enableLogging(DesiredCapabilities capabilities) {
Expand All @@ -225,14 +210,6 @@ private FirefoxProfile makeFirefoxProfile() {
// TODO - look at FirefoxDriver.getProfile().
}
final FirefoxProfile firefoxProfile = new FirefoxProfile();

/*
* TODO: Evaluate need for this Disable unnecessary connection to
* sb-ssl.google.com
* firefoxProfile.setPreference("browser.safebrowsing.malware.enabled",
* false);
*/

firefoxProfile.setAlwaysLoadNoFocusLib(true);
firefoxProfile.setEnableNativeEvents(true);
firefoxProfile.setAcceptUntrustedCertificates(true);
Expand Down
16 changes: 0 additions & 16 deletions functional-test/src/main/java/org/zanata/util/Constants.java
Expand Up @@ -55,20 +55,4 @@ public String value() {
return value;
}

public static Map<String, String> projectTypeOptions() {
Map<String, String> projectTypeOptions = new HashMap<String, String>();
projectTypeOptions.put("None", "-- No selection --");
projectTypeOptions.put("File",
"File. For plain text, LibreOffice, InDesign, HTML.");
projectTypeOptions.put("Gettext",
"Gettext. For gettext software strings.");
projectTypeOptions.put("Podir", "Podir. For publican/docbook strings.");
projectTypeOptions.put("Properties",
"Properties. For Java properties files.");
projectTypeOptions.put("Utf8Properties",
"Utf8Properties. For UTF8-encoded Java properties.");
projectTypeOptions.put("Xliff", "Xliff. For supported XLIFF files.");
projectTypeOptions.put("Xml", "Xml. For XML from the Zanata REST API.");
return projectTypeOptions;
}
}
Expand Up @@ -67,7 +67,10 @@ private void createScreenshot(String ofType) {
File testIDDir = null;
try {
testIDDir = ScreenshotDirForTest.screenshotForTest(testId);
testIDDir.mkdirs();
if (!testIDDir.exists()) {
log.info("Creating screenshot dir {}", testIDDir.getAbsolutePath());
assert (testIDDir.mkdirs());
}
File screenshotFile =
((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile,
Expand Down
Expand Up @@ -65,14 +65,15 @@ public void testFailure(Failure failure) throws Exception {

private static void enableScreenshotForTest(String testDisplayName)
throws Exception {
WebDriverFactory.INSTANCE.updateListenerTestName(testDisplayName);
WebDriverFactory.INSTANCE.registerScreenshotListener(testDisplayName);
String date = new Date().toString();
log.debug("[TEST] {}:{}", testDisplayName, date);
}

private static void deleteScreenshots(String testDisplayName) {
File testDir = ScreenshotDirForTest.screenshotForTest(testDisplayName);
try {
log.info("Deleting screenshots for {}", testDisplayName);
FileUtils.deleteDirectory(testDir);
} catch (IOException e) {
log.warn("error deleting screenshot base directory: {}",
Expand All @@ -81,6 +82,6 @@ private static void deleteScreenshots(String testDisplayName) {
}

private static void unregisterScreenshot() {
WebDriverFactory.INSTANCE.unregisterScreenshot();
WebDriverFactory.INSTANCE.unregisterScreenshotListener();
}
}

0 comments on commit 362e8d7

Please sign in to comment.