2023-07-15 17:05:25 +00:00
|
|
|
#!/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);
|
|
|
|
}
|
|
|
|
|
2023-07-15 18:38:44 +00:00
|
|
|
function Start-CustomDownload() {
|
2023-07-15 17:05:25 +00:00
|
|
|
param(
|
|
|
|
[Context] $context,
|
|
|
|
[string] $url,
|
2023-07-15 18:38:44 +00:00
|
|
|
[Action[OpenQA.Selenium.Firefox.FirefoxDriver]] $action,
|
2023-07-15 17:47:56 +00:00
|
|
|
[string] $outDir = $null
|
2023-07-15 17:05:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
$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()
|
|
|
|
|
2023-07-15 17:37:30 +00:00
|
|
|
$files = Get-ChildItem $tempDir;
|
2023-07-15 17:33:12 +00:00
|
|
|
|
2023-07-15 17:41:43 +00:00
|
|
|
if ($(@($files)).Count -gt 0) {
|
2023-07-15 17:37:30 +00:00
|
|
|
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);
|
2023-07-15 17:33:12 +00:00
|
|
|
|
2023-07-15 17:37:30 +00:00
|
|
|
if ($stream) {
|
|
|
|
$stream.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return $true;
|
|
|
|
}
|
2023-07-15 17:33:12 +00:00
|
|
|
}
|
2023-07-15 17:56:45 +00:00
|
|
|
|
|
|
|
return $false;
|
2023-07-15 17:37:30 +00:00
|
|
|
} else {
|
2023-07-15 17:41:43 +00:00
|
|
|
return $true;
|
2023-07-15 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:05:25 +00:00
|
|
|
$browser = [OpenQA.Selenium.Firefox.FirefoxDriver]::new($options);
|
|
|
|
$browser.Navigate().GoToUrl($url);
|
2023-07-15 18:38:44 +00:00
|
|
|
$action.Invoke($browser);
|
2023-07-15 17:33:12 +00:00
|
|
|
|
|
|
|
while ($downloadChecker.Invoke()) {
|
|
|
|
Write-Information "Waiting for download to finish...";
|
|
|
|
Start-Sleep 1;
|
|
|
|
}
|
2023-07-15 17:47:56 +00:00
|
|
|
|
|
|
|
$file = Get-ChildItem $tempDir;
|
|
|
|
|
|
|
|
if (-not $outDir) {
|
|
|
|
$outDir = ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = Move-Item $file $outDir -PassThru;
|
2023-07-15 18:04:25 +00:00
|
|
|
$browser.Quit();
|
2023-07-15 17:53:20 +00:00
|
|
|
Remove-Item -Recurse $tempDir;
|
2023-07-15 17:47:56 +00:00
|
|
|
return $result;
|
2023-07-15 17:05:25 +00:00
|
|
|
}
|
2023-07-15 18:38:44 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|