Refactor browser automation scripts

This commit is contained in:
Manuel Thalmann 2024-08-07 14:28:26 +02:00
parent c9fc08dfd6
commit 1eeb391454
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";
Remove-Item -Recurse $tempDir;

View file

@ -1,47 +1,80 @@
#!/bin/bash
. "$PSScriptRoot/Context.ps1";
if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) {
if ((Get-Package Selenium.WebDriver -ErrorAction "SilentlyContinue")) {
$null = New-Module {
. "$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);
$webDriver = ($zipFile.Entries | Where-Object { $_.FullName -like "lib/net6.0/WebDriver.dll" })[0];
$stream = [System.IO.MemoryStream]::new();
$reader = [System.IO.StreamReader]($zipFile.Entries[2]).Open();
$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);
}
}
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(
[Context] $context,
[string] $url,
[Action[OpenQA.Selenium.Firefox.FirefoxDriver]] $action,
[string] $outDir = $null
[string] $URL,
[scriptblock] $Action,
[string] $OutDir
)
$tempDir = $context.GetTempDirectory();
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", $tempDir);
# $options.AddArgument("--headless");
$options.SetPreference("browser.download.dir", $dir);
$downloadChecker = {
param()
$files = Get-ChildItem $dir;
$files = Get-ChildItem $tempDir;
if ($(@($files)).Count -gt 0) {
foreach ($file in Get-ChildItem $tempDir) {
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;
@ -52,41 +85,50 @@ function Start-CustomDownload() {
} else {
return $true;
}
}
};
$browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options);
$browser.Navigate().GoToUrl($url);
$action.Invoke($browser);
$browser.Navigate().GoToUrl($URL);
& $Action -Browser $browser;
while ($downloadChecker.Invoke()) {
Write-Information "Waiting for download to finish...";
while (& $downloadChecker) {
Write-Host "Waiting for the download to finish…";
Start-Sleep 1;
}
$file = Get-ChildItem $tempDir;
if (-not $outDir) {
$outDir = ".";
$file = Get-ChildItem $dir;
$result = Move-Item $file $OutDir -PassThru;
$browser.Quit();
Remove-Item -Recurse $dir;
$result;
}
$result = Move-Item $file $outDir -PassThru;
$browser.Quit();
Remove-Item -Recurse $tempDir;
return $result;
}
<#
.SYNOPSIS
Downloads a file from the specified url using browser automation.
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(
[Context] $context,
[string] $url,
[string] $buttonSelector,
[string] $outDir = $null
[string] $URL,
[string] $ButtonSelector,
[string] $OutDir = $null
)
$action = {
param([OpenQA.Selenium.Firefox.FirefoxDriver] $browser)
$browser.FindElement([OpenQA.Selenium.By]::CssSelector("$buttonSelector")).Click();
}
Start-CustomBrowserDownload @PSBoundParameters -Action {
param(
[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";
$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";

View file

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