Skip to content

Commit d0176f4

Browse files
committed
Allow downloads when headless, via Chrome Command
1 parent 0a44c97 commit d0176f4

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Selenium.psm1

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ elseif($IsMacOS){
88
$AssembliesPath = "$PSScriptRoot/assemblies/macos"
99
}
1010

11-
# Grant Execution permission to assemblies on Linux and MacOS
11+
# Grant Execution permission to assemblies on Linux and MacOS
1212
if($IsLinux -or $IsMacOS){
1313
# Check if powershell is NOT running as root
1414
$AssemblieFiles = Get-ChildItem -Path $AssembliesPath |Where-Object{$_.Name -eq 'chromedriver' -or $_.Name -eq 'geckodriver'}
@@ -62,7 +62,7 @@ function Start-SeChrome {
6262
}
6363
PROCESS{
6464
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
65-
65+
6666
if($DefaultDownloadPath){
6767
Write-Verbose "Setting Default Download directory: $DefaultDownloadPath"
6868
$Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
@@ -71,11 +71,11 @@ function Start-SeChrome {
7171
Write-Verbose "Setting Profile directory: $ProfileDirectoryPath"
7272
$Chrome_Options.AddArgument("user-data-dir=$ProfileDirectoryPath")
7373
}
74-
74+
7575
if($DisableBuiltInPDFViewer){
7676
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
7777
}
78-
78+
7979
if ($Headless) {
8080
$Chrome_Options.AddArguments('headless')
8181
}
@@ -97,7 +97,7 @@ function Start-SeChrome {
9797
$Chrome_Options.AddArguments($Argument)
9898
}
9999
}
100-
100+
101101
if (!$HideVersionHint) {
102102
Write-Verbose "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'"
103103
}
@@ -106,14 +106,22 @@ function Start-SeChrome {
106106
$Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $AssembliesPath,$Chrome_Options
107107
}
108108
else{
109-
$Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
109+
$Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
110110
}
111111

112112
if($Minimized){
113113
$driver.Manage().Window.Minimize();
114114

115115
}
116116

117+
if($Headless -and $DefaultDownloadPath) {
118+
$CmdParams = New-Object 'system.collections.generic.dictionary[[System.String],[System.Object]]]'
119+
$CmdParams.Add('behavior', 'allow')
120+
$CmdParams.Add('downloadPath', $DefaultDownloadPath.FullName)
121+
122+
$Driver.ExecuteChromeCommand('Page.setDownloadBehavior', $CmdParams)
123+
}
124+
117125
if($StartURL){
118126
Enter-SeUrl -Driver $Driver -Url $StartURL
119127
}
@@ -126,7 +134,7 @@ function Start-SeChrome {
126134
function Start-SeInternetExplorer {
127135
$InternetExplorer_Options = New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerOptions"
128136
$InternetExplorer_Options.IgnoreZoomLevel = $true
129-
New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerDriver" -ArgumentList $InternetExplorer_Options
137+
New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerDriver" -ArgumentList $InternetExplorer_Options
130138
}
131139

132140
function Start-SeEdge {
@@ -161,7 +169,7 @@ function Start-SeFirefox {
161169
}
162170

163171
function Stop-SeDriver {
164-
param($Driver)
172+
param($Driver)
165173

166174
$Driver.Dispose()
167175
}
@@ -222,7 +230,7 @@ function Find-SeElement {
222230
if ($PSCmdlet.ParameterSetName -eq "ById") {
223231
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
224232
}
225-
233+
226234
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
227235
$TargetElement = [OpenQA.Selenium.By]::LinkText($LinkText)
228236
}
@@ -238,15 +246,15 @@ function Find-SeElement {
238246
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
239247
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
240248
}
241-
249+
242250
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
243251
$TargetElement = [OpenQA.Selenium.By]::XPath($XPath)
244252
}
245253

246254
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
247255
$TargetElement = [OpenQA.Selenium.By]::CssSelector($Css)
248256
}
249-
257+
250258
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
251259
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
252260
$WebDriverWait.Until($Condition)
@@ -279,7 +287,7 @@ function Find-SeElement {
279287
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
280288
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
281289
}
282-
290+
283291
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
284292
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
285293
}
@@ -307,17 +315,17 @@ function Invoke-SeClick {
307315
}
308316

309317
function Get-SeKeys {
310-
318+
311319
[OpenQA.Selenium.Keys] | Get-Member -MemberType Property -Static | Select-Object -Property Name, @{N = "ObjectString"; E = { "[OpenQA.Selenium.Keys]::$($_.Name)" } }
312320
}
313321

314322
function Send-SeKeys {
315323
param([OpenQA.Selenium.IWebElement]$Element, [string]$Keys)
316-
324+
317325
foreach ($Key in @(Get-SeKeys).Name) {
318326
$Keys = $Keys -replace "{{$Key}}", [OpenQA.Selenium.Keys]::$Key
319327
}
320-
328+
321329
$Element.SendKeys($Keys)
322330
}
323331

@@ -344,7 +352,7 @@ function Remove-SeCookie {
344352

345353
function Set-SeCookie {
346354
param(
347-
[ValidateNotNull()]$Driver,
355+
[ValidateNotNull()]$Driver,
348356
[string]$Name,
349357
[string]$Value,
350358
[string]$Path,
@@ -360,7 +368,7 @@ function Set-SeCookie {
360368
Cookie(String, String, String, Nullable<DateTime>)
361369
Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
362370
Cookie(String, String, String, String, Nullable<DateTime>)
363-
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
371+
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
364372
#>
365373
Begin{
366374
if($ExpiryDate -ne $null -and $ExpiryDate.GetType().Name -ne 'DateTime'){
@@ -412,7 +420,7 @@ function Get-SeElementAttribute {
412420

413421
Process {
414422
$Element.GetAttribute($Attribute)
415-
}
423+
}
416424
}
417425

418426
function Invoke-SeScreenshot {
@@ -448,7 +456,7 @@ function Get-SeWindow {
448456

449457
Process {
450458
$Driver.WindowHandles
451-
}
459+
}
452460
}
453461

454462
function Switch-SeWindow {
@@ -459,5 +467,5 @@ function Switch-SeWindow {
459467

460468
Process {
461469
$Driver.SwitchTo().Window($Window)|Out-Null
462-
}
470+
}
463471
}

0 commit comments

Comments
 (0)