Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#292] Bug: Selenium Manager is downloaded even if both driver and browser are in PATH #293

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
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;
import org.openqa.selenium.firefox.FirefoxOptions;
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;
Expand Down Expand Up @@ -178,15 +181,15 @@ private static FirefoxBinary createFirefoxBinary(final String pathToBrowser)
* if <a href="https://github.com/Xceptance/neodymium-library/wiki/Selenium-grid">Selenium grid</a> 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();
final WebDriverStateContainer wDSC = new WebDriverStateContainer();
SelenideProxyServer selenideProxyServer = null;
if (Neodymium.configuration().useLocalProxy())
{
BrowserUpProxy proxy = setupEmbeddedProxy();
final BrowserUpProxy proxy = setupEmbeddedProxy();

// set the Proxy for later usage
wDSC.setProxy(proxy);
Expand All @@ -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();
Expand All @@ -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<String, Object> prefs = new HashMap<>();
final HashMap<String, Object> prefs = new HashMap<>();

// if we have configured prefs, we need to add all to the experimental options
if (config.getPreferences() != null && !config.getPreferences().isEmpty())
Expand All @@ -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())
{
Expand All @@ -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())
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
Expand All @@ -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<WebDriver>(new NeodymiumWebDriverListener()).decorate(wDSC.getWebDriver());
final WebDriver decoratedDriver = new EventFiringDecorator<WebDriver>(new NeodymiumWebDriverListener()).decorate(wDSC.getWebDriver());
wDSC.setDecoratedWebDriver(decoratedDriver);
WebDriverRunner.setWebDriver(decoratedDriver, selenideProxyServer);
return wDSC;
Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -483,11 +501,12 @@ private static String popularContentTypes()
{
try
{
List<String> popularContentTypes = IOUtils.readLines(BrowserConfigurationMapper.class.getResourceAsStream("/content-types.properties"), UTF_8);
final List<String> 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";
Expand Down