diff --git a/src/main/java/com/xceptance/neodymium/common/browser/BrowserRunnerHelper.java b/src/main/java/com/xceptance/neodymium/common/browser/BrowserRunnerHelper.java index 059dd152..8c7f884c 100644 --- a/src/main/java/com/xceptance/neodymium/common/browser/BrowserRunnerHelper.java +++ b/src/main/java/com/xceptance/neodymium/common/browser/BrowserRunnerHelper.java @@ -23,6 +23,7 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; @@ -30,7 +31,9 @@ import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.GeckoDriverService; import org.openqa.selenium.ie.InternetExplorerDriver; +import org.openqa.selenium.ie.InternetExplorerDriverService; import org.openqa.selenium.ie.InternetExplorerOptions; +import org.openqa.selenium.os.ExecutableFinder; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.remote.RemoteWebDriver; @@ -178,7 +181,7 @@ private static FirefoxBinary createFirefoxBinary(final String pathToBrowser) * if Selenium grid is * used and the given grid URL is invalid */ - public static WebDriverStateContainer createWebDriverStateContainer(final BrowserConfiguration config, Object testClassInstance) + public static WebDriverStateContainer createWebDriverStateContainer(final BrowserConfiguration config, final Object testClassInstance) throws MalformedURLException { final MutableCapabilities capabilities = config.getCapabilities(); @@ -186,7 +189,7 @@ public static WebDriverStateContainer createWebDriverStateContainer(final Browse SelenideProxyServer selenideProxyServer = null; if (Neodymium.configuration().useLocalProxy()) { - BrowserUpProxy proxy = setupEmbeddedProxy(); + final BrowserUpProxy proxy = setupEmbeddedProxy(); // set the Proxy for later usage wDSC.setProxy(proxy); @@ -203,16 +206,17 @@ else if (Neodymium.configuration().useProxy()) { if (Neodymium.configuration().enableSelenideProxy()) { - SelenideProxyServerFactory selenideProxyServerFactory = Plugins.inject(SelenideProxyServerFactory.class); + final SelenideProxyServerFactory selenideProxyServerFactory = Plugins.inject(SelenideProxyServerFactory.class); selenideProxyServer = selenideProxyServerFactory.create(new SelenideConfig(), (Proxy) capabilities.getCapability(CapabilityType.PROXY)); - var proxy = selenideProxyServer.getSeleniumProxy(); + final var proxy = selenideProxyServer.getSeleniumProxy(); capabilities.setCapability(CapabilityType.PROXY, proxy); } final String browserName = capabilities.getBrowserName(); if (chromeBrowsers.contains(browserName)) { - final ChromeOptions options = (ChromeOptions) capabilities; + final ChromeOptions options = new ChromeOptions(); + final String driverInPathPath = new ExecutableFinder().find("chromedriver"); // do we have a custom path? final String pathToBrowser = Neodymium.configuration().getChromeBrowserPath(); @@ -234,9 +238,10 @@ else if (Neodymium.configuration().useProxy()) options.setExperimentalOption("prefs", config.getPreferences()); } - if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || StringUtils.isNotBlank(config.getDownloadDirectory())) + if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || + StringUtils.isNotBlank(config.getDownloadDirectory())) { - HashMap prefs = new HashMap<>(); + final HashMap prefs = new HashMap<>(); // if we have configured prefs, we need to add all to the experimental options if (config.getPreferences() != null && !config.getPreferences().isEmpty()) @@ -252,12 +257,14 @@ else if (Neodymium.configuration().useProxy()) options.setExperimentalOption("prefs", prefs); } - - wDSC.setWebDriver(new ChromeDriver(options)); + wDSC.setWebDriver(new ChromeDriver(new ChromeDriverService.Builder() + .usingDriverExecutable(new File(driverInPathPath)) + .build(), options.merge(capabilities))); } else if (firefoxBrowsers.contains(browserName)) { - final FirefoxOptions options = new FirefoxOptions().merge(capabilities); + final FirefoxOptions options = new FirefoxOptions(); + final String driverInPathPath = new ExecutableFinder().find("geckodriver"); options.setBinary(createFirefoxBinary(Neodymium.configuration().getFirefoxBrowserPath())); if (config.isHeadless()) { @@ -267,9 +274,10 @@ else if (firefoxBrowsers.contains(browserName)) { options.addArguments(config.getArguments()); } - if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || StringUtils.isNotBlank(config.getDownloadDirectory())) + if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || + StringUtils.isNotBlank(config.getDownloadDirectory())) { - FirefoxProfile profile = new FirefoxProfile(); + final FirefoxProfile profile = new FirefoxProfile(); // if we have configured prefs, we need to add all to the experimental options if (config.getPreferences() != null && !config.getPreferences().isEmpty()) @@ -302,45 +310,52 @@ else if (StringUtils.isNumeric(val.toString())) options.setProfile(profile); } - wDSC.setWebDriver(new FirefoxDriver(new GeckoDriverService.Builder().withAllowHosts("localhost").build(), options)); + wDSC.setWebDriver(new FirefoxDriver(new GeckoDriverService.Builder().withAllowHosts("localhost") + .usingDriverExecutable(new File(driverInPathPath)) + .build(), options.merge(capabilities))); } else if (internetExplorerBrowsers.contains(browserName)) { - final InternetExplorerOptions options = new InternetExplorerOptions().merge(capabilities); + final InternetExplorerOptions options = new InternetExplorerOptions(); + final String driverInPathPath = new ExecutableFinder().find("IEDriverServer"); if (config.getArguments() != null && config.getArguments().size() > 0) { - for (String argument : config.getArguments()) + for (final String argument : config.getArguments()) { options.addCommandSwitches(argument); } } - wDSC.setWebDriver(new InternetExplorerDriver(options)); + wDSC.setWebDriver(new InternetExplorerDriver(new InternetExplorerDriverService.Builder() + .usingDriverExecutable(new File(driverInPathPath)) + .build(), options.merge(capabilities))); } else if (safariBrowsers.contains(browserName)) { - final SafariOptions options = (SafariOptions) capabilities; + // safari driver is not expected to be in PATH, it will be looked in + // /usr/bin/safaridriver and /Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver + final SafariOptions options = new SafariOptions(); wDSC.setWebDriver(new SafariDriver(options)); } else { - wDSC.setWebDriver(new RemoteWebDriver(capabilities)); + wDSC.setWebDriver(new RemoteWebDriver(capabilities.merge(capabilities))); } } else { // establish connection to target website - TestEnvironment testEnvironmentProperties = MultibrowserConfiguration.getInstance().getTestEnvironment(testEnvironment); + final TestEnvironment testEnvironmentProperties = MultibrowserConfiguration.getInstance().getTestEnvironment(testEnvironment); if (testEnvironmentProperties == null) { throw new IllegalArgumentException("No properties found for test environment: \"" + testEnvironment + "\""); } - String testEnvironmentUrl = testEnvironmentProperties.getUrl(); + final String testEnvironmentUrl = testEnvironmentProperties.getUrl(); ClientConfig configClient = ClientConfig.defaultConfig(); configClient = configClient.baseUrl(new URL(testEnvironmentUrl)); config.getGridProperties().put("userName", testEnvironmentProperties.getUsername()); config.getGridProperties().put("accessKey", testEnvironmentProperties.getPassword()); - String buildId = StringUtils.isBlank(System.getenv("BUILD_NUMBER")) ? "local run" : System.getenv("BUILD_NUMBER"); + final String buildId = StringUtils.isBlank(System.getenv("BUILD_NUMBER")) ? "local run" : System.getenv("BUILD_NUMBER"); config.getGridProperties().put("sessionName", testClassInstance.getClass().toString()); config.getGridProperties().put("buildName", "Test Automation"); config.getGridProperties().put("buildIdentifier", buildId); @@ -354,10 +369,10 @@ else if (testEnvironmentUrl.contains("saucelabs")) } else { - String optionsTag = testEnvironmentProperties.getOptionsTag(); + final String optionsTag = testEnvironmentProperties.getOptionsTag(); if (StringUtils.isBlank(optionsTag)) { - for (String key : config.getGridProperties().keySet()) + for (final String key : config.getGridProperties().keySet()) { capabilities.setCapability(key, config.getGridProperties().get(key)); } @@ -369,7 +384,7 @@ else if (testEnvironmentUrl.contains("saucelabs")) } wDSC.setWebDriver(new RemoteWebDriver(new HttpCommandExecutor(new HashMap<>(), configClient, new NeodymiumProxyHttpClientFactory(testEnvironmentProperties)), capabilities)); } - WebDriver decoratedDriver = new EventFiringDecorator(new NeodymiumWebDriverListener()).decorate(wDSC.getWebDriver()); + final WebDriver decoratedDriver = new EventFiringDecorator(new NeodymiumWebDriverListener()).decorate(wDSC.getWebDriver()); wDSC.setDecoratedWebDriver(decoratedDriver); WebDriverRunner.setWebDriver(decoratedDriver, selenideProxyServer); return wDSC; @@ -383,7 +398,8 @@ private static BrowserUpProxy setupEmbeddedProxy() if (Neodymium.configuration().useLocalProxyWithSelfSignedCertificate()) { final CertificateAndKeySource rootCertificateSource = createLocalProxyRootCertSource(); - final ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder().rootCertificateSource(rootCertificateSource).build(); + final ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder().rootCertificateSource(rootCertificateSource) + .build(); proxy.setMitmManager(mitmManager); } else @@ -451,8 +467,10 @@ public static Proxy createProxyCapabilities() final Proxy webdriverProxy = new Proxy(); webdriverProxy.setHttpProxy(proxyHost); webdriverProxy.setSslProxy(proxyHost); - if (!StringUtils.isAllEmpty(Neodymium.configuration().getProxySocketUsername(), Neodymium.configuration().getProxySocketPassword()) - || Neodymium.configuration().getProxySocketVersion() != null) + if (!StringUtils.isAllEmpty(Neodymium.configuration().getProxySocketUsername(), + Neodymium.configuration().getProxySocketPassword()) + || + Neodymium.configuration().getProxySocketVersion() != null) { webdriverProxy.setSocksProxy(proxyHost); if (StringUtils.isNoneEmpty(Neodymium.configuration().getProxySocketUsername(), @@ -483,11 +501,12 @@ private static String popularContentTypes() { try { - List popularContentTypes = IOUtils.readLines(BrowserConfigurationMapper.class.getResourceAsStream("/content-types.properties"), UTF_8); + final List popularContentTypes = IOUtils.readLines(BrowserConfigurationMapper.class.getResourceAsStream("/content-types.properties"), + UTF_8); popularContentTypes.add("application/x-download"); return String.join(";", popularContentTypes); } - catch (Exception e) + catch (final Exception e) { return "text/plain;text/csv;application/zip;application/pdf;application/octet-stream;" + "application/msword;application/vnd.ms-excel;text/css;text/html";