Exemple #1
0
        // Public members

        public static IWebDriver Create(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            if (webBrowserInfo is null)
            {
                throw new ArgumentNullException(nameof(webBrowserInfo));
            }

            if (webDriverOptions is null)
            {
                throw new ArgumentNullException(nameof(webDriverOptions));
            }

            IWebDriver result;

            switch (webBrowserInfo.Id)
            {
            case WebBrowserId.Chrome:
                result = CreateChromeWebDriver(webBrowserInfo, webDriverOptions);
                break;

            case WebBrowserId.Firefox:
                result = CreateFirefoxWebDriver(webBrowserInfo, webDriverOptions);
                break;

            default:
                throw new ArgumentException("The given web browser is not supported.", nameof(webBrowserInfo));
            }

            result.Manage().Window.Position = webDriverOptions.WindowPosition;

            return(result);
        }
Exemple #2
0
        // Private members

        private IWebDriver GetWebDriverInternal(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions, string overriddenUserAgent)
        {
            string webDriverDirectoryPath = Path.GetDirectoryName(webDriverOptions.WebDriverExecutablePath);

            ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(webDriverDirectoryPath);

            ConfigureDriverService(driverService);

            ChromeOptions driverOptions = new ChromeOptions {
                BinaryLocation = webBrowserInfo.ExecutablePath
            };

            ConfigureDriverOptions(driverOptions, webDriverOptions, overriddenUserAgent);

            IWebDriver driver = new ChromeDriver(driverService, driverOptions);

            ConfigureDriver(driver as ChromeDriver, webDriverOptions);

            if (string.IsNullOrEmpty(overriddenUserAgent))
            {
                driver = OverrideHeadlessUserAgent(driver, webBrowserInfo, webDriverOptions);
            }

            return(driver);
        }
Exemple #3
0
        private void ConfigureDriverOptions(ChromeOptions options, IWebDriverOptions webDriverOptions, string overriddenUserAgent)
        {
            if (webDriverOptions.Headless)
            {
                options.AddArgument("--headless");
            }

            string userAgent = !string.IsNullOrEmpty(webDriverOptions.UserAgent) ?
                               webDriverOptions.UserAgent :
                               overriddenUserAgent;

            if (!string.IsNullOrEmpty(userAgent))
            {
                options.AddArgument($"--user-agent={userAgent}");
            }

            if (!webDriverOptions.Proxy.IsEmpty())
            {
                options.AddArgument($"--proxy-server={webDriverOptions.Proxy.ToProxyString()}");
            }

            options.PageLoadStrategy = (OpenQA.Selenium.PageLoadStrategy)webDriverOptions.PageLoadStrategy;

            // Resize the window to a reasonable resolution so that viewport matches a conventional monitor viewport.

            options.AddArgument($"--window-size={webDriverOptions.WindowSize.Width},{webDriverOptions.WindowSize.Height}");
            options.AddArgument($"--window-position={webDriverOptions.WindowPosition.X},{webDriverOptions.WindowPosition.Y}");

            // Disable the "navigator.webdriver" property.

            if (webDriverOptions.Stealth)
            {
                options.AddArgument("--disable-blink-features=AutomationControlled");
            }
        }
Exemple #4
0
        private void ConfigureDriverOptions(FirefoxOptions options, IWebDriverOptions webDriverOptions)
        {
            if (webDriverOptions.Headless)
            {
                options.AddArgument("--headless");
            }

            // Apply user agent.

            FirefoxProfile profile = new FirefoxProfile {
                DeleteAfterUse = true
            };

            if (!string.IsNullOrEmpty(webDriverOptions.UserAgent))
            {
                profile.SetPreference("general.useragent.override", webDriverOptions.UserAgent);
            }

            // If the user specified a proxy, apply the proxy.

            if (!webDriverOptions.Proxy.IsEmpty())
            {
                string proxyAbsoluteUri = webDriverOptions.Proxy.ToProxyString();

                Proxy proxy = new Proxy {
                    HttpProxy = proxyAbsoluteUri,
                    SslProxy  = proxyAbsoluteUri
                };

                options.Proxy = proxy;
            }

            if (webDriverOptions.DisablePopUps)
            {
                profile.SetPreference("dom.popup_allowed_events", "");
            }

            options.Profile = profile;

            options.PageLoadStrategy = (OpenQA.Selenium.PageLoadStrategy)webDriverOptions.PageLoadStrategy;

            // Resize the window to a reasonable resolution so that viewport matches a conventional monitor viewport.

            options.AddArguments($"--width={webDriverOptions.WindowSize.Width}");
            options.AddArguments($"--height={webDriverOptions.WindowSize.Height}");

            // Disable the "navigator.webdriver" property.

            if (webDriverOptions.Stealth)
            {
                profile.SetPreference("dom.webdriver.enabled", false);
            }
        }
Exemple #5
0
        public WebDriverOptions(IWebDriverOptions other)
        {
            this.Uri       = other.Uri;
            this.Proxy     = other.Proxy;
            this.Timeout   = other.Timeout;
            this.UserAgent = other.UserAgent;
            this.WebDriverExecutablePath = other.WebDriverExecutablePath;
            this.Headless       = other.Headless;
            this.WindowPosition = other.WindowPosition;
            this.WindowSize     = other.WindowSize;

            this.PageLoadStrategy = other.PageLoadStrategy;
            this.DisablePopUps    = other.DisablePopUps;
            this.Stealth          = other.Stealth;
        }
        // Protected members

        protected override IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            string webDriverDirectoryPath = Path.GetDirectoryName(webDriverOptions.WebDriverExecutablePath);

            EdgeDriverService driverService = EdgeDriverService.CreateDefaultService(webDriverDirectoryPath);

            ConfigureDriverService(driverService);

            EdgeOptions driverOptions = new EdgeOptions();

            //ConfigureDriverOptions(driverOptions);

            EdgeDriver driver = new EdgeDriver(driverService, driverOptions);

            //ConfigureDriver(driver);

            return(driver);
        }
Exemple #7
0
        // Protected members

        protected override IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            string webDriverDirectoryPath = Path.GetDirectoryName(webDriverOptions.WebDriverExecutablePath);

            FirefoxDriverService driverService = FirefoxDriverService.CreateDefaultService(webDriverDirectoryPath);

            ConfigureDriverService(driverService);

            FirefoxOptions driverOptions = new FirefoxOptions {
                BrowserExecutableLocation = webBrowserInfo.ExecutablePath
            };

            ConfigureDriverOptions(driverOptions, webDriverOptions);

            FirefoxDriver driver = new FirefoxDriver(driverService, driverOptions);

            ConfigureDriver(driver, webDriverOptions);

            return(driver);
        }
Exemple #8
0
        // Public members

        public WebDriverPool(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions, int poolSize)
        {
            if (webBrowserInfo is null)
            {
                throw new ArgumentNullException(nameof(webBrowserInfo));
            }

            if (webDriverOptions is null)
            {
                throw new ArgumentNullException(nameof(webDriverOptions));
            }

            this.webBrowserInfo   = webBrowserInfo;
            this.webDriverOptions = webDriverOptions;
            this.poolSize         = poolSize;

            if (poolSize < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(poolSize), "The pool size must be at least 1.");
            }
        }
Exemple #9
0
        public WebDriverFactory(IHttpWebRequestFactory webRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions)
        {
            if (webDriverOptions is null)
            {
                throw new ArgumentNullException(nameof(webDriverOptions));
            }

            if (webDriverFactoryOptions is null)
            {
                throw new ArgumentNullException(nameof(webDriverFactoryOptions));
            }

            if (webRequestFactory is null)
            {
                throw new ArgumentNullException(nameof(webRequestFactory));
            }

            this.webRequestFactory       = webRequestFactory;
            this.webDriverOptions        = webDriverOptions;
            this.webDriverFactoryOptions = webDriverFactoryOptions;
        }
Exemple #10
0
        private void ConfigureDriver(ChromeDriver driver, IWebDriverOptions webDriverOptions)
        {
            if (webDriverOptions.Stealth)
            {
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.utils);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.chrome_app);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.chrome_runtime);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.iframe_contentWindow);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.media_codecs);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_languages);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_permissions);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_plugins);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_vendor);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_webdriver);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.webgl_vendor);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.window_outerdimensions);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_hardwareConcurrency);
                //driver.AddScriptToEvaluateOnNewDocument("(() => { utils = undefined; })();");
            }

            driver.Manage().Window.Position = webDriverOptions.WindowPosition;
        }
Exemple #11
0
        private static IWebDriver CreateChromeWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            string webDriverExecutablePath = GetFullWebDriverExecutablePath(webDriverOptions.WebDriverExecutablePath);

            // Create the driver service.

            ChromeOptions driverOptions = new ChromeOptions {
                BinaryLocation = webBrowserInfo.ExecutablePath
            };

            ChromeDriverService driverService = string.IsNullOrEmpty(webDriverExecutablePath) ?
                                                ChromeDriverService.CreateDefaultService() :
                                                ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(webDriverExecutablePath));

            driverService.HideCommandPromptWindow = true;

            if (webDriverOptions.Headless)
            {
                driverOptions.AddArgument("--headless");
            }

            if (!string.IsNullOrEmpty(webDriverOptions.UserAgent))
            {
                driverOptions.AddArgument($"--user-agent={webDriverOptions.UserAgent}");
            }

            if (!webDriverOptions.Proxy.IsEmpty())
            {
                driverOptions.AddArgument($"--proxy-server={webDriverOptions.Proxy.GetProxyString()}");
            }

            driverOptions.PageLoadStrategy = (OpenQA.Selenium.PageLoadStrategy)webDriverOptions.PageLoadStrategy;

            // Resize the window to a reasonable resolution so that viewport matches a conventional monitor viewport.

            driverOptions.AddArgument($"--window-size={webDriverOptions.WindowSize.Width},{webDriverOptions.WindowSize.Height}");
            driverOptions.AddArgument($"--window-position={webDriverOptions.WindowPosition.X},{webDriverOptions.WindowPosition.Y}");

            // Disable the "navigator.webdriver" property.

            driverOptions.AddArgument("--disable-blink-features=AutomationControlled");

            ChromeDriver driver = new ChromeDriver(driverService, driverOptions);

            if (webDriverOptions.Stealth)
            {
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.utils);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.chrome_app);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.chrome_runtime);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.iframe_contentWindow);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.media_codecs);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_languages);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_permissions);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_plugins);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_vendor);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_webdriver);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.webgl_vendor);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.window_outerdimensions);
                driver.AddScriptToEvaluateOnNewDocument(Properties.Resources.navigator_hardwareConcurrency);
                //driver.AddScriptToEvaluateOnNewDocument("(() => { utils = undefined; })();");
            }

            return(driver);
        }
Exemple #12
0
 public WebDriverFactory(IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
     this(HttpWebRequestFactory.Default, webDriverOptions, webDriverFactoryOptions)
 {
 }
Exemple #13
0
 protected WebDriverFactoryBase(WebBrowserId webBrowserId, IHttpWebRequestFactory httpWebRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions)
 {
     this.webBrowserId            = webBrowserId;
     this.httpWebRequestFactory   = httpWebRequestFactory;
     this.webDriverOptions        = webDriverOptions;
     this.webDriverFactoryOptions = webDriverFactoryOptions;
 }
Exemple #14
0
 protected abstract IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions);
Exemple #15
0
 private void ConfigureDriver(FirefoxDriver driver, IWebDriverOptions webDriverOptions)
 {
     driver.Manage().Window.Position = webDriverOptions.WindowPosition;
 }
Exemple #16
0
 public FirefoxWebDriverFactory(IWebDriverOptions webDriverOptions) :
     this(webDriverOptions, WebDriverFactoryOptions.Default)
 {
 }
Exemple #17
0
 public FirefoxWebDriverFactory(IHttpWebRequestFactory webRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
     base(WebBrowserId.Firefox, webRequestFactory, webDriverOptions, webDriverFactoryOptions)
 {
 }
 public EdgeWebDriverFactory(IHttpWebRequestFactory webRequestFactory, IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
     base(WebBrowserId.Edge, webRequestFactory, webDriverOptions, webDriverFactoryOptions)
 {
 }
 public EdgeWebDriverFactory(IWebDriverOptions webDriverOptions) :
     this(webDriverOptions, WebDriverFactoryOptions.Default)
 {
 }
 public EdgeWebDriverFactory(IWebDriverOptions webDriverOptions, IWebDriverFactoryOptions webDriverFactoryOptions) :
     this(new HttpWebRequestFactory(), webDriverOptions, webDriverFactoryOptions)
 {
 }
Exemple #21
0
        private IWebDriver OverrideHeadlessUserAgent(IWebDriver webDriver, IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            if (webDriverOptions.Stealth && webDriverOptions.Headless && string.IsNullOrWhiteSpace(webDriverOptions.UserAgent))
            {
                string userAgent = webDriver.GetUserAgent();

                // The user agent will contain the string "HeadlessChrome" when using headless mode.

                if (userAgent.Contains("HeadlessChrome/"))
                {
                    OnLog.Info("HeadlessChrome detected; patching user agent");

                    string newUserAgent = userAgent.Replace("HeadlessChrome/", "Chrome/");

                    // Recreate the web driver using the new user agent string.

                    webDriver.Quit();
                    webDriver.Dispose();

                    return(GetWebDriverInternal(webBrowserInfo, webDriverOptions, newUserAgent));
                }
            }

            // Return the web driver unmodified.

            return(webDriver);
        }
Exemple #22
0
        // Protected members

        protected override IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            return(GetWebDriverInternal(webBrowserInfo, webDriverOptions, overriddenUserAgent: string.Empty));
        }
Exemple #23
0
        private static IWebDriver CreateFirefoxWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            string webDriverExecutablePath = GetFullWebDriverExecutablePath(webDriverOptions.WebDriverExecutablePath);

            // Create the driver service.

            FirefoxOptions driverOptions = new FirefoxOptions {
                BrowserExecutableLocation = webBrowserInfo.ExecutablePath
            };

            FirefoxDriverService driverService = string.IsNullOrEmpty(webDriverExecutablePath) ?
                                                 FirefoxDriverService.CreateDefaultService() :
                                                 FirefoxDriverService.CreateDefaultService(Path.GetDirectoryName(webDriverExecutablePath));

            driverService.HideCommandPromptWindow = true;

            if (webDriverOptions.Headless)
            {
                driverOptions.AddArgument("--headless");
            }

            // Apply user agent.

            FirefoxProfile profile = new FirefoxProfile {
                DeleteAfterUse = true
            };

            if (!string.IsNullOrEmpty(webDriverOptions.UserAgent))
            {
                profile.SetPreference("general.useragent.override", webDriverOptions.UserAgent);
            }

            // If the user specified a proxy, apply the proxy.

            if (webDriverOptions.Proxy.IsEmpty())
            {
                string proxyAbsoluteUri = webDriverOptions.Proxy.GetProxyString();

                Proxy proxy = new Proxy {
                    HttpProxy = proxyAbsoluteUri,
                    SslProxy  = proxyAbsoluteUri
                };

                driverOptions.Proxy = proxy;
            }

            if (webDriverOptions.DisablePopUps)
            {
                profile.SetPreference("dom.popup_allowed_events", "");
            }

            driverOptions.Profile = profile;

            driverOptions.PageLoadStrategy = (OpenQA.Selenium.PageLoadStrategy)webDriverOptions.PageLoadStrategy;

            // Resize the window to a reasonable resolution so that viewport matches a conventional monitor viewport.

            driverOptions.AddArguments($"--width={webDriverOptions.WindowSize.Width}");
            driverOptions.AddArguments($"--height={webDriverOptions.WindowSize.Height}");

            // Disable the "navigator.webdriver" property.

            profile.SetPreference("dom.webdriver.enabled", false);

            IWebDriver driver = new FirefoxDriver(driverService, driverOptions);

            return(driver);
        }