Refactor browser automation scripts

This commit is contained in:
Manuel Thalmann 2024-08-07 14:28:26 +02:00
parent b016f310ea
commit ee4d402a5d
4 changed files with 124 additions and 82 deletions

View file

@ -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"; Start-Process -Wait -FilePath $file -ArgumentList "/S";
Remove-Item -Recurse $tempDir; Remove-Item -Recurse $tempDir;

View file

@ -1,47 +1,80 @@
#!/bin/bash #!/bin/bash
. "$PSScriptRoot/Context.ps1"; . "$PSScriptRoot/Context.ps1";
if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) { $null = New-Module {
if ((Get-Package Selenium.WebDriver -ErrorAction "SilentlyContinue")) { . "$PSScriptRoot/System.ps1";
<#
.SYNOPSIS
Runs an action involving browser automation.
.PARAMETER Action
The action to execute.
#>
function Start-BrowserAutomation {
param(
[scriptblock] $Action
)
if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) {
$zipFile = [System.IO.Compression.ZipFile]::OpenRead((Get-Package Selenium.WebDriver).Source); $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(); $stream = [System.IO.MemoryStream]::new();
$reader = [System.IO.StreamReader]($zipFile.Entries[2]).Open(); $reader = [System.IO.StreamReader]($webDriver).Open();
$reader.BaseStream.CopyTo($stream); $reader.BaseStream.CopyTo($stream);
[byte[]]$bytes = $stream.ToArray(); [byte[]]$bytes = $stream.ToArray();
$reader.Close(); $reader.Close();
$reader.Dispose();
$stream.Close();
$stream.Dispose();
$zipFile.Dispose(); $zipFile.Dispose();
$null = [System.Reflection.Assembly]::Load($bytes); $null = [System.Reflection.Assembly]::Load($bytes);
} }
}
function Start-CustomDownload() { & $Action;
}
<#
.SYNOPSIS
Downloads a file from the specified url using browser automation.
.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( param(
[Context] $context, [string] $URL,
[string] $url, [scriptblock] $Action,
[Action[OpenQA.Selenium.Firefox.FirefoxDriver]] $action, [string] $OutDir
[string] $outDir = $null
) )
$tempDir = $context.GetTempDirectory(); if (-not $OutDir) {
$OutDir = ".";
}
$dir = New-TemporaryDirectory;
Write-Host "Downloading ``$URL`` using browser automation…";
$options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new(); $options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new();
$options.SetPreference("browser.download.folderList", 2); $options.SetPreference("browser.download.folderList", 2);
$options.SetPreference("browser.download.dir", $tempDir); $options.SetPreference("browser.download.dir", $dir);
# $options.AddArgument("--headless");
$downloadChecker = { $downloadChecker = {
param() $files = Get-ChildItem $dir;
$files = Get-ChildItem $tempDir; if ((@($files)).Count -gt 0) {
foreach ($file in $files) {
if ($(@($files)).Count -gt 0) {
foreach ($file in Get-ChildItem $tempDir) {
try { try {
$stream = [System.IO.File]::Open($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None); $stream = [System.IO.File]::Open($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None);
if ($stream) { if ($stream) {
$stream.Close(); $stream.Close();
} }
} }
catch { catch {
return $true; return $true;
@ -52,41 +85,50 @@ function Start-CustomDownload() {
} else { } else {
return $true; return $true;
} }
} };
$browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options); $browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options);
$browser.Navigate().GoToUrl($url); $browser.Navigate().GoToUrl($URL);
$action.Invoke($browser); & $Action -Browser $browser;
while ($downloadChecker.Invoke()) { while (& $downloadChecker) {
Write-Information "Waiting for download to finish..."; Write-Host "Waiting for the download to finish…";
Start-Sleep 1; Start-Sleep 1;
} }
$file = Get-ChildItem $tempDir; $file = Get-ChildItem $dir;
$result = Move-Item $file $OutDir -PassThru;
if (-not $outDir) { $browser.Quit();
$outDir = "."; Remove-Item -Recurse $dir;
$result;
} }
$result = Move-Item $file $outDir -PassThru; <#
$browser.Quit(); .SYNOPSIS
Remove-Item -Recurse $tempDir; Downloads a file from the specified url using browser automation.
return $result;
}
function Start-AutomatedDownload() { .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( param(
[Context] $context, [string] $URL,
[string] $url, [string] $ButtonSelector,
[string] $buttonSelector, [string] $OutDir = $null
[string] $outDir = $null
) )
$action = { Start-CustomBrowserDownload @PSBoundParameters -Action {
param([OpenQA.Selenium.Firefox.FirefoxDriver] $browser) param(
$browser.FindElement([OpenQA.Selenium.By]::CssSelector("$buttonSelector")).Click(); [OpenQA.Selenium.Firefox.FirefoxDriver] $Browser
} )
return Start-CustomDownload $context $url $action $outDir; $Browser.FindElement([OpenQA.Selenium.By]::CssSelector($ButtonSelector)).Click();
} };
}
};

View file

@ -25,7 +25,7 @@ $action = {
} }
Write-Information "Downloading GPU-Z"; 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"; Write-Information "Launching GPU-Z installer";
Start-Process -Wait -FilePath $file.FullName -ArgumentList "-installSilent"; Start-Process -Wait -FilePath $file.FullName -ArgumentList "-installSilent";

View file

@ -16,7 +16,7 @@ $null = New-Module {
$tempDir = $context.GetTempDirectory(); $tempDir = $context.GetTempDirectory();
Write-Information "Downloading reWASD"; 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"; Write-Information "Installing reWASD";
Start-Process -Wait -FilePath $file.FullName -ArgumentList "/S"; Start-Process -Wait -FilePath $file.FullName -ArgumentList "/S";