Refactor browser automation scripts

This commit is contained in:
Manuel Thalmann 2024-08-07 14:28:26 +02:00
parent 7b8849e0bf
commit d13d774b4b
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,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;
}
};

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