예제 #1
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,
            }));
        }
예제 #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
        private IWebDriver SpawnOrGetWebDriverFromPool()
        {
            PoolItem webDriverItem = null;

            lock (poolLock) {
                if (!isDisposed)
                {
                    if (pool.Count() > 0)
                    {
                        // If there is a driver in the pool we can use, use it.

                        webDriverItem = pool.Dequeue();

                        OnLog.Info($"Took web driver {webDriverItem.Id} from the pool");
                    }
                    else if (spawnedDrivers.Count() < poolSize)
                    {
                        // If we haven't spawned the maximum amount of drivers yet, create a new one to use.

                        webDriverItem = new PoolItem(currentWebDriverId++, WebDriver.Create(webBrowserInfo, webDriverOptions));

                        spawnedDrivers.Add(webDriverItem);

                        OnLog.Info($"Spawned new web driver with ID {webDriverItem.Id} ({webBrowserInfo})");
                    }
                }
            }

            return(webDriverItem?.WebDriver);
        }
예제 #4
0
        private void ReleaseWebDriverInternal(IWebDriver webDriver, bool disposeWebDriver)
        {
            lock (poolLock) {
                if (!isDisposed)
                {
                    PoolItem webDriverItem = spawnedDrivers.Where(item => item.WebDriver == webDriver)
                                             .FirstOrDefault();

                    if (webDriverItem is null)
                    {
                        throw new ArgumentException(nameof(webDriver), "The given driver is not owned by this pool.");
                    }

                    if (disposeWebDriver || webDriver.HasQuit())
                    {
                        // Close and dispose of the web driver entirely.

                        if (!webDriver.HasQuit())
                        {
                            webDriver.Quit();
                        }

                        webDriver.Dispose();

                        spawnedDrivers.Remove(webDriverItem);

                        OnLog.Info($"Removed web driver {webDriverItem.Id} from the pool");
                    }
                    else
                    {
                        // Return the web driver to the pool.

                        pool.Enqueue(webDriverItem);

                        OnLog.Info($"Returned web driver {webDriverItem.Id} to the pool");
                    }

                    // Allow the next thread waiting for a web driver to continue.

                    poolAccessWaiter.Set();
                }
            }
        }
예제 #5
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);
        }