예제 #1
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);
        }
        // Public members

        public IWebDriverInfo GetLatestWebDriver(string webDriverFilePath)
        {
            // Check the cache for information on this web driver.

            IWebDriverInfo webDriverInfo = cache?.GetWebDriverInfo(webDriverFilePath);

            bool updateRequired = !File.Exists(webDriverFilePath) ||
                                  webDriverInfo is null ||
                                  (DateTimeOffset.Now - webDriverInfo.LastUpdated).TotalDays <= 1.0;

            if (updateRequired)
            {
                // Get info about the latest web driver version.

                try {
                    IWebDriverInfo latestWebDriverInfo = GetLatestWebDriverInfo();

                    // If this is the same version as the one we already have, we won't download it again.

                    if (!(latestWebDriverInfo?.DownloadUri is null) && (webDriverInfo is null || webDriverInfo?.Version < latestWebDriverInfo?.Version))
                    {
                        // Download the updated web driver.

                        string downloadFilePath = Path.GetTempFileName();
                        string downloadFileExt  = PathUtilities.GetFileExtension(latestWebDriverInfo.DownloadUri.AbsoluteUri);

                        using (WebClient webClient = webRequestFactory.ToWebClientFactory().Create())
                            webClient.DownloadFile(latestWebDriverInfo.DownloadUri, downloadFilePath);

                        try {
                            if (downloadFileExt.Equals(".zip", StringComparison.OrdinalIgnoreCase))
                            {
                                // If this is a .zip archive, extract the desired file.

                                string filename = PathUtilities.GetFileName(webDriverFilePath);

                                using (IArchive archive = Archive.OpenRead(downloadFilePath))
                                    using (FileStream outputStream = File.OpenWrite(webDriverFilePath))
                                        archive.ExtractEntry(archive.GetEntry(filename), outputStream);
                            }
                            else if (downloadFileExt.Equals(".exe", StringComparison.OrdinalIgnoreCase))
                            {
                                // Otherwise, attempt to replace the old web driver.

                                File.Copy(downloadFilePath, webDriverFilePath, overwrite: true);
                            }

                            webDriverInfo = latestWebDriverInfo;
                        }
                        finally {
                            // Delete the downloaded file.

                            File.Delete(downloadFilePath);
                        }
                    }
                }
                finally {
                    if (File.Exists(webDriverFilePath))
                    {
                        // Update the web driver info.

                        webDriverInfo = new WebDriverInfo(webDriverInfo)
                        {
                            Md5Hash     = FileUtilities.CalculateMD5Hash(webDriverFilePath),
                            LastUpdated = DateTimeOffset.Now,
                        };

                        // Update the cache.

                        if (!(cache is null))
                        {
                            cache.AddWebDriverInfo(webDriverInfo);
                        }
                    }
                }
            }

            return(webDriverInfo);
        }