From 82c5fe6a82514afe2820826fc16e8faf6bb3528c Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 7 Aug 2024 14:28:26 +0200 Subject: [PATCH] Refactor browser automation scripts --- .../Windows/Scripts/AmdSoftwarePackage.ps1 | 2 +- scripts/Windows/Scripts/BrowserAutomation.ps1 | 200 +++++++++++------- scripts/Windows/Software/GPU-Z/Install.ps1 | 2 +- scripts/Windows/Software/reWASD/Manage.ps1 | 2 +- 4 files changed, 124 insertions(+), 82 deletions(-) diff --git a/scripts/Windows/Scripts/AmdSoftwarePackage.ps1 b/scripts/Windows/Scripts/AmdSoftwarePackage.ps1 index 6cd445f4..7e2ecc31 100644 --- a/scripts/Windows/Scripts/AmdSoftwarePackage.ps1 +++ b/scripts/Windows/Scripts/AmdSoftwarePackage.ps1 @@ -36,7 +36,7 @@ function Install-AmdSoftwarePackage() { } } - $file = Start-CustomDownload $context $url $action $tempDir; + $file = Start-CustomBrowserDownload $context $url $action $tempDir; Start-Process -Wait -FilePath $file -ArgumentList "/S"; Remove-Item -Recurse $tempDir; diff --git a/scripts/Windows/Scripts/BrowserAutomation.ps1 b/scripts/Windows/Scripts/BrowserAutomation.ps1 index b84635e8..0c52aa9f 100644 --- a/scripts/Windows/Scripts/BrowserAutomation.ps1 +++ b/scripts/Windows/Scripts/BrowserAutomation.ps1 @@ -1,92 +1,134 @@ #!/bin/bash . "$PSScriptRoot/Context.ps1"; -if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) { - if ((Get-Package Selenium.WebDriver -ErrorAction "SilentlyContinue")) { - $zipFile = [System.IO.Compression.ZipFile]::OpenRead((Get-Package Selenium.WebDriver).Source); - $stream = [System.IO.MemoryStream]::new(); - $reader = [System.IO.StreamReader]($zipFile.Entries[2]).Open(); - $reader.BaseStream.CopyTo($stream); - [byte[]]$bytes = $stream.ToArray(); - $reader.Close(); - $zipFile.Dispose(); - $null = [System.Reflection.Assembly]::Load($bytes); - } -} +$null = New-Module { + . "$PSScriptRoot/System.ps1"; -function Start-CustomDownload() { - param( - [Context] $context, - [string] $url, - [Action[OpenQA.Selenium.Firefox.FirefoxDriver]] $action, - [string] $outDir = $null - ) + <# + .SYNOPSIS + Runs an action involving browser automation. + + .PARAMETER Action + The action to execute. + #> + function Start-BrowserAutomation { + param( + [scriptblock] $Action + ) - $tempDir = $context.GetTempDirectory(); - $options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new(); - $options.SetPreference("browser.download.folderList", 2); - $options.SetPreference("browser.download.dir", $tempDir); - # $options.AddArgument("--headless"); - - $downloadChecker = { - param() - - $files = Get-ChildItem $tempDir; - - if ($(@($files)).Count -gt 0) { - foreach ($file in Get-ChildItem $tempDir) { - 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; - } - } - - return $false; - } else { - return $true; + if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) { + $zipFile = [System.IO.Compression.ZipFile]::OpenRead((Get-Package Selenium.WebDriver).Source); + $webDriver = ($zipFile.Entries | Where-Object { $_.FullName -like "lib/net6.0/WebDriver.dll" })[0]; + $stream = [System.IO.MemoryStream]::new(); + $reader = [System.IO.StreamReader]($webDriver).Open(); + $reader.BaseStream.CopyTo($stream); + [byte[]]$bytes = $stream.ToArray(); + $reader.Close(); + $reader.Dispose(); + $stream.Close(); + $stream.Dispose(); + $zipFile.Dispose(); + $null = [System.Reflection.Assembly]::Load($bytes); } + + & $Action; } - $browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options); - $browser.Navigate().GoToUrl($url); - $action.Invoke($browser); + <# + .SYNOPSIS + Downloads a file from the specified url using browser automation. - while ($downloadChecker.Invoke()) { - Write-Information "Waiting for download to finish..."; - Start-Sleep 1; + .PARAMETER URL + The url to download the file from. + + .PARAMETER Action + The action to execute in the browser for initiating the download. + + .PARAMETER OutDir + The directory to download the file to. + #> + function Start-CustomBrowserDownload { + param( + [string] $URL, + [scriptblock] $Action, + [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(); + } + } + catch { + 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; + } + + $file = Get-ChildItem $dir; + $result = Move-Item $file $OutDir -PassThru; + $browser.Quit(); + Remove-Item -Recurse $dir; + $result; } - $file = Get-ChildItem $tempDir; + <# + .SYNOPSIS + Downloads a file from the specified url using browser automation. - if (-not $outDir) { - $outDir = "."; + .PARAMETER URL + The url to download the file from. + + .PARAMETER ButtonSelector + The jQuery selector for finding the download button. + + .PARAMETER OutDir + The directory to download the file to. + #> + function Start-BrowserDownload { + param( + [string] $URL, + [string] $ButtonSelector, + [string] $OutDir = $null + ) + + Start-CustomBrowserDownload @PSBoundParameters -Action { + param( + [OpenQA.Selenium.Firefox.FirefoxDriver] $Browser + ) + + $Browser.FindElement([OpenQA.Selenium.By]::CssSelector($ButtonSelector)).Click(); + }; } - - $result = Move-Item $file $outDir -PassThru; - $browser.Quit(); - Remove-Item -Recurse $tempDir; - return $result; -} - -function Start-AutomatedDownload() { - param( - [Context] $context, - [string] $url, - [string] $buttonSelector, - [string] $outDir = $null - ) - - $action = { - param([OpenQA.Selenium.Firefox.FirefoxDriver] $browser) - $browser.FindElement([OpenQA.Selenium.By]::CssSelector("$buttonSelector")).Click(); - } - - return Start-CustomDownload $context $url $action $outDir; -} +}; diff --git a/scripts/Windows/Software/GPU-Z/Install.ps1 b/scripts/Windows/Software/GPU-Z/Install.ps1 index c251a166..3b9fe767 100644 --- a/scripts/Windows/Software/GPU-Z/Install.ps1 +++ b/scripts/Windows/Software/GPU-Z/Install.ps1 @@ -25,7 +25,7 @@ $action = { } Write-Information "Downloading GPU-Z"; -$file = Start-CustomDownload $context "https://www.techpowerup.com/download/techpowerup-gpu-z/" $action $tempDir; +$file = Start-CustomBrowserDownload $context "https://www.techpowerup.com/download/techpowerup-gpu-z/" $action $tempDir; Write-Information "Launching GPU-Z installer"; Start-Process -Wait -FilePath $file.FullName -ArgumentList "-installSilent"; diff --git a/scripts/Windows/Software/reWASD/Manage.ps1 b/scripts/Windows/Software/reWASD/Manage.ps1 index 7b8db798..e95a3ef6 100644 --- a/scripts/Windows/Software/reWASD/Manage.ps1 +++ b/scripts/Windows/Software/reWASD/Manage.ps1 @@ -16,7 +16,7 @@ $null = New-Module { $tempDir = $context.GetTempDirectory(); Write-Information "Downloading reWASD"; - $file = Start-AutomatedDownload $context "https://rewasd.com/" 'a.btn-default[href="#"]' $tempDir; + $file = Start-BrowserDownload $context "https://rewasd.com/" 'a.btn-default[href="#"]' $tempDir; Write-Information "Installing reWASD"; Start-Process -Wait -FilePath $file.FullName -ArgumentList "/S";