@@ -8,54 +8,118 @@ elseif($IsMacOS){
8
8
$AssembliesPath = " $PSScriptRoot /assemblies/macos"
9
9
}
10
10
11
+ # Grant Execution permission to assemblies on Linux and MacOS
12
+ if ($IsLinux -or $IsMacOS ){
13
+ # Check if powershell is NOT running as root
14
+ $AssemblieFiles = Get-ChildItem - Path $AssembliesPath | Where-Object {$_.Name -eq ' chromedriver' -or $_.Name -eq ' geckodriver' }
15
+ foreach ($AssemblieFile in $AssemblieFiles ){
16
+ if ($IsLinux ){
17
+ $FileMod = stat - c " %a" $AssemblieFile.fullname
18
+ }
19
+ elseif ($IsMacOS ){
20
+ $FileMod = / usr/ bin/ stat -f " %A" $AssemblieFile.fullname
21
+ }
22
+
23
+ if ($FileMod [2 ] -ne ' 5' -and $FileMod [2 ] -ne ' 7' ){
24
+ Write-Host " Granting $ ( $AssemblieFile.fullname ) Execution Permissions ..."
25
+ chmod + x $AssemblieFile.fullname
26
+ }
27
+ }
28
+ }
29
+
11
30
function Start-SeChrome {
12
31
Param (
13
32
[Parameter (Mandatory = $false )]
14
33
[array ]$Arguments ,
15
34
[switch ]$HideVersionHint ,
35
+ [string ]$StartURL ,
16
36
[System.IO.FileInfo ]$DefaultDownloadPath ,
37
+ [System.IO.FileInfo ]$ProfileDirectoryPath ,
17
38
[bool ]$DisableBuiltInPDFViewer = $true ,
18
39
[switch ]$Headless ,
19
40
[switch ]$Incognito ,
20
- [switch ]$Maximized
41
+ [switch ]$Maximized ,
42
+ [switch ]$Minimized ,
43
+ [switch ]$Fullscreen
21
44
)
22
45
23
- $Chrome_Options = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeOptions"
24
-
25
- if ($DefaultDownloadPath ){
26
- Write-Host " Setting Default Download directory: $DefaultDownloadPath "
27
- $Chrome_Options.AddUserProfilePreference (' download' , @ {' default_directory' = $ ($DefaultDownloadPath.FullName ); ' prompt_for_download' = $false ; })
28
- }
29
-
30
- if ($DisableBuiltInPDFViewer ){
31
- $Chrome_Options.AddUserProfilePreference (' plugins' , @ {' always_open_pdf_externally' = $true ;})
46
+ BEGIN {
47
+ if ($Maximized -ne $false -and $Minimized -ne $false ) {
48
+ throw ' Maximized and Minimized may not be specified together.'
49
+ }
50
+ elseif ($Maximized -ne $false -and $Fullscreen -ne $false ){
51
+ throw ' Maximized and Fullscreen may not be specified together.'
52
+ }
53
+ elseif ($Minimized -ne $false -and $Fullscreen -ne $false ){
54
+ throw ' Minimized and Fullscreen may not be specified together.'
55
+ }
56
+
57
+ if ($StartURL ){
58
+ if (! [system.uri ]::IsWellFormedUriString($StartURL , [System.UriKind ]::Absolute)){
59
+ throw ' Incorrect StartURL please make sure the URL starts with http:// or https://'
60
+ }
61
+ }
32
62
}
63
+ PROCESS {
64
+ $Chrome_Options = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeOptions"
33
65
34
- if ($Headless ) {
35
- $Chrome_Options.AddArguments (' headless' )
36
- }
66
+ if ($DefaultDownloadPath ){
67
+ Write-Verbose " Setting Default Download directory: $DefaultDownloadPath "
68
+ $Chrome_Options.AddUserProfilePreference (' download' , @ {' default_directory' = $ ($DefaultDownloadPath.FullName ); ' prompt_for_download' = $false ; })
69
+ }
70
+ if ($ProfileDirectoryPath ){
71
+ Write-Verbose " Setting Profile directory: $ProfileDirectoryPath "
72
+ $Chrome_Options.AddArgument (" user-data-dir=$ProfileDirectoryPath " )
73
+ }
74
+
75
+ if ($DisableBuiltInPDFViewer ){
76
+ $Chrome_Options.AddUserProfilePreference (' plugins' , @ {' always_open_pdf_externally' = $true ;})
77
+ }
78
+
79
+ if ($Headless ) {
80
+ $Chrome_Options.AddArguments (' headless' )
81
+ }
37
82
38
- if ($Incognito ) {
39
- $Chrome_Options.AddArguments (' Incognito' )
40
- }
83
+ if ($Incognito ) {
84
+ $Chrome_Options.AddArguments (' Incognito' )
85
+ }
41
86
42
- if ($Maximized ) {
43
- $Chrome_Options.AddArguments (' start-maximized' )
44
- }
87
+ if ($Maximized ) {
88
+ $Chrome_Options.AddArguments (' start-maximized' )
89
+ }
45
90
46
- if ($Arguments ) {
47
- $Chrome_Options.AddArguments ($Arguments )
48
- }
49
-
50
- if (! $HideVersionHint ) {
51
- Write-Host " Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" - ForegroundColor Yellow
52
- }
91
+ if ($Fullscreen ) {
92
+ $Chrome_Options.AddArguments (' start-fullscreen' )
93
+ }
94
+
95
+ if ($Arguments ) {
96
+ foreach ($Argument in $Arguments ){
97
+ $Chrome_Options.AddArguments ($Argument )
98
+ }
99
+ }
100
+
101
+ if (! $HideVersionHint ) {
102
+ Write-Verbose " Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'"
103
+ }
53
104
54
- if ($IsLinux -or $IsMacOS ){
55
- New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver" - ArgumentList $AssembliesPath , $Chrome_Options
105
+ if ($IsLinux -or $IsMacOS ){
106
+ $Driver = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver" - ArgumentList $AssembliesPath , $Chrome_Options
107
+ }
108
+ else {
109
+ $Driver = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver" - ArgumentList $Chrome_Options
110
+ }
111
+
112
+ if ($Minimized ){
113
+ $driver.Manage ().Window.Minimize();
114
+
115
+ }
116
+
117
+ if ($StartURL ){
118
+ Enter-SeUrl - Driver $Driver - Url $StartURL
119
+ }
56
120
}
57
- else {
58
- New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver " - ArgumentList $Chrome_Options
121
+ END {
122
+ return $Driver
59
123
}
60
124
}
61
125
@@ -264,20 +328,29 @@ function Get-SeCookie {
264
328
}
265
329
266
330
function Remove-SeCookie {
267
- param ($Driver )
268
-
269
- $Driver.Manage ().Cookies.DeleteAllCookies()
331
+ param (
332
+ $Driver ,
333
+ [switch ]$DeleteAllCookies ,
334
+ [string ]$Name
335
+ )
336
+
337
+ if ($DeleteAllCookies ){
338
+ $Driver.Manage ().Cookies.DeleteAllCookies()
339
+ }
340
+ else {
341
+ $Driver.Manage ().Cookies.DeleteCookieNamed($Name )
342
+ }
270
343
}
271
344
272
345
function Set-SeCookie {
273
346
param (
274
- $Driver ,
275
- [string ]$Name ,
347
+ [ ValidateNotNull ()] $Driver ,
348
+ [string ]$Name ,
276
349
[string ]$Value ,
277
350
[string ]$Path ,
278
351
[string ]$Domain ,
279
- [ datetime ] $ExpiryDate
280
- )
352
+ $ExpiryDate
353
+ )
281
354
282
355
<# Selenium Cookie Information
283
356
Cookie(String, String)
@@ -289,37 +362,44 @@ function Set-SeCookie {
289
362
Cookie(String, String, String, String, Nullable<DateTime>)
290
363
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
291
364
#>
292
-
293
- if ($Name -and $Value -and (! $Path -and ! $Domain -and ! $ExpiryDate )){
294
- $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value
295
- }
296
- Elseif ($Name -and $Value -and $Path -and (! $Domain -and ! $ExpiryDate )){
297
- $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Path
298
- }
299
- Elseif ($Name -and $Value -and $Path -and $ExpiryDate -and ! $Domain ){
300
- $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Path , $ExpiryDate
365
+ Begin {
366
+ if ($ExpiryDate -ne $null -and $ExpiryDate.GetType ().Name -ne ' DateTime' ){
367
+ throw ' $ExpiryDate can only be $null or TypeName: System.DateTime'
368
+ }
301
369
}
302
- Elseif ($Name -and $Value -and $Path -and $ExpiryDate -and $Domain ){
303
- if ($Driver.Url -match $Domain ){
304
- $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Domain , $Path , $ExpiryDate
370
+
371
+ Process {
372
+ if ($Name -and $Value -and (! $Path -and ! $Domain -and ! $ExpiryDate )){
373
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value
374
+ }
375
+ Elseif ($Name -and $Value -and $Path -and (! $Domain -and ! $ExpiryDate )){
376
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Path
377
+ }
378
+ Elseif ($Name -and $Value -and $Path -and $ExpiryDate -and ! $Domain ){
379
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Path , $ExpiryDate
380
+ }
381
+ Elseif ($Name -and $Value -and $Path -and $Domain -and (! $ExpiryDate -or $ExpiryDate )){
382
+ if ($Driver.Url -match $Domain ){
383
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Domain , $Path , $ExpiryDate
384
+ }
385
+ else {
386
+ Throw ' In order to set the cookie the browser needs to be on the cookie domain URL'
387
+ }
305
388
}
306
389
else {
307
- Throw ' In order to set the cookie the browser needs to be on the cookie domain URL'
390
+ Throw " Incorrect Cookie Layout:
391
+ Cookie(String, String)
392
+ Initializes a new instance of the Cookie class with a specific name and value.
393
+ Cookie(String, String, String)
394
+ Initializes a new instance of the Cookie class with a specific name, value, and path.
395
+ Cookie(String, String, String, Nullable<DateTime>)
396
+ Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
397
+ Cookie(String, String, String, String, Nullable<DateTime>)
398
+ Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date."
308
399
}
309
- }
310
- else {
311
- Throw " Incorrect Cookie Layout:
312
- Cookie(String, String)
313
- Initializes a new instance of the Cookie class with a specific name and value.
314
- Cookie(String, String, String)
315
- Initializes a new instance of the Cookie class with a specific name, value, and path.
316
- Cookie(String, String, String, Nullable<DateTime>)
317
- Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
318
- Cookie(String, String, String, String, Nullable<DateTime>)
319
- Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date."
320
- }
321
400
322
- $Driver.Manage ().Cookies.AddCookie($cookie )
401
+ $Driver.Manage ().Cookies.AddCookie($cookie )
402
+ }
323
403
}
324
404
325
405
function Get-SeElementAttribute {
0 commit comments