Пример #1
0
        // Protected members

        protected override Uri GetWebDriverUri(IWebBrowserInfo webBrowserInfo, IHttpWebRequestFactory webRequestFactory)
        {
            // Note that Edge and EdgeHTML use different web drivers-- MicrosoftEdgeDriver.exe and msedgedriver.exe, respectively.
            // The latest version of EdgeHTML is 18.19041, so that can be used to determine which web driver to download.
            // Currently, this method only gets web driver URIs for Edge, and not EdgeHTML.

            string edgeMajorVersionStr = webBrowserInfo.Version.Major.ToString(CultureInfo.InvariantCulture);

            // We'll get an XML document listing all web driver versions available for this version of Edge.
            // There might not be one for this specific version, so we'll pick the closest.

            using (IWebClient client = webRequestFactory.CreateWebClient()) {
                int    maxResults  = 999;
                string responseXml = client.DownloadString($"https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver?prefix={edgeMajorVersionStr}&delimiter=%2F&maxresults={maxResults}&restype=container&comp=list");

                string webDriverVersionStr = Regex.Matches(responseXml, @"<Name>([\d.]+)", RegexOptions.IgnoreCase).Cast <Match>()
                                             .Select(m => m.Groups[1].Value) // version strings
                                             .OrderByDescending(versionString => CountMatchingRevisions(versionString, edgeMajorVersionStr))
                                             .ThenByDescending(versionString => versionString, new NaturalSortComparer())
                                             .FirstOrDefault();

                if (string.IsNullOrWhiteSpace(webDriverVersionStr))
                {
                    webDriverVersionStr = edgeMajorVersionStr;
                }

                return(new Uri($"https://msedgedriver.azureedge.net/{webDriverVersionStr}/edgedriver_{GetPlatformOS()}.zip"));
            }
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
0
        // Protected members

        protected override Uri GetWebDriverUri(IWebBrowserInfo webBrowserInfo, IHttpWebRequestFactory webRequestFactory)
        {
            Uri versionUri = new Uri("https://chromedriver.storage.googleapis.com/LATEST_RELEASE");

            // If we can get the current version of Google Chrome, we can select an exact web driver version.

            Version browserVersion = webBrowserInfo?.Version;

            if (browserVersion is object)
            {
                versionUri = new Uri(versionUri.AbsoluteUri + $"_{browserVersion.Major}.{browserVersion.Minor}.{browserVersion.Build}");
            }

            using (IWebClient webClient = webRequestFactory.ToWebClientFactory().Create()) {
                // Get the latest web driver version.

                string latestVersionString = webClient.DownloadString(versionUri);

                // Create a download URL for this version.

                if (!string.IsNullOrWhiteSpace(latestVersionString))
                {
                    Uri downloadUri = new Uri(versionUri, $"/{latestVersionString}/chromedriver_{GetPlatformOS()}.zip");

                    return(downloadUri);
                }
            }

            // We weren't able to get information on the latest web driver.

            return(null);
        }
Пример #5
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);
        }
Пример #6
0
        public IWebDriver Create(IWebBrowserInfo webBrowserInfo)
        {
            if (webBrowserInfo is null)
            {
                throw new ArgumentNullException(nameof(webBrowserInfo));
            }

            if (webBrowserId != WebBrowserId.Unknown && webBrowserInfo.Id != webBrowserId)
            {
                throw new ArgumentException(string.Format(Properties.ExceptionMessages.UnsupportedWebBrowser, webBrowserInfo.Name), nameof(webBrowserInfo));
            }

            // Get the web driver executable path.

            OnLog.Info($"Creating web driver ({webBrowserInfo})");

            string webDriverExecutablePath = Path.GetFullPath(GetDriverExecutablePathInternal(webBrowserInfo));

            // Create the driver.

            return(GetWebDriver(webBrowserInfo, new WebDriverOptions(webDriverOptions)
            {
                WebDriverExecutablePath = webDriverExecutablePath,
            }));
        }
Пример #7
0
        public IWebDriver Create()
        {
            IWebBrowserInfo webBrowserInfo = webDriverFactoryOptions.DefaultWebBrowser ??
                                             (webBrowserId != WebBrowserId.Unknown ? WebBrowserInfoFactory.Default.GetInfo(webBrowserId) : WebBrowserInfoFactory.Default.GetDefaultWebBrowser());

            return(Create(webBrowserInfo));
        }
Пример #8
0
        private IWebBrowserCookieReader GetCookieReader(IWebBrowserInfo webBrowserInfo)
        {
            switch (webBrowserInfo.Id)
            {
            case WebBrowserId.Chrome:
                return(new ChromeWebBrowserCookieReader());

            case WebBrowserId.Firefox:
                return(new FirefoxWebBrowserCookieReader());

            default:
                throw new ArgumentException(nameof(webBrowserInfo));
            }
        }
Пример #9
0
        public IWebDriver Create(IWebBrowserInfo webBrowserInfo)
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(nameof(WebDriverFactory));
            }

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

            return(GetOrCreateFactory(webBrowserInfo).Create(webBrowserInfo));
        }
Пример #10
0
        public ICookiesReader Create(IWebBrowserInfo webBrowserInfo)
        {
            switch (webBrowserInfo.Id)
            {
            case WebBrowserId.Chrome:
                return(CreateChromeCookiesReader());

            case WebBrowserId.Firefox:
                return(CreateFirefoxCookiesReader());

            default:
                throw new ArgumentOutOfRangeException(nameof(webBrowserInfo));
            }
        }
Пример #11
0
        private bool DownloadWebDriver(IWebBrowserInfo webBrowserInfo, CancellationToken cancellationToken)
        {
            string webDriverExecutablePath = GetWebDriverExecutablePathInternal();

            OnLog.Info("Getting web driver download url");

            Uri    webDriverDownloadUri = GetWebDriverUri(webBrowserInfo, webRequestFactory);
            string downloadFilePath     = PathUtilities.SetFileExtension(Path.GetTempFileName(), ".zip");

            if (webDriverDownloadUri is object)
            {
                OnLog.Info($"Downloading {webDriverDownloadUri}");

                using (IWebClient webClient = webRequestFactory.ToWebClientFactory().Create()) {
                    webClient.DownloadProgressChanged += (sender, e) => OnDownloadFileProgressChanged(this, new DownloadFileProgressChangedEventArgs(webDriverDownloadUri, downloadFilePath, e));
                    webClient.DownloadFileCompleted   += (sender, e) => OnDownloadFileCompleted(this, new DownloadFileCompletedEventArgs(webDriverDownloadUri, downloadFilePath, e.Error is null));

                    webClient.DownloadFileSync(webDriverDownloadUri, downloadFilePath, cancellationToken);
                }

                try {
                    string filePathInArchive = PathUtilities.GetFilename(webDriverExecutablePath);

                    OnLog.Info($"Extracting {filePathInArchive}");

                    ArchiveUtilities.ExtractFile(downloadFilePath, filePathInArchive, webDriverExecutablePath);
                }
                catch (Exception ex) {
                    OnLog.Error(ex.ToString());

                    throw ex;
                }
                finally {
                    File.Delete(downloadFilePath);
                }

                // We were able to successfully update the web driver.

                return(true);
            }

            // We were not able to successfully update the web driver.

            return(false);
        }
Пример #12
0
        // 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);
        }
        // Protected members

        protected override Uri GetWebDriverUri(IWebBrowserInfo webBrowserInfo, IHttpWebRequestFactory webRequestFactory)
        {
            string releasesUrl = "https://github.com/mozilla/geckodriver/releases/latest";

            IGitHubClient gitHubClient = new GitHubWebClient(webRequestFactory);
            IRelease      release      = gitHubClient.GetLatestRelease(releasesUrl);

            IReleaseAsset asset = release.Assets.Where(a => a.Name.Contains(GetPlatformOS()))
                                  .FirstOrDefault();

            if (!string.IsNullOrWhiteSpace(asset?.DownloadUrl))
            {
                return(new Uri(asset.DownloadUrl));
            }

            // We weren't able to get information on the latest web driver.

            return(null);
        }
Пример #14
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);
        }
Пример #15
0
        // Public members

        public IWebDriverInfo Update(IWebBrowserInfo webBrowserInfo, CancellationToken cancellationToken)
        {
            if (webBrowserInfo is null)
            {
                throw new ArgumentNullException(nameof(webBrowserInfo));
            }

            if (!IsSupportedWebBrowser(webBrowserInfo))
            {
                throw new ArgumentException(string.Format(Properties.ExceptionMessages.UnsupportedWebBrowser, webBrowserInfo.Name), nameof(webBrowserInfo));
            }

            OnLog.Info($"Checking for web driver updates");

            IWebDriverInfo webDriverInfo = GetWebDriverInfo();

            bool updateRequired = (!webDriverInfo.Version?.Equals(webBrowserInfo.Version) ?? true) ||
                                  !File.Exists(webDriverInfo.ExecutablePath);

            if (updateRequired)
            {
                OnLog.Info($"Updating web driver to version {webBrowserInfo.Version}");

                if (DownloadWebDriver(webBrowserInfo, cancellationToken))
                {
                    webDriverInfo = new WebDriverInfo()
                    {
                        ExecutablePath = GetWebDriverExecutablePathInternal(),
                        Version        = webBrowserInfo.Version
                    };

                    SaveWebDriverInfo(webDriverInfo);
                }
            }
            else
            {
                OnLog.Info($"Web driver is up to date ({webBrowserInfo.Version})");
            }

            return(webDriverInfo);
        }
Пример #16
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.");
            }
        }
Пример #17
0
        public IWebDriverUpdater Create(IWebBrowserInfo webBrowserInfo)
        {
            if (webBrowserInfo is null)
            {
                throw new ArgumentNullException(nameof(webBrowserInfo));
            }

            switch (webBrowserInfo.Id)
            {
            case WebBrowserId.Chrome:
                return(new ChromeWebDriverUpdater(webRequestFactory, webDriverUpdaterOptions));

            case WebBrowserId.Edge:
                return(new EdgeWebDriverUpdater(webRequestFactory, webDriverUpdaterOptions));

            case WebBrowserId.Firefox:
                return(new FirefoxWebDriverUpdater(webRequestFactory, webDriverUpdaterOptions));

            default:
                throw new ArgumentException(string.Format(Properties.ExceptionMessages.UnsupportedWebBrowser, webBrowserInfo.Name), nameof(webBrowserInfo));
            }
        }
Пример #18
0
        private string GetDriverExecutablePathInternal(IWebBrowserInfo webBrowserInfo)
        {
            if (!string.IsNullOrWhiteSpace(webDriverOptions.WebDriverExecutablePath))
            {
                return(webDriverOptions.WebDriverExecutablePath);
            }

            if (webBrowserInfo is object && webDriverFactoryOptions.AutoUpdateEnabled)
            {
                IWebDriverUpdater updater = GetUpdaterInternal();

                if (updater is object)
                {
                    try {
                        IWebDriverInfo webDriverInfo = updater.Update(webBrowserInfo, updaterCancellationTokenSource.Token);

                        if (!string.IsNullOrWhiteSpace(webDriverInfo?.ExecutablePath))
                        {
                            return(webDriverInfo.ExecutablePath);
                        }
                    }
                    catch (Exception ex) {
                        OnLog.Error(ex.ToString());

                        if (!webDriverFactoryOptions.IgnoreUpdateErrors)
                        {
                            throw ex;
                        }
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(webDriverFactoryOptions.WebDriverDirectoryPath))
            {
                return(GetWebDriverExecutablePath());
            }

            return(Path.Combine(webDriverFactoryOptions.WebDriverDirectoryPath, GetWebDriverExecutablePath()));
        }
Пример #19
0
        private IWebDriverFactory GetOrCreateFactory(IWebBrowserInfo webBrowserInfo)
        {
            IWebDriverFactory factory = null;

            lock (factoryDict) {
                if (!factoryDict.TryGetValue(webBrowserInfo.Id, out factory))
                {
                    switch (webBrowserInfo.Id)
                    {
                    case WebBrowserId.Chrome:
                        factoryDict[webBrowserInfo.Id] = new ChromeWebDriverFactory(webDriverOptions, webDriverFactoryOptions);
                        break;

                    case WebBrowserId.Edge:
                        factoryDict[webBrowserInfo.Id] = new EdgeWebDriverFactory(webDriverOptions, webDriverFactoryOptions);
                        break;

                    case WebBrowserId.Firefox:
                        factoryDict[webBrowserInfo.Id] = new FirefoxWebDriverFactory(webDriverOptions, webDriverFactoryOptions);
                        break;

                    default:
                        throw new ArgumentException(string.Format(Properties.ExceptionMessages.UnsupportedWebBrowser, webBrowserInfo.Name), nameof(webBrowserInfo));
                    }

                    factory = factoryDict[webBrowserInfo.Id];

                    factory.Log += Log;

                    factory.DownloadFileCompleted       += DownloadFileCompleted;
                    factory.DownloadFileProgressChanged += DownloadFileProgressChanged;
                }
            }

            return(factory);
        }
Пример #20
0
        public static IWebBrowserInfo GetDefaultWebBrowserInfo()
        {
            // From Windows 8 forward, this seems to be the most reliable location for finding the default browser.
            // https://stackoverflow.com/a/17599201

            IWebBrowserInfo webBrowserInfo = GetWebBrowserInfoFromUserChoiceKey();

            // For Windows 7 and previous, the UserChoice key may be empty.
            // https://stackoverflow.com/a/56707674

            if (webBrowserInfo is null)
            {
                webBrowserInfo = GetWebBrowserInfoFromClassesRootCommandKey();
            }

            // Default to Internet Explorer if we can't find the default web browser.

            if (webBrowserInfo is null)
            {
                webBrowserInfo = GetWebBrowserInfo(WebBrowserId.InternetExplorer);
            }

            return(webBrowserInfo);
        }
Пример #21
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);
        }
Пример #22
0
        public static WebHeaderCollection GetWebBrowserRequestHeaders(IWebBrowserInfo webBrowserInfo, TimeSpan timeout, string responseBody)
        {
            WebHeaderCollection requestHeaders = new WebHeaderCollection();

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

            if (!File.Exists(webBrowserInfo?.ExecutablePath))
            {
                throw new FileNotFoundException();
            }

            // Start an HTTP server on a random port.

            Uri  requestUri        = new Uri($"http://localhost:{SocketUtilities.GetUnusedPort()}/");
            bool listenForRequests = true;

            using (HttpListener listener = new HttpListener()) {
                listener.Prefixes.Add(requestUri.AbsoluteUri);

                listener.Start();

                // Open the HTTP server in the user's web browser.

                Process.Start(webBrowserInfo.ExecutablePath, requestUri.AbsoluteUri);

                // Wait for an incoming request.

                while (listenForRequests)
                {
                    listenForRequests = false;

                    HttpListenerContext context = listener.GetContext(timeout);

                    if (!(context is null))
                    {
                        HttpListenerRequest request = context.Request;

                        if (request.HttpMethod.Equals("get", StringComparison.OrdinalIgnoreCase))
                        {
                            // The web browser requested our webpage, so copy the request headers.

                            context.Request.Headers.CopyTo(requestHeaders);

                            // Respond to the request.

                            HttpListenerResponse response = context.Response;

                            StringBuilder responseBuilder = new StringBuilder();

                            responseBuilder.AppendLine("<!DOCTYPE html>");
                            responseBuilder.Append("<html>");
                            responseBuilder.Append("<body>");
                            responseBuilder.Append("<script>fetch(\"" + requestUri.AbsoluteUri + "\", {method: \"POST\"});</script>");
                            responseBuilder.Append(responseBody ?? "This window may now be closed.");
                            responseBuilder.Append("</body>");
                            responseBuilder.Append("</html>");

                            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(responseBuilder.ToString())))
                                ms.CopyTo(response.OutputStream);

                            response.Close();

                            // Wait for the web browser to POST back let us know it got our response.

                            listenForRequests = true;
                        }
                        else
                        {
                            // The web browser has POSTed back to let us know it got our response.
                        }
                    }
                }
            }

            return(requestHeaders);
        }
Пример #23
0
 public static WebHeaderCollection GetWebBrowserRequestHeaders(IWebBrowserInfo webBrowserInfo, TimeSpan timeout)
 {
     return(GetWebBrowserRequestHeaders(webBrowserInfo, timeout, null));
 }
Пример #24
0
        // Public members

        public static WebHeaderCollection GetWebBrowserRequestHeaders(IWebBrowserInfo webBrowserInfo)
        {
            return(GetWebBrowserRequestHeaders(webBrowserInfo, TimeSpan.FromSeconds(10)));
        }
Пример #25
0
        // Public members

        public WebBrowserCookieReader(IWebBrowserInfo webBrowserInfo)
        {
            this.webBrowserInfo = webBrowserInfo;
        }
Пример #26
0
 protected bool IsSupportedWebBrowser(IWebBrowserInfo webBrowserInfo)
 {
     return(webBrowserId == WebBrowserId.Unknown ||
            webBrowserId.Equals(webBrowserInfo.Id));
 }
Пример #27
0
 protected abstract IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions);
Пример #28
0
 protected abstract Uri GetWebDriverUri(IWebBrowserInfo webBrowserInfo, IHttpWebRequestFactory webRequestFactory);
Пример #29
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);
        }
Пример #30
0
        // Protected members

        protected override IWebDriver GetWebDriver(IWebBrowserInfo webBrowserInfo, IWebDriverOptions webDriverOptions)
        {
            return(GetWebDriverInternal(webBrowserInfo, webDriverOptions, overriddenUserAgent: string.Empty));
        }