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

Commit

Permalink
Allow WebDriver to be stopped and started
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jan 21, 2016
1 parent ea41b51 commit f720cfb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
54 changes: 35 additions & 19 deletions functional-test/src/main/java/org/zanata/page/WebDriverFactory.java
Expand Up @@ -84,7 +84,7 @@ protected SimpleDateFormat initialValue() {
// can reuse, share globally
private static ObjectMapper mapper = new ObjectMapper();

private volatile EventFiringWebDriver driver = createDriver();
private @Nullable EventFiringWebDriver driver;
private DriverService driverService;
private TestEventForScreenshotListener screenshotListener;
private int webdriverWait;
Expand All @@ -102,8 +102,13 @@ protected SimpleDateFormat initialValue() {

@Nullable
private WebDriverEventListener logListener;
@Nullable
private BrowserMobProxy proxy;

public WebDriver getDriver() {
public synchronized EventFiringWebDriver getDriver() {
if (driver == null) {
driver = createDriver();
}
return driver;
}

Expand Down Expand Up @@ -173,6 +178,7 @@ private boolean ignorable(String message) {
* @throws WebDriverLogException exception containing the first warning/error message, if any
*/
private void logLogs(String type, boolean throwIfWarn) {
WebDriver driver = getDriver();
@Nullable
WebDriverLogException firstException = null;
String logName = WebDriverFactory.class.getName() + "." + type;
Expand Down Expand Up @@ -265,6 +271,7 @@ public int getWebDriverWait() {

public void registerScreenshotListener(String testName) {
log.info("Enabling screenshot module...");
EventFiringWebDriver driver = getDriver();
if (screenshotListener == null && ScreenshotDirForTest.isScreenshotEnabled()) {
screenshotListener = new TestEventForScreenshotListener(driver);
}
Expand All @@ -289,16 +296,16 @@ protected Object handleInvocation(
}
});
}
driver.register(logListener);
getDriver().register(logListener);
}

public void unregisterLogListener() {
driver.unregister(logListener);
getDriver().unregister(logListener);
}

public void unregisterScreenshotListener() {
log.info("Deregistering screenshot module...");
driver.unregister(screenshotListener);
getDriver().unregister(screenshotListener);
}

public void injectScreenshot(String tag) {
Expand All @@ -325,7 +332,7 @@ private EventFiringWebDriver configureChromeDriver() {
enableLogging(capabilities);

// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy = new BrowserMobProxyServer();
proxy.start(0);

proxy.addFirstHttpFilterFactory(new ResponseFilterAdapter.FilterSource(
Expand Down Expand Up @@ -389,21 +396,30 @@ private FirefoxProfile makeFirefoxProfile() {
return firefoxProfile;
}

public synchronized void killWebDriver() {
// If webdriver is running, kill it
if (driver != null) {
try {
log.info("Quitting webdriver.");
driver.quit();
driver = null;
} catch (Throwable e) {
// Ignoring driver tear down errors.
}
}
if (driverService != null && driverService.isRunning()) {
driverService.stop();
driverService = null;
}
if (proxy != null) {
proxy.abort();
proxy = null;
}
}

private class ShutdownHook extends Thread {
public void run() {
// If webdriver is running quit.
WebDriver driver = getDriver();
if (driver != null) {
try {
log.info("Quitting webdriver.");
driver.quit();
} catch (Throwable e) {
// Ignoring driver tear down errors.
}
}
if (driverService != null && driverService.isRunning()) {
driverService.stop();
}
killWebDriver();
}
}
}
Expand Up @@ -61,6 +61,8 @@ protected void before() throws Throwable {
@Override
protected void after() {
WebDriverFactory.INSTANCE.unregisterLogListener();
// uncomment this if you need a fresh browser between test runs
// WebDriverFactory.INSTANCE.killWebDriver();
}
};

Expand Down

0 comments on commit f720cfb

Please sign in to comment.