Skip to content

Commit

Permalink
Refactoring to simplify WebDriverBuilder, and make it use driver Opti…
Browse files Browse the repository at this point in the history
…ons classes for capabilities.
  • Loading branch information
barancev committed Jan 12, 2018
1 parent 004bcae commit 7046e64
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected void before() throws Throwable {

String baseUrl = whereIs("selenium-server/");

WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
WebDriver driver = new WebDriverBuilder().get(caps);
selenium = new WebDriverBackedSelenium(driver, baseUrl);

selenium.setBrowserLogLevel("debug");
Expand Down
2 changes: 1 addition & 1 deletion java/client/test/org/openqa/selenium/PageLoadingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void initLocalDriver(String strategy) {
localDriver.quit();
}
Capabilities caps = new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, strategy);
localDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
localDriver = new WebDriverBuilder().get(caps);
}

@Test
Expand Down
6 changes: 3 additions & 3 deletions java/client/test/org/openqa/selenium/ProxySettingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void canConfigureManualHttpProxy() {
Proxy proxyToUse = proxyServer.asProxy();
Capabilities caps = new ImmutableCapabilities(PROXY, proxyToUse);

WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
WebDriver driver = new WebDriverBuilder().get(caps);
registerDriverTeardown(driver);

driver.get(appServer.whereElseIs("simpleTest.html"));
Expand All @@ -103,7 +103,7 @@ public void canConfigureProxyThroughPACFile() {

Capabilities caps = new ImmutableCapabilities(PROXY, proxy);

WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
WebDriver driver = new WebDriverBuilder().get(caps);
registerDriverTeardown(driver);

driver.get(appServer.whereElseIs("mouseOver.html"));
Expand Down Expand Up @@ -132,7 +132,7 @@ public void canUsePACThatOnlyProxiesCertainHosts() throws Exception {

Capabilities caps = new ImmutableCapabilities(PROXY, proxy);

WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
WebDriver driver = new WebDriverBuilder().get(caps);
registerDriverTeardown(driver);

driver.get("http://" + getHostAndPort(helloServer));
Expand Down
2 changes: 1 addition & 1 deletion java/client/test/org/openqa/selenium/ReferrerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ WebDriver createDriver(String pacUrl) {

Capabilities caps = new ImmutableCapabilities(PROXY, proxy);

return driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
return driver = new WebDriverBuilder().get(caps);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void runScenarioWithUnhandledAlert(
Capabilities caps = behaviour == null
? new ImmutableCapabilities()
: new ImmutableCapabilities(UNEXPECTED_ALERT_BEHAVIOUR, behaviour);
driver2 = new WebDriverBuilder().setDesiredCapabilities(caps).get();
driver2 = new WebDriverBuilder().get(caps);

driver2.get(pages.alertsPage);
driver2.findElement(By.id("prompt-with-default")).click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public void disableDatabaseCapability() {

private void configureCapability(String capability, boolean isEnabled) {
Capabilities desiredCaps = new ImmutableCapabilities(capability, isEnabled);
WebDriverBuilder builder = new WebDriverBuilder().setDesiredCapabilities(desiredCaps);
localDriver = builder.get();
localDriver = new WebDriverBuilder().get(desiredCaps);
Capabilities caps = ((HasCapabilities) localDriver).getCapabilities();
assertTrue(String.format("The %s capability should be included in capabilities " +
"for the session", capability), caps.getCapability(capability) != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public void testPersistentHoverCanBeTurnedOff() throws Exception {
driver.quit();

Capabilities caps = new ImmutableCapabilities(ENABLE_PERSISTENT_HOVERING, false);
WebDriverBuilder builder = new WebDriverBuilder().setDesiredCapabilities(caps);
driver = builder.get();
driver = new WebDriverBuilder().get(caps);

try {
driver.get(pages.javascriptPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public void profilerLogShouldBeDisabledByDefault() {
public void shouldBeAbleToEnableProfilerLog() {
assumeFalse(isOldChromedriver(driver));
Capabilities caps = new ImmutableCapabilities(ENABLE_PROFILING_CAPABILITY, true);
WebDriverBuilder builder = new WebDriverBuilder().setDesiredCapabilities(caps);
localDriver = builder.get();
localDriver = new WebDriverBuilder().get(caps);
Set<String> logTypes = localDriver.manage().logs().getAvailableLogTypes();
assertTrue("Profiler log should be enabled", logTypes.contains(LogType.PROFILER));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ private void createWebDriverWithLogging(String logType, Level logLevel) {
LoggingPreferences loggingPrefs = new LoggingPreferences();
loggingPrefs.enable(logType, logLevel);
Capabilities caps = new ImmutableCapabilities(CapabilityType.LOGGING_PREFS, loggingPrefs);
WebDriverBuilder builder = new WebDriverBuilder().setDesiredCapabilities(caps);
localDriver = builder.get();
localDriver = new WebDriverBuilder().get(caps);
localDriver.get(pages.simpleTestPage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void createLocalDriverWithPerformanceLogType() {
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
Capabilities caps = new ImmutableCapabilities(CapabilityType.LOGGING_PREFS, logPrefs);
localDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
localDriver = new WebDriverBuilder().get(caps);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ public void testGetsYieldToPageLoadLogEntries() throws Exception {

private void startLoggingDriver() {
if (loggingDriver == null) {
WebDriverBuilder builder = new WebDriverBuilder().setDesiredCapabilities(
new ImmutableCapabilities(ENABLE_PROFILING_CAPABILITY, true));
loggingDriver = builder.get();
loggingDriver = new WebDriverBuilder()
.get(new ImmutableCapabilities(ENABLE_PROFILING_CAPABILITY, true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testShouldBeAbleToDisableSnapshotOnException() {

Capabilities caps = new ImmutableCapabilities("webdriver.remote.quietExceptions", true);

WebDriver noScreenshotDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
WebDriver noScreenshotDriver = new WebDriverBuilder().get(caps);

noScreenshotDriver.get(pages.simpleTestPage);

Expand Down
2 changes: 1 addition & 1 deletion java/client/test/org/openqa/selenium/testing/drivers/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ java_library(name = 'browser',

java_library(name = 'drivers',
srcs = glob(['*Driver.java', '*Supplier.java',]) + [
'BrowserToCapabilities.java',
'Firebug.java',
'IgnoreComparator.java',
'OutOfProcessSeleniumServer.java',
Expand All @@ -28,6 +27,7 @@ java_library(name = 'drivers',
'//java/client/src/org/openqa/selenium/firefox:firefox',
'//java/client/src/org/openqa/selenium/ie:ie',
'//java/client/src/org/openqa/selenium/opera:opera',
'//java/client/src/org/openqa/selenium/safari:safari',
'//java/client/src/org/openqa/selenium/remote:remote',
'//java/client/src/org/openqa/selenium/support:support',
'//java/client/test/org/openqa/selenium:helpers',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,84 @@

package org.openqa.selenium.testing.drivers;

import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.opera.OperaOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariOptions;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.stream.Stream;

public class WebDriverBuilder implements Supplier<WebDriver> {
private Capabilities desiredCapabilities;

private static Map<Browser, Supplier<Capabilities>> capabilitySuppliers =
new ImmutableMap.Builder<Browser, Supplier<Capabilities>>()
.put(Browser.chrome, ChromeOptions::new)
.put(Browser.ff, () -> new FirefoxOptions().setLegacy(!Boolean.parseBoolean(
System.getProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true"))))
.put(Browser.ie, () -> {
InternetExplorerOptions options = new InternetExplorerOptions();
if (Boolean.getBoolean("selenium.browser.native_events")) {
options.enableNativeEvents();
}
return options;
})
.put(Browser.htmlunit, DesiredCapabilities::htmlUnit)
.put(Browser.operablink, OperaOptions::new)
.put(Browser.safari, SafariOptions::new)
.build();

public static Capabilities getStandardCapabilitiesFor(Browser browser) {
return capabilitySuppliers.getOrDefault(browser, ImmutableCapabilities::new).get();
}

private final Browser browser;

public WebDriverBuilder() {
this(Browser.detect());
}

public WebDriverBuilder(Browser browser) {
if (browser == null) {
this.browser = Browser.chrome;
} else {
this.browser = browser;
}
this.browser = Optional.ofNullable(browser).orElse(Browser.chrome);
}

public WebDriver get() {
Capabilities standardCapabilities = BrowserToCapabilities.of(browser);
Capabilities desiredCaps = new MutableCapabilities(standardCapabilities).merge(desiredCapabilities);

List<Supplier<WebDriver>> suppliers = getSuppliers(desiredCaps);

for (Supplier<WebDriver> supplier : suppliers) {
WebDriver driver = supplier.get();
if (driver != null) {
modifyLogLevel(driver);
return driver;
}
}
return get(new ImmutableCapabilities());
}

throw new RuntimeException("Cannot instantiate driver instance: " + desiredCapabilities);
public WebDriver get(Capabilities desiredCapabilities) {
Capabilities desiredCaps = getStandardCapabilitiesFor(browser).merge(desiredCapabilities);

WebDriver driver =
Stream.of(
new ExternalDriverSupplier(desiredCaps),
new SauceBackedDriverSupplier(desiredCaps),
new GridSupplier(desiredCaps),
new RemoteSupplier(desiredCaps),
new TestInternetExplorerSupplier(desiredCaps),
new ReflectionBackedDriverSupplier(desiredCaps),
new DefaultDriverSupplier(desiredCaps))
.map(Supplier::get)
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new RuntimeException("Cannot instantiate driver instance: " + desiredCapabilities));

modifyLogLevel(driver);
return driver;
}

private void modifyLogLevel(WebDriver driver) {
Expand All @@ -78,23 +113,6 @@ private void modifyLogLevel(WebDriver driver) {
}
}

private List<Supplier<WebDriver>> getSuppliers(Capabilities desiredCaps) {
List<Supplier<WebDriver>> suppliers = Lists.newArrayList();
suppliers.add(new ExternalDriverSupplier(desiredCaps));
suppliers.add(new SauceBackedDriverSupplier(desiredCaps));
suppliers.add(new GridSupplier(desiredCaps));
suppliers.add(new RemoteSupplier(desiredCaps));
suppliers.add(new TestInternetExplorerSupplier(desiredCaps));
suppliers.add(new ReflectionBackedDriverSupplier(desiredCaps));
suppliers.add(new DefaultDriverSupplier(desiredCaps));
return suppliers;
}

public WebDriverBuilder setDesiredCapabilities(Capabilities caps) {
this.desiredCapabilities = caps;
return this;
}

private enum LogLevel {
OFF("OFF", Level.OFF),
DEBUG("DEBUG", Level.FINE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.drivers.BrowserToCapabilities;
import org.openqa.selenium.testing.drivers.OutOfProcessSeleniumServer;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -85,7 +85,7 @@ public void stopDriver() {
}

private void startDriver() {
Capabilities caps = BrowserToCapabilities.of(Browser.detect());
Capabilities caps = WebDriverBuilder.getStandardCapabilitiesFor(Browser.detect());
localDriver = new RemoteWebDriver(server.getWebDriverUrl(), caps);
localDriver.setFileDetector(new LocalFileDetector());
}
Expand Down

0 comments on commit 7046e64

Please sign in to comment.