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,92 +1,134 @@
#!/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";
$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);
}
}
function Start-CustomDownload() { <#
param( .SYNOPSIS
[Context] $context, Runs an action involving browser automation.
[string] $url,
[Action[OpenQA.Selenium.Firefox.FirefoxDriver]] $action,
[string] $outDir = $null
)
$tempDir = $context.GetTempDirectory(); .PARAMETER Action
$options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new(); The action to execute.
$options.SetPreference("browser.download.folderList", 2); #>
$options.SetPreference("browser.download.dir", $tempDir); function Start-BrowserAutomation {
# $options.AddArgument("--headless"); param(
[scriptblock] $Action
)
$downloadChecker = { if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) {
param() $zipFile = [System.IO.Compression.ZipFile]::OpenRead((Get-Package Selenium.WebDriver).Source);
$webDriver = ($zipFile.Entries | Where-Object { $_.FullName -like "lib/net6.0/WebDriver.dll" })[0];
$files = Get-ChildItem $tempDir; $stream = [System.IO.MemoryStream]::new();
$reader = [System.IO.StreamReader]($webDriver).Open();
if ($(@($files)).Count -gt 0) { $reader.BaseStream.CopyTo($stream);
foreach ($file in Get-ChildItem $tempDir) { [byte[]]$bytes = $stream.ToArray();
try { $reader.Close();
$stream = [System.IO.File]::Open($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None); $reader.Dispose();
$stream.Close();
if ($stream) { $stream.Dispose();
$stream.Close(); $zipFile.Dispose();
} $null = [System.Reflection.Assembly]::Load($bytes);
}
catch {
return $true;
}
}
return $false;
} else {
return $true;
} }
& $Action;
} }
$browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options); <#
$browser.Navigate().GoToUrl($url); .SYNOPSIS
$action.Invoke($browser); Downloads a file from the specified url using browser automation.
while ($downloadChecker.Invoke()) { .PARAMETER URL
Write-Information "Waiting for download to finish..."; The url to download the file from.
Start-Sleep 1;
.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) { .PARAMETER URL
$outDir = "."; 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;
}

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";