diff --git a/scripts/Common/Scripts/BrowserAutomation.ps1 b/scripts/Common/Scripts/BrowserAutomation.ps1 index 47547c48..b51a1901 100644 --- a/scripts/Common/Scripts/BrowserAutomation.ps1 +++ b/scripts/Common/Scripts/BrowserAutomation.ps1 @@ -52,53 +52,55 @@ $null = New-Module { [string] $OutDir ) - if (-not $OutDir) { - $OutDir = "."; - } + Start-BrowserAutomation { + if (-not $OutDir) { + $OutDir = "."; + } - $dir = New-TemporaryDirectory; - Write-Host "Downloading ``$URL`` using browser automation…"; - $options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new(); - $options.SetPreference("browser.download.folderList", 2); - $options.SetPreference("browser.download.dir", "$dir"); + $dir = New-TemporaryDirectory; + Write-Host "Downloading ``$URL`` using browser automation…"; + $options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new(); + $options.SetPreference("browser.download.folderList", 2); + $options.SetPreference("browser.download.dir", "$dir"); - $downloadChecker = { - $files = Get-ChildItem $dir; + $downloadChecker = { + $files = Get-ChildItem $dir; - if ((@($files)).Count -gt 0) { - foreach ($file in $files) { - try { - $stream = [System.IO.File]::Open($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None); + if ((@($files)).Count -gt 0) { + foreach ($file in $files) { + try { + $stream = [System.IO.File]::Open($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None); - if ($stream) { - $stream.Close(); + if ($stream) { + $stream.Close(); + } + } + catch { + return $true; } } - catch { - return $true; - } + + return $false; + } else { + return $true; } + }; - return $false; - } else { - return $true; + $browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options); + $browser.Navigate().GoToUrl($URL); + & $Action -Browser $browser; + + while (& $downloadChecker) { + Write-Host "Waiting for the download to finish…"; + Start-Sleep 1; } - }; - $browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options); - $browser.Navigate().GoToUrl($URL); - & $Action -Browser $browser; - - while (& $downloadChecker) { - Write-Host "Waiting for the download to finish…"; - Start-Sleep 1; + $file = Get-ChildItem $dir; + $result = Move-Item $file $OutDir -PassThru; + $browser.Quit(); + Remove-Item -Recurse $dir; + $result; } - - $file = Get-ChildItem $dir; - $result = Move-Item $file $OutDir -PassThru; - $browser.Quit(); - Remove-Item -Recurse $dir; - $result; } <#