Skip to content

Commit

Permalink
Refactored Chrome and Edge to use a driver service
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Sep 29, 2016
1 parent 3392903 commit ad24564
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
package net.serenitybdd.core.support;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.IOException;

public class ChromeService {
private final ChromeDriverService chromeDriverService;

public class ChromeService extends ManagedDriverService{
public ChromeService() {
this.chromeDriverService = new ChromeDriverService.Builder()
super(new ChromeDriverService.Builder()
.usingAnyFreePort()
.build();
}

public void start() throws IOException {
chromeDriverService.start();
}

public void stop() {
chromeDriverService.stop();
}

public WebDriver newDriver() {
return newDriver(DesiredCapabilities.chrome());
}

public WebDriver newDriver(DesiredCapabilities capabilities) {
return new RemoteWebDriver(chromeDriverService.getUrl(), capabilities);
.build());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.serenitybdd.core.support;

import net.thucydides.core.util.EnvironmentVariables;
import org.openqa.selenium.edge.EdgeDriverService;

import java.io.File;

import static net.thucydides.core.ThucydidesSystemProperty.WEBDRIVER_IE_DRIVER;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

public class EdgeService extends ManagedDriverService {
public EdgeService(EnvironmentVariables environmentVariables) {
super(
isNotEmpty(environmentVariables.getProperty(WEBDRIVER_IE_DRIVER)) ?
new EdgeDriverService.Builder().usingAnyFreePort()
.usingDriverExecutable(new File(environmentVariables.getProperty(WEBDRIVER_IE_DRIVER)))
.build()
: new EdgeDriverService.Builder()
.usingAnyFreePort()
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.serenitybdd.core.support;

import net.thucydides.core.util.EnvironmentVariables;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;

import java.io.File;

import static net.thucydides.core.ThucydidesSystemProperty.WEBDRIVER_IE_DRIVER;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

public class InternetExplorerService extends ManagedDriverService{
public InternetExplorerService(EnvironmentVariables environmentVariables) {
super(
isNotEmpty(environmentVariables.getProperty(WEBDRIVER_IE_DRIVER)) ?
new InternetExplorerDriverService.Builder().usingAnyFreePort()
.usingDriverExecutable(new File(environmentVariables.getProperty(WEBDRIVER_IE_DRIVER)))
.build()
: new InternetExplorerDriverService.Builder()
.usingAnyFreePort()
.build()
);
}

@Override
public WebDriver newDriver(Capabilities capabilities) {
return new InternetExplorerDriver((InternetExplorerDriverService) driverService, capabilities);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.serenitybdd.core.support;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverService;

import java.io.IOException;

public abstract class ManagedDriverService<T extends DriverService> {
protected final DriverService driverService;

public ManagedDriverService(DriverService driverService) {
this.driverService = driverService;
}

public void start() throws IOException {
driverService.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if (driverService.isRunning()) {
driverService.stop();
}
}
});
}

public void stop() {
if (driverService.isRunning()) {
driverService.stop();
}
}

public WebDriver newDriver(Capabilities capabilities) {
return new RemoteWebDriver(driverService.getUrl(), capabilities);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package net.thucydides.core.webdriver.strategies;

import com.google.common.eventbus.Subscribe;
import net.serenitybdd.core.buildinfo.DriverCapabilityRecord;
import net.serenitybdd.core.support.ChromeService;
import net.serenitybdd.core.support.ManagedDriverService;
import net.thucydides.core.ThucydidesSystemProperty;
import net.thucydides.core.events.TestLifecycleEvents;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.util.EnvironmentVariables;
Expand All @@ -24,33 +23,22 @@ public class ChromeDriverBuilder implements DriverBuilder {
private final EnvironmentVariables environmentVariables;
private final CapabilityEnhancer enhancer;
private final DriverCapabilityRecord driverProperties;
private static final Logger LOGGER = LoggerFactory.getLogger(ChromeService.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ChromeDriverBuilder.class);

private ThreadLocal<ChromeService> chromeDriverService = new ThreadLocal<>();
private ThreadLocal<ManagedDriverService> driverService = new ThreadLocal<>();

private ChromeService getChromeDriverService() throws IOException {
if (chromeDriverService.get() == null) {
chromeDriverService.set(new ChromeService());
chromeDriverService.get().start();
private ManagedDriverService getDriverService() throws IOException {
if (driverService.get() == null) {
driverService.set(new ChromeService());
driverService.get().start();
}
return chromeDriverService.get();
return driverService.get();
}

public ChromeDriverBuilder(EnvironmentVariables environmentVariables, CapabilityEnhancer enhancer) {
this.environmentVariables = environmentVariables;
this.enhancer = enhancer;
this.driverProperties = Injectors.getInjector().getInstance(DriverCapabilityRecord.class);

TestLifecycleEvents.register(this);
}


@Subscribe
public void shutdownChromeService(TestLifecycleEvents.TestSuiteFinished testSuiteFinished) {
if (chromeDriverService.get() != null) {
chromeDriverService.get().stop();
chromeDriverService.remove();
}
}

@Override
Expand All @@ -64,7 +52,7 @@ public WebDriver newInstance() {
DesiredCapabilities desiredCapabilities = enhancer.enhanced(capabilities);
WebDriver driver;
try {
driver = getChromeDriverService().newDriver(desiredCapabilities);
driver = getDriverService().newDriver(desiredCapabilities);
} catch (IOException couldNotStartChromeServer) {
LOGGER.warn("Failed to start the chrome driver service, using a native driver instead", couldNotStartChromeServer.getMessage());
driver = new ChromeDriver(enhancer.enhanced(capabilities));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
package net.thucydides.core.webdriver.strategies;

import net.serenitybdd.core.buildinfo.DriverCapabilityRecord;
import net.serenitybdd.core.support.EdgeService;
import net.serenitybdd.core.support.ManagedDriverService;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.webdriver.CapabilityEnhancer;
import net.thucydides.core.webdriver.stubs.WebDriverStub;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class EdgeDriverBuilder implements DriverBuilder {

private final EnvironmentVariables environmentVariables;
private final CapabilityEnhancer enhancer;
private final DriverCapabilityRecord driverProperties;

private static final Logger LOGGER = LoggerFactory.getLogger(EdgeDriverBuilder.class);

public EdgeDriverBuilder(EnvironmentVariables environmentVariables, CapabilityEnhancer enhancer) {
this.environmentVariables = environmentVariables;
this.enhancer = enhancer;
this.driverProperties = Injectors.getInjector().getInstance(DriverCapabilityRecord.class);
}
private ThreadLocal<ManagedDriverService> driverService = new ThreadLocal<>();

private ManagedDriverService getDriverService() throws IOException {
if (driverService.get() == null) {
driverService.set(new EdgeService(environmentVariables));
driverService.get().start();
}
return driverService.get();
}


@Override
public WebDriver newInstance() {
if (StepEventBus.getEventBus().webdriverCallsAreSuspended()) {
return new WebDriverStub();
}
SafariDriver driver = new SafariDriver(enhancer.enhanced(DesiredCapabilities.edge()));
driverProperties.registerCapabilities("edge", driver.getCapabilities());
DesiredCapabilities desiredCapabilities = enhancer.enhanced(DesiredCapabilities.edge());
WebDriver driver;
try {
driver = getDriverService().newDriver(desiredCapabilities);
} catch (IOException couldNotStartChromeServer) {
LOGGER.warn("Failed to start the edge driver service, using a native driver instead", couldNotStartChromeServer.getMessage());
driver = new ChromeDriver(desiredCapabilities);
}
driverProperties.registerCapabilities("edge", desiredCapabilities);
return driver;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package net.thucydides.core.webdriver.strategies;

import net.serenitybdd.core.buildinfo.DriverCapabilityRecord;
import net.serenitybdd.core.support.InternetExplorerService;
import net.serenitybdd.core.support.ManagedDriverService;
import net.serenitybdd.core.time.InternalSystemClock;
import net.thucydides.core.ThucydidesSystemProperty;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.webdriver.CapabilityEnhancer;
import net.thucydides.core.webdriver.stubs.WebDriverStub;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

public class InternetExplorerDriverBuilder implements DriverBuilder {

Expand All @@ -33,48 +31,41 @@ public InternetExplorerDriverBuilder(EnvironmentVariables environmentVariables,
this.driverProperties = Injectors.getInjector().getInstance(DriverCapabilityRecord.class);
}

private ThreadLocal<ManagedDriverService> driverService = new ThreadLocal<>();

private ManagedDriverService getDriverService() throws IOException {
if (driverService.get() == null) {
driverService.set(new InternetExplorerService(environmentVariables));
driverService.get().start();
}
return driverService.get();
}

@Override
public WebDriver newInstance() {
if (StepEventBus.getEventBus().webdriverCallsAreSuspended()) {
return new WebDriverStub();
}

InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder().usingAnyFreePort();
String environmentDefinedIEDriverPath = environmentVariables.getProperty(ThucydidesSystemProperty.WEBDRIVER_IE_DRIVER);
if (StringUtils.isNotEmpty(environmentDefinedIEDriverPath)) {
builder.usingDriverExecutable(new File(environmentDefinedIEDriverPath));
}
final InternetExplorerDriverService service = builder.build();

try {
service.start();
} catch (Exception e) {
throw new RuntimeException("InternetExplorerDriverService could not be started", e);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
service.stop();
}
});
DesiredCapabilities browserCapabilities = DesiredCapabilities.internetExplorer();
browserCapabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
browserCapabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
browserCapabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, false);
browserCapabilities.setJavascriptEnabled(true);
browserCapabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);

InternetExplorerDriver driver = driver(service, browserCapabilities);
driverProperties.registerCapabilities("iexplorer", driver.getCapabilities());
return driver;
}

private InternetExplorerDriver driver(InternetExplorerDriverService service, DesiredCapabilities browserCapabilities) {
WebDriver driver;
try {
return new InternetExplorerDriver(service, browserCapabilities);
} catch (NoSuchSessionException e) {
driver = getDriverService().newDriver(browserCapabilities);
} catch (IOException e) {
LOGGER.error(e.getClass().getCanonicalName() + " happened - retrying in 2 seconds");
new InternalSystemClock().pauseFor(2000);
return new InternetExplorerDriver(service, browserCapabilities);
try {
driver = getDriverService().newDriver(browserCapabilities);
} catch (IOException failedASecondTime) {
driver = new InternetExplorerDriver(browserCapabilities);
}
}
driverProperties.registerCapabilities("iexplorer", browserCapabilities);
return driver;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.serenitybdd.core.annotations.findby.integration

import net.serenitybdd.core.support.ChromeService
import net.serenitybdd.core.support.ManagedDriverService
import net.thucydides.core.steps.StepEventBus
import net.thucydides.core.webdriver.SerenityWebdriverManager
import net.thucydides.core.webdriver.integration.PageWithFindBys
Expand All @@ -11,11 +11,11 @@ import spock.lang.Specification
class WhenLocatingWebElementsUsingEnhancedFindBys extends Specification {


@Shared ChromeService chromeService;
@Shared ManagedDriverService chromeService;
WebDriver driver

def setupSpec() {
chromeService = new ChromeService()
chromeService = new ManagedDriverService()
chromeService.start()
StepEventBus.eventBus.clear()

Expand Down
Loading

0 comments on commit ad24564

Please sign in to comment.