From 2bf89b377ae8a049b52805cf5e17c6fad97a3041 Mon Sep 17 00:00:00 2001 From: Maksim Petrov <47208721+vmapetr@users.noreply.github.com> Date: Wed, 6 May 2020 07:19:15 +0300 Subject: [PATCH] Improve Windows browsers provisioners stability (#725) * Improve Chrome provisioner * Improve Edge provisioner * Improve Firefox provisioner * Resolve conflicts in Install-Chrome.ps1 --- .../win/scripts/Installers/Install-Chrome.ps1 | 91 +++++++++---------- .../win/scripts/Installers/Install-Edge.ps1 | 43 +++++---- .../scripts/Installers/Install-Firefox.ps1 | 91 +++++++------------ 3 files changed, 98 insertions(+), 127 deletions(-) diff --git a/images/win/scripts/Installers/Install-Chrome.ps1 b/images/win/scripts/Installers/Install-Chrome.ps1 index c94641f1061b..22b477b28aec 100644 --- a/images/win/scripts/Installers/Install-Chrome.ps1 +++ b/images/win/scripts/Installers/Install-Chrome.ps1 @@ -3,23 +3,25 @@ ## Desc: Install Google Chrome ################################################################################ -Import-Module -Name ImageHelpers -Force; +Import-Module -Name ImageHelpers -Force -$ChromeInstallerFile = "chrome_installer.exe"; -$ChromeInstallerUrl = "https://dl.google.com/chrome/install/375.126/${ChromeInstallerFile}"; +# Download and install latest Chrome browser +$ChromeInstallerFile = "chrome_installer.exe" +$ChromeInstallerUrl = "https://dl.google.com/chrome/install/375.126/${ChromeInstallerFile}" Install-Binary -Url $ChromeInstallerUrl -Name $ChromeInstallerFile -ArgumentList ("/silent", "/install") -Write-Host "Adding the firewall rule for Google update blocking"; -New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe"; +# Prepare firewall rules +Write-Host "Adding the firewall rule for Google update blocking..." +New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe" -$GoogleSvcs = ('gupdate','gupdatem'); -$GoogleSvcs | Stop-SvcWithErrHandling -StopOnError; -$GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}; +$GoogleSvcs = ('gupdate','gupdatem') +$GoogleSvcs | Stop-SvcWithErrHandling -StopOnError +$GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"} -$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update"; -$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome"; +$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update" +$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome" ($regGoogleUpdatePath, $regGoogleUpdateChrome) | ForEach-Object { - New-Item -Path $_ -Force; + New-Item -Path $_ -Force } $regGoogleParameters = @( @@ -31,52 +33,45 @@ $regGoogleParameters = @( ) $regGoogleParameters | ForEach-Object { - $Arguments = $_; - if (-not ($Arguments.Path)) { - $Arguments.Add("Path", $regGoogleUpdatePath); + $Arguments = $_ + if (-not ($Arguments.Path)) + { + $Arguments.Add("Path", $regGoogleUpdatePath) } - $Arguments.Add("Force", $true); - New-ItemProperty @Arguments; + $Arguments.Add("Force", $true) + New-ItemProperty @Arguments } -# Reinstall Chrome Web Driver -Write-Host "Install Chrome WebDriver" -$DestinationPath = "$($env:SystemDrive)\"; -$ChromeDriverPath = "${DestinationPath}SeleniumWebDrivers\ChromeDriver"; - -if (-not (Test-Path -Path $ChromeDriverPath)) { - New-Item -Path $ChromeDriverPath -ItemType "directory" +# Install Chrome WebDriver +Write-Host "Install Chrome WebDriver..." +$ChromeDriverPath = "$($env:SystemDrive)\SeleniumWebDrivers\ChromeDriver" +if (-not (Test-Path -Path $ChromeDriverPath)) +{ + New-Item -Path $ChromeDriverPath -ItemType Directory -Force } +Write-Host "Get the Chrome WebDriver version..." $RegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths" -$ChromePath = (Get-ItemProperty "$RegistryPath\chrome.exe").'(default)'; -[version]$ChromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion; -Write-Host "Chrome version: [$ChromeVersion]"; - -$ChromeDriverVersionUri = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$($ChromeVersion.Major).$($ChromeVersion.Minor).$($ChromeVersion.Build)"; -Write-Host "Chrome driver version Uri [$ChromeDriverVersionUri]"; -Write-Host "Getting the Chrome driver version..."; -$ChromeDriverVersion = Invoke-WebRequest -Uri $ChromeDriverVersionUri; -Write-Host "Current Chrome driver version: [$ChromeDriverVersion]"; - -$ChromeDriverZipDownloadUri = "https://chromedriver.storage.googleapis.com/$($ChromeDriverVersion.ToString())/chromedriver_win32.zip"; -Write-Host "Chrome driver zip file download Uri: [$ChromeDriverZipDownloadUri]"; +$ChromePath = (Get-ItemProperty "$RegistryPath\chrome.exe").'(default)' +[version]$ChromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion +$ChromeDriverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$($ChromeVersion.Major).$($ChromeVersion.Minor).$($ChromeVersion.Build)" -$DestFile= "$ChromeDriverPath\chromedriver_win32.zip"; -$ChromeDriverVersion.Content | Out-File -FilePath "$ChromeDriverPath\versioninfo.txt" -Force; +$ChromeDriverVersionFile = Start-DownloadWithRetry -Url $ChromeDriverVersionUrl -Name "versioninfo.txt" -DownloadPath $ChromeDriverPath -Write-Host "Chrome driver download...."; -Invoke-WebRequest -Uri $ChromeDriverZipDownloadUri -OutFile $DestFile; +Write-Host "Download Chrome WebDriver..." +$ChromeDriverVersion = Get-Content -Path $ChromeDriverVersionFile +$ChromeDriverArchName = "chromedriver_win32.zip" +$ChromeDriverZipDownloadUrl = "https://chromedriver.storage.googleapis.com/${ChromeDriverVersion}/${ChromeDriverArchName}" -Write-Host "Chrome driver install...."; -Expand-Archive -Path "$ChromeDriverPath\chromedriver_win32.zip" -DestinationPath $ChromeDriverPath -Force; -Remove-Item -Path "$ChromeDriverPath\chromedriver_win32.zip" -Force; +$ChromeDriverArchPath = Start-DownloadWithRetry -Url $ChromeDriverZipDownloadUrl -Name $ChromeDriverArchName -Write-Host "Setting the environment variables" +Write-Host "Expand Chrome WebDriver archive..." +Expand-Archive -Path $ChromeDriverArchPath -DestinationPath $ChromeDriverPath -Force -setx ChromeWebDriver "$ChromeDriverPath" /M; +Write-Host "Setting the environment variables..." +setx ChromeWebDriver "$ChromeDriverPath" /M -$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'; -$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path'; -$PathValue += ";$ChromeDriverPath\"; -Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue; +$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\' +$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path' +$PathValue += ";$ChromeDriverPath\" +Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue \ No newline at end of file diff --git a/images/win/scripts/Installers/Install-Edge.ps1 b/images/win/scripts/Installers/Install-Edge.ps1 index 430e8bbb9a11..c10f24226b80 100644 --- a/images/win/scripts/Installers/Install-Edge.ps1 +++ b/images/win/scripts/Installers/Install-Edge.ps1 @@ -5,37 +5,36 @@ Choco-Install -PackageName microsoft-edge -# Install Microsoft Edge Web Driver -Write-Host "Install Edge WebDriver" -$DestinationPath = "$($env:SystemDrive)\"; - -$EdgeDriverPath = "${DestinationPath}SeleniumWebDrivers\EdgeDriver" -if (-not (Test-Path -Path $EdgeDriverPath)) { - New-Item -Path $EdgeDriverPath -ItemType "directory" +# Install Microsoft Edge WebDriver +Write-Host "Install Edge WebDriver..." +$EdgeDriverPath = "$($env:SystemDrive)\SeleniumWebDrivers\EdgeDriver" +if (-not (Test-Path -Path $EdgeDriverPath)) +{ + New-Item -Path $EdgeDriverPath -ItemType Directory -Force } +Write-Host "Get the Microsoft Edge WebDriver version..." $RegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths" $EdgePath = (Get-ItemProperty "$RegistryPath\msedge.exe").'(default)' [version]$EdgeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($EdgePath).ProductVersion $EdgeDriverVersionUrl = "https://msedgedriver.azureedge.net/LATEST_RELEASE_$($EdgeVersion.Major)" -$EdgeDriverVersionFile = "$EdgeDriverPath\versioninfo.txt" -Invoke-WebRequest -Uri $EdgeDriverVersionUrl -OutFile $EdgeDriverVersionFile -Write-Host "Microsoft Edge driver download started" +$EdgeDriverVersionFile = Start-DownloadWithRetry -Url $EdgeDriverVersionUrl -Name "versioninfo.txt" -DownloadPath $EdgeDriverPath + +Write-Host "Download Microsoft Edge WebDriver..." $EdgeDriverLatestVersion = Get-Content -Path $EdgeDriverVersionFile -$EdgeDriverDownloadUrl="https://msedgedriver.azureedge.net/${EdgeDriverLatestVersion}/edgedriver_win64.zip" -$DestFile = "$EdgeDriverPath\edgedriver_win64.zip" -Invoke-WebRequest -Uri $EdgeDriverDownloadUrl -OutFile $DestFile +$EdgeDriverArchName = "edgedriver_win64.zip" +$EdgeDriverDownloadUrl="https://msedgedriver.azureedge.net/${EdgeDriverLatestVersion}/${EdgeDriverArchName}" -Write-Host "Microsoft Edge driver installation started" -Expand-Archive -Path $DestFile -DestinationPath $EdgeDriverPath -Force -Remove-Item -Path $DestFile -Force +$EdgeDriverArchPath = Start-DownloadWithRetry -Url $EdgeDriverDownloadUrl -Name $EdgeDriverArchName -Write-Host "Setting the environment variables" +Write-Host "Expand Microsoft Edge WebDriver archive..." +Expand-Archive -Path $EdgeDriverArchPath -DestinationPath $EdgeDriverPath -Force -setx EdgeWebDriver "$EdgeDriverPath" /M; +Write-Host "Setting the environment variables..." +setx EdgeWebDriver "$EdgeDriverPath" /M -$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'; -$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path'; -$PathValue += ";$EdgeDriverPath\"; -Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue; +$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\' +$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path' +$PathValue += ";$EdgeDriverPath\" +Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue \ No newline at end of file diff --git a/images/win/scripts/Installers/Install-Firefox.ps1 b/images/win/scripts/Installers/Install-Firefox.ps1 index 2f919baa5a3d..efd3fe8b47d7 100644 --- a/images/win/scripts/Installers/Install-Firefox.ps1 +++ b/images/win/scripts/Installers/Install-Firefox.ps1 @@ -5,71 +5,48 @@ Import-Module -Name ImageHelpers -Force -$temp_install_dir = 'C:\Windows\Installer' -New-Item -Path $temp_install_dir -ItemType Directory -Force - -$versionsJson = Invoke-RestMethod "https://product-details.mozilla.org/1.0/firefox_versions.json" -$latestVersion = $versionsJson.LATEST_FIREFOX_VERSION -Write-Host "Firefox latest version: $latestVersion" - -# url for latest version of firefox -$urlLatestVersion = "https://download.mozilla.org/?product=firefox-${latestVersion}&os=win64&lang=en-US" -Install-Binary -Url $urlLatestVersion -Name "Firefox Setup $latestVersion.exe" -ArgumentList ("/silent", "/install") - -# Disable autoupdate -$firefoxDirectoryPath = Join-Path $env:ProgramFiles "Mozilla Firefox" -New-Item -path $firefoxDirectoryPath -Name 'mozilla.cfg' -Value '// +# Install and configure Firefox browser +Write-Host "Install latest Firefox browser..." +$VersionsManifest = Invoke-RestMethod "https://product-details.mozilla.org/1.0/firefox_versions.json" +$InstallerName = "firefox-browser.exe" +$InstallerUrl = "https://download.mozilla.org/?product=firefox-$($VersionsManifest.LATEST_FIREFOX_VERSION)&os=win64&lang=en-US" +$ArgumentList = ("/silent", "/install") + +Install-Binary -Url $InstallerUrl -Name $InstallerName -ArgumentList $ArgumentList + +Write-Host "Disable autoupdate..." +$FirefoxDirectoryPath = Join-Path $env:ProgramFiles "Mozilla Firefox" +New-Item -path $FirefoxDirectoryPath -Name 'mozilla.cfg' -Value '// pref("browser.shell.checkDefaultBrowser", false); pref("app.update.enabled", false);' -ItemType file -force -$firefoxPreferencesFolder = Join-Path $firefoxDirectoryPath "defaults\pref" -New-Item -path $firefoxPreferencesFolder -Name 'local-settings.js' -Value 'pref("general.config.obscure_value", 0); +$FirefoxPreferencesFolder = Join-Path $FirefoxDirectoryPath "defaults\pref" +New-Item -path $FirefoxPreferencesFolder -Name 'local-settings.js' -Value 'pref("general.config.obscure_value", 0); pref("general.config.filename", "mozilla.cfg");' -ItemType file -force -# Install Firefox gecko Web Driver -Write-Host "Install Firefox WebDriver" -$DestinationPath = "$($env:SystemDrive)\"; -$SeleniumWebDriverPath = Join-Path $DestinationPath "SeleniumWebDrivers" - -$geckodriverJson = Invoke-RestMethod "https://api.github.com/repos/mozilla/geckodriver/releases/latest" -$geckodriverWindowsAsset = $geckodriverJson.assets | Where-Object { $_.name -Match "win64" } | Select-Object -First 1 - -$geckodriverVersion = $geckodriverJson.tag_name -Write-Host "Geckodriver version: $geckodriverVersion" - -$DriversZipFile = $geckodriverWindowsAsset.name -Write-Host "Selenium drivers download and install..." - -$FirefoxDriverPath = Join-Path $SeleniumWebDriverPath "GeckoDriver" - -if (-not (Test-Path -Path $FirefoxDriverPath)) { - New-Item -Path $FirefoxDriverPath -ItemType "directory" -} - -$geckodriverVersion.Substring(1) | Out-File -FilePath "$FirefoxDriverPath\versioninfo.txt" -Force; - -# Install Firefox Web Driver -Write-Host "FireFox driver download...." -if (-not (Test-Path -Path $FireFoxDriverPath)) { - New-Item -Path $FireFoxDriverPath -ItemType "directory" +# Download and install Gecko WebDriver +Write-Host "Install Gecko WebDriver..." +$GeckoDriverPath = "$($env:SystemDrive)\SeleniumWebDrivers\GeckoDriver" +if (-not (Test-Path -Path $GeckoDriverPath)) +{ + New-Item -Path $GeckoDriverPath -ItemType Directory -Force } -$DestFile = Join-Path $FireFoxDriverPath $DriversZipFile -$FireFoxDriverDownloadUrl = $geckodriverWindowsAsset.browser_download_url -try{ - Invoke-WebRequest -Uri $FireFoxDriverDownloadUrl -OutFile $DestFile -} catch { - Write-Error "[!] Failed to download $DriversZipFile" - exit 1 -} +Write-Host "Get the Gecko WebDriver version..." +$GeckoDriverJson = Invoke-RestMethod "https://api.github.com/repos/mozilla/geckodriver/releases/latest" +$GeckoDriverWindowsAsset = $GeckoDriverJson.assets | Where-Object { $_.name -Match "win64" } | Select-Object -First 1 +$GeckoDriverVersion = $GeckoDriverJson.tag_name +$GeckoDriverVersion.Substring(1) | Out-File -FilePath "$GeckoDriverPath\versioninfo.txt" -Force; -Write-Host "FireFox driver install...." -Expand-Archive -Path $DestFile -DestinationPath $FireFoxDriverPath -Force -Remove-Item -Path $DestFile -Force +Write-Host "Download Gecko WebDriver WebDriver..." +$GeckoDriverArchName = $GeckoDriverWindowsAsset.name +$GeckoDriverDownloadUrl = $GeckoDriverWindowsAsset.browser_download_url +$GeckoDriverArchPath = Start-DownloadWithRetry -Url $GeckoDriverDownloadUrl -Name $GeckoDriverArchName -Write-Host "Setting the environment variables" -Add-MachinePathItem -PathItem $FireFoxDriverPath -setx GeckoWebDriver "$FirefoxDriverPath" /M; +Write-Host "Expand Gecko WebDriver archive..." +Expand-Archive -Path $GeckoDriverArchPath -DestinationPath $GeckoDriverPath -Force -exit 0 +Write-Host "Setting the environment variables..." +Add-MachinePathItem -PathItem $GeckoDriverPath +setx GeckoWebDriver "$GeckoDriverPath" /M \ No newline at end of file