PortValhalla/scripts/Windows/Scripts/BrowserAutomation.ps1

56 lines
1.7 KiB
PowerShell
Raw Normal View History

#!/bin/bash
. "$PSScriptRoot/Context.ps1";
if (-not ("OpenQA.Selenium.Firefox.FirefoxDriver" -as [type])) {
$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();
[System.Reflection.Assembly]::Load($bytes);
}
function Start-AutomatedDownload() {
param(
[Context] $context,
[string] $url,
[string] $buttonSelector
)
$tempDir = $context.GetTempDirectory();
$options = [OpenQA.Selenium.Firefox.FirefoxOptions]::new();
$options.SetPreference("browser.download.folderList", 2);
$options.SetPreference("browser.download.dir", $tempDir);
# $options.AddArgument("--headless");
2023-07-15 17:33:12 +00:00
$downloadChecker = {
param()
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();
}
return $false;
}
catch {
return $true;
}
}
}
$browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options);
$browser.Navigate().GoToUrl($url);
2023-07-15 17:19:42 +00:00
$browser.FindElement([OpenQA.Selenium.By]::CssSelector("$buttonSelector")).Click();
2023-07-15 17:33:12 +00:00
while ($downloadChecker.Invoke()) {
Write-Information "Waiting for download to finish...";
Start-Sleep 1;
}
}