[java] Refactoring test driver supplier again, no functional changes
package org.openqa.selenium.testing.drivers;
+import com.google.common.collect.ImmutableMap;
+
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.safari.SafariDriver;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+import java.util.function.Function;
import java.util.function.Supplier;
public class DefaultDriverSupplier implements Supplier<WebDriver> {
- private Supplier<WebDriver> driverSupplier;
+ private static Map<String, Function<Capabilities, WebDriver>> driverConstructors =
+ new ImmutableMap.Builder<String, Function<Capabilities, WebDriver>>()
+ .put(BrowserType.CHROME, TestChromeDriver::new)
+ .put(BrowserType.OPERA_BLINK, TestOperaBlinkDriver::new)
+ .put(BrowserType.FIREFOX, FirefoxDriver::new)
+ .put(BrowserType.HTMLUNIT, HtmlUnitDriver::new)
+ .put(BrowserType.IE, InternetExplorerDriver::new)
+ .put(BrowserType.EDGE, EdgeDriver::new)
+ .put(BrowserType.SAFARI, SafariDriver::new)
+ .put("Safari Technology Preview", SafariDriver::new)
+ .build();
+
+ private Capabilities capabilities;
+
+ DefaultDriverSupplier(Capabilities capabilities) {
+ this.capabilities = new ImmutableCapabilities(capabilities);
+ }
- public DefaultDriverSupplier(Capabilities capabilities) {
- String browserName = capabilities == null ? "" : capabilities.getBrowserName();
+ @Override
+ public WebDriver get() {
+ Function<Capabilities, WebDriver> driverConstructor;
- if (BrowserType.CHROME.equals(browserName)) {
- driverSupplier = () -> new TestChromeDriver(capabilities);
- } else if (BrowserType.OPERA_BLINK.equals(browserName)) {
- driverSupplier = () -> new TestOperaBlinkDriver(capabilities);
- } else if (BrowserType.FIREFOX.equals(browserName)) {
- driverSupplier = () -> new FirefoxDriver(capabilities);
- } else if (BrowserType.HTMLUNIT.equals(browserName)) {
- driverSupplier = () -> new HtmlUnitDriver(
- capabilities == null ? new ImmutableCapabilities() : capabilities);
- } else if (BrowserType.IE.equals(browserName)) {
- driverSupplier = () -> new InternetExplorerDriver(capabilities);
- } else if (BrowserType.EDGE.equals(browserName)) {
- driverSupplier = () -> new EdgeDriver(capabilities);
- } else if (browserName.toLowerCase().contains(BrowserType.SAFARI)) {
- driverSupplier = () -> new SafariDriver(capabilities);
- } else if (System.getProperty("selenium.browser.class_name") != null) {
- // No browser name specified, let's try reflection
+ if (capabilities != null) {
+ String browserName = capabilities.getBrowserName();
+ driverConstructor = driverConstructors.getOrDefault(browserName, caps -> {
+ throw new RuntimeException("No driver can be provided for capabilities " + caps);
+ });
+ } else {
String className = System.getProperty("selenium.browser.class_name");
- driverSupplier = () -> {
try {
Class<? extends WebDriver> driverClass = Class.forName(className).asSubclass(WebDriver.class);
- return driverClass.getConstructor(Capabilities.class).newInstance(capabilities);
- } catch (ReflectiveOperationException e) {
+ Constructor<? extends WebDriver> constructor = driverClass.getConstructor(Capabilities.class);
+ driverConstructor = (caps) -> {
+ try {
+ return constructor.newInstance(caps);
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
};
- } else {
- throw new RuntimeException("No driver can be provided for capabilities " + capabilities);
+ } catch (ClassNotFoundException | NoSuchMethodException e) {
+ throw new RuntimeException(e);
}
}
- @Override
- public WebDriver get() {
- return driverSupplier.get();
+ return driverConstructor.apply(capabilities);
}
}
