From 0d6f1ea0a15e83b330db26c0572689a746ba513a Mon Sep 17 00:00:00 2001 From: mingyaulee Date: Sun, 17 Sep 2023 06:58:55 +0800 Subject: [PATCH] Fallback latest version in powershell script --- lib/setup-chromedriver.ps1 | 40 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/setup-chromedriver.ps1 b/lib/setup-chromedriver.ps1 index 486e9afa..6bf17693 100644 --- a/lib/setup-chromedriver.ps1 +++ b/lib/setup-chromedriver.ps1 @@ -2,6 +2,7 @@ Param( [string]$version ) +$json_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json" $chrome_fullversion = (Get-Item "C:\Program Files\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion Write-Output "Chrome version: $chrome_fullversion" @@ -13,6 +14,7 @@ else { $chrome_majorversion = $version.Split(".")[0] } + if($chrome_majorversion -lt 115) { $response = Invoke-WebRequest "http://chromedriver.storage.googleapis.com/LATEST_RELEASE_$chrome_majorversion" @@ -20,18 +22,32 @@ if($chrome_majorversion -lt 115) Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$version/chromedriver_win32.zip" -OutFile chromedriver_win32.zip Expand-Archive -Path chromedriver_win32.zip -DestinationPath C:\SeleniumWebDrivers\ChromeDriver -Force Remove-Item chromedriver_win32.zip + Return 0 } -else + +if([string]::IsNullOrEmpty($version)) +{ + $version = $chrome_fullversion +} + +$arch = "win32" +Write-Output $arch + +Write-Output "Downloading $json_url..." +$json = Invoke-WebRequest $json_url -UseBasicParsing | ConvertFrom-Json +$url = $json | Select-Object -ExpandProperty versions | Where-Object { $_.version -eq $version } | Select-Object -ExpandProperty downloads | Select-Object -ExpandProperty chromedriver | Where-Object { $_.platform -eq $arch } | Select-Object -ExpandProperty url +if (!$url) { - if([string]::IsNullOrEmpty($version)) - { - $version = $chrome_fullversion - } - $arch = "win32" - Write-Output $arch - $url = Invoke-WebRequest "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json" -UseBasicParsing | ConvertFrom-Json | Select-Object -ExpandProperty versions | Where-Object { $_.version -eq $version } | Select-Object -ExpandProperty downloads | Select-Object -ExpandProperty chromedriver | Where-Object { $_.platform -eq $arch } | Select-Object -ExpandProperty url - Invoke-WebRequest $url -OutFile chromedriver-win32.zip - Expand-Archive -Path chromedriver-win32.zip -Force - Move-Item -Path .\chromedriver-win32\chromedriver-win32\chromedriver.exe -Destination C:\SeleniumWebDrivers\ChromeDriver -Force - Remove-Item chromedriver-win32.zip + Write-Output "Falling back to latest version of ChromeDriver for $arch" + $version3 = $version.Substring(0, $version.LastIndexOf('.')) + Write-Output "VERSION3 = $version3" + $version = $json | ConvertFrom-Json | Select-Object -ExpandProperty versions | Where-Object { $_.version -like "$version3.*" } | Select-Object -Last 1 -ExpandProperty version + Write-Output "VERSION = $version" + $url = $json | Select-Object -ExpandProperty versions | Where-Object { $_.version -eq $version } | Select-Object -ExpandProperty downloads | Select-Object -ExpandProperty chromedriver | Where-Object { $_.platform -eq $arch } | Select-Object -ExpandProperty url } +Write-Output "Installing ChromeDriver $version for $arch" +Write-Output "Downloading $url..." +Invoke-WebRequest $url -OutFile chromedriver-win32.zip +Expand-Archive -Path chromedriver-win32.zip -Force +Move-Item -Path .\chromedriver-win32\chromedriver-win32\chromedriver.exe -Destination C:\SeleniumWebDrivers\ChromeDriver -Force +Remove-Item chromedriver-win32.zip