@@ -13,49 +13,94 @@ function Start-SeChrome {
13
13
[Parameter (Mandatory = $false )]
14
14
[array ]$Arguments ,
15
15
[switch ]$HideVersionHint ,
16
+ [string ]$StartURL ,
16
17
[System.IO.FileInfo ]$DefaultDownloadPath ,
18
+ [System.IO.FileInfo ]$ProfileDirectoryPath ,
17
19
[bool ]$DisableBuiltInPDFViewer = $true ,
18
20
[switch ]$Headless ,
19
21
[switch ]$Incognito ,
20
- [switch ]$Maximized
22
+ [switch ]$Maximized ,
23
+ [switch ]$Minimized ,
24
+ [switch ]$Fullscreen
21
25
)
22
26
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 ;})
27
+ BEGIN {
28
+ if ($Maximized -ne $false -and $Minimized -ne $false ) {
29
+ throw ' Maximized and Minimized may not be specified together.'
30
+ }
31
+ elseif ($Maximized -ne $false -and $Fullscreen -ne $false ){
32
+ throw ' Maximized and Fullscreen may not be specified together.'
33
+ }
34
+ elseif ($Minimized -ne $false -and $Fullscreen -ne $false ){
35
+ throw ' Minimized and Fullscreen may not be specified together.'
36
+ }
37
+
38
+ if ($StartURL ){
39
+ if (! [system.uri ]::IsWellFormedUriString($StartURL , [System.UriKind ]::Absolute)){
40
+ throw ' Incorrect StartURL please make sure the URL starts with http:// or https://'
41
+ }
42
+ }
32
43
}
44
+ PROCESS {
45
+ $Chrome_Options = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeOptions"
33
46
34
- if ($Headless ) {
35
- $Chrome_Options.AddArguments (' headless' )
36
- }
47
+ if ($DefaultDownloadPath ){
48
+ Write-Host " Setting Default Download directory: $DefaultDownloadPath "
49
+ $Chrome_Options.AddUserProfilePreference (' download' , @ {' default_directory' = $ ($DefaultDownloadPath.FullName ); ' prompt_for_download' = $false ; })
50
+ }
51
+ if ($ProfileDirectoryPath ){
52
+ Write-Host " Setting Profile directory: $ProfileDirectoryPath "
53
+ $Chrome_Options.AddArgument (" user-data-dir=$ProfileDirectoryPath " )
54
+ }
55
+
56
+ if ($DisableBuiltInPDFViewer ){
57
+ $Chrome_Options.AddUserProfilePreference (' plugins' , @ {' always_open_pdf_externally' = $true ;})
58
+ }
59
+
60
+ if ($Headless ) {
61
+ $Chrome_Options.AddArguments (' headless' )
62
+ }
37
63
38
- if ($Incognito ) {
39
- $Chrome_Options.AddArguments (' Incognito' )
40
- }
64
+ if ($Incognito ) {
65
+ $Chrome_Options.AddArguments (' Incognito' )
66
+ }
41
67
42
- if ($Maximized ) {
43
- $Chrome_Options.AddArguments (' start-maximized' )
44
- }
68
+ if ($Maximized ) {
69
+ $Chrome_Options.AddArguments (' start-maximized' )
70
+ }
45
71
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
- }
72
+ if ($Fullscreen ) {
73
+ $Chrome_Options.AddArguments (' start-fullscreen' )
74
+ }
75
+
76
+ if ($Arguments ) {
77
+ foreach ($Argument in $Arguments ){
78
+ $Chrome_Options.AddArguments ($Argument )
79
+ }
80
+ }
81
+
82
+ if (! $HideVersionHint ) {
83
+ Write-Host " Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" - ForegroundColor Yellow
84
+ }
85
+
86
+ if ($IsLinux -or $IsMacOS ){
87
+ $Driver = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver" - ArgumentList $AssembliesPath , $Chrome_Options
88
+ }
89
+ else {
90
+ $Driver = New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver" - ArgumentList $Chrome_Options
91
+ }
53
92
54
- if ($IsLinux -or $IsMacOS ){
55
- New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver" - ArgumentList $AssembliesPath , $Chrome_Options
93
+ if ($Minimized ){
94
+ $driver.Manage ().Window.Minimize();
95
+
96
+ }
97
+
98
+ if ($StartURL ){
99
+ Enter-SeUrl - Driver $Driver - Url $StartURL
100
+ }
56
101
}
57
- else {
58
- New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver " - ArgumentList $Chrome_Options
102
+ END {
103
+ return $Driver
59
104
}
60
105
}
61
106
@@ -264,20 +309,29 @@ function Get-SeCookie {
264
309
}
265
310
266
311
function Remove-SeCookie {
267
- param ($Driver )
268
-
269
- $Driver.Manage ().Cookies.DeleteAllCookies()
312
+ param (
313
+ $Driver ,
314
+ [switch ]$DeleteAllCookies ,
315
+ [string ]$Name
316
+ )
317
+
318
+ if ($DeleteAllCookies ){
319
+ $Driver.Manage ().Cookies.DeleteAllCookies()
320
+ }
321
+ else {
322
+ $Driver.Manage ().Cookies.DeleteCookieNamed($Name )
323
+ }
270
324
}
271
325
272
326
function Set-SeCookie {
273
327
param (
274
- $Driver ,
275
- [string ]$Name ,
328
+ [ ValidateNotNull ()] $Driver ,
329
+ [string ]$Name ,
276
330
[string ]$Value ,
277
331
[string ]$Path ,
278
332
[string ]$Domain ,
279
- [ datetime ] $ExpiryDate
280
- )
333
+ $ExpiryDate
334
+ )
281
335
282
336
<# Selenium Cookie Information
283
337
Cookie(String, String)
@@ -289,37 +343,44 @@ function Set-SeCookie {
289
343
Cookie(String, String, String, String, Nullable<DateTime>)
290
344
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
291
345
#>
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
346
+ Begin {
347
+ if ($ExpiryDate -ne $null -and $ExpiryDate.GetType ().Name -ne ' DateTime' ){
348
+ throw ' $ExpiryDate can only be $null or TypeName: System.DateTime'
349
+ }
301
350
}
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
351
+
352
+ Process {
353
+ if ($Name -and $Value -and (! $Path -and ! $Domain -and ! $ExpiryDate )){
354
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value
355
+ }
356
+ Elseif ($Name -and $Value -and $Path -and (! $Domain -and ! $ExpiryDate )){
357
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Path
358
+ }
359
+ Elseif ($Name -and $Value -and $Path -and $ExpiryDate -and ! $Domain ){
360
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Path , $ExpiryDate
361
+ }
362
+ Elseif ($Name -and $Value -and $Path -and $Domain -and (! $ExpiryDate -or $ExpiryDate )){
363
+ if ($Driver.Url -match $Domain ){
364
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $Value , $Domain , $Path , $ExpiryDate
365
+ }
366
+ else {
367
+ Throw ' In order to set the cookie the browser needs to be on the cookie domain URL'
368
+ }
305
369
}
306
370
else {
307
- Throw ' In order to set the cookie the browser needs to be on the cookie domain URL'
371
+ Throw " Incorrect Cookie Layout:
372
+ Cookie(String, String)
373
+ Initializes a new instance of the Cookie class with a specific name and value.
374
+ Cookie(String, String, String)
375
+ Initializes a new instance of the Cookie class with a specific name, value, and path.
376
+ Cookie(String, String, String, Nullable<DateTime>)
377
+ Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
378
+ Cookie(String, String, String, String, Nullable<DateTime>)
379
+ Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date."
308
380
}
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
381
322
- $Driver.Manage ().Cookies.AddCookie($cookie )
382
+ $Driver.Manage ().Cookies.AddCookie($cookie )
383
+ }
323
384
}
324
385
325
386
function Get-SeElementAttribute {
0 commit comments