From f08e54151a59cd6ee7098e3cbb0d14422c0bd42e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 8 Aug 2024 02:59:02 +0200 Subject: [PATCH] Ensure Selenium is imported --- scripts/Common/Scripts/BrowserAutomation.ps1 | 86 ++++++++++---------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/scripts/Common/Scripts/BrowserAutomation.ps1 b/scripts/Common/Scripts/BrowserAutomation.ps1 index 4f171ac9..5fcb02d4 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 = "."; - } - - $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; - - 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(); + 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"); + + $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 ($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; } <#