PortValhalla/scripts/Common/Scripts/BrowserAutomation.ps1

164 lines
4.8 KiB
PowerShell
Raw Normal View History

#!/bin/bash
2024-08-07 12:28:26 +00:00
$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]($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;
2023-07-15 20:23:28 +00:00
}
2024-08-07 12:28:26 +00:00
<#
.SYNOPSIS
Downloads a file from the specified url using browser automation.
2023-07-15 17:56:45 +00:00
2024-08-07 12:28:26 +00:00
.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
)
2024-08-08 01:32:03 +00:00
$downloadAction = $Action;
2024-08-08 00:59:02 +00:00
Start-BrowserAutomation {
if (-not $OutDir) {
$OutDir = ".";
}
2023-07-15 17:33:12 +00:00
2024-08-08 00:59:02 +00:00
$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");
2023-07-15 17:33:12 +00:00
2024-08-08 00:59:02 +00:00
$downloadChecker = {
$files = Get-ChildItem $dir;
2023-07-15 17:47:56 +00:00
2024-08-16 15:57:22 +00:00
if ((@($files)).Count -eq 1) {
$file = $files[0];
try {
$stream = [System.IO.File]::Open($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None);
if ($stream) {
$stream.Close();
2024-08-07 12:28:26 +00:00
}
}
2024-08-16 15:57:22 +00:00
catch {
return $true;
}
2024-08-08 00:59:02 +00:00
return $false;
} else {
return $true;
2024-08-07 12:28:26 +00:00
}
2024-08-08 00:59:02 +00:00
};
2024-08-07 12:28:26 +00:00
2024-08-08 00:59:02 +00:00
$browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options);
$browser.Navigate().GoToUrl($URL);
2024-08-07 12:28:26 +00:00
while (-not ($browser.ExecuteScript("return document.readyState;") -eq "complete")) {
Start-Sleep 0.1;
}
2024-08-14 16:47:29 +00:00
try {
$null = & $downloadAction -Browser $browser;
while (& $downloadChecker) {
Write-Host "Waiting for the download to finish…";
Start-Sleep 1;
}
2023-07-15 17:47:56 +00:00
2024-08-14 16:47:29 +00:00
$file = Get-ChildItem $dir;
$result = Move-Item $file $OutDir -PassThru;
$browser.Quit();
Remove-Item -Recurse $dir;
$result;
}
catch {
2024-08-17 11:49:21 +00:00
Write-Error "$Error";
2024-08-14 16:47:29 +00:00
}
2024-08-07 12:28:26 +00:00
}
2023-07-15 18:38:44 +00:00
}
2024-08-07 12:28:26 +00:00
<#
.SYNOPSIS
Downloads a file from the specified url using browser automation.
.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
)
2024-08-14 16:47:29 +00:00
$selector = [OpenQA.Selenium.By]::CssSelector($ButtonSelector);
[OpenQA.Selenium.IWebElement] $element = $null;
for ($i = 0; $i -lt 5; $i++) {
$element = $Browser.FindElement($selector);
if ($element) {
break;
} else {
Start-Sleep 1;
}
}
if ($element) {
$Browser.FindElement([OpenQA.Selenium.By]::CssSelector($ButtonSelector)).Click();
} else {
2024-08-17 11:49:21 +00:00
Write-Error "Unable to find download button!";
2024-08-14 16:47:29 +00:00
}
2024-08-07 12:28:26 +00:00
};
}
};