forked from serenity-bdd/serenity-core
-
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.
Refactored Chrome and Edge to use a driver service
- Loading branch information
Showing
11 changed files
with
167 additions
and
95 deletions.
There are no files selected for viewing
31 changes: 4 additions & 27 deletions
31
serenity-core/src/main/java/net/serenitybdd/core/support/ChromeService.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,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()); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
serenity-core/src/main/java/net/serenitybdd/core/support/EdgeService.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,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() | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
serenity-core/src/main/java/net/serenitybdd/core/support/InternetExplorerService.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,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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
serenity-core/src/main/java/net/serenitybdd/core/support/ManagedDriverService.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,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); | ||
} | ||
} |
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
31 changes: 28 additions & 3 deletions
31
serenity-core/src/main/java/net/thucydides/core/webdriver/strategies/EdgeDriverBuilder.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,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; | ||
} | ||
} |
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
Oops, something went wrong.