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