Skip to content

Commit 4f00f9b

Browse files
committed
Group Changes #1 See Description for additional info
1) Added Start-SeChrome Minimized and Fullscreen 2) Added Start-SeChrome StartURL parameter to specify a URL that chrome will go to on startup. 3) Added the option to specify a ProfileDirectoryPath in Start-SeChrome 4) Added better handeling of multiple arguments in Start-SeChrome 5) Added the option to delete a cookie by name to Remove-SeCookie 6) Fixed an issue that prevented $null value in Set-SeCookie $ExpiryDate 7) Added appropriate tests to Selenium.tests.ps1 8) Made approprate changes to README.md to replace the above changes.
1 parent b153741 commit 4f00f9b

File tree

3 files changed

+171
-89
lines changed

3 files changed

+171
-89
lines changed

README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Import-Module "{FullPath}\selenium-powershell\Selenium.psm1"
1717
```
1818

1919
# Usage
20+
`Note: in order to use a specific driver you will need to have the brower of the driver installed on your system.
21+
For example if you use Start-SeChrome you will need to have either a Chrome or Chromium browser installed
22+
`
2023

2124
## Start a Browser Driver
2225
```powershell
@@ -38,15 +41,15 @@ $Driver = Start-SeFirefox
3841
Enter-SeUrl https://www.poshud.com -Driver $Driver
3942
```
4043

41-
## Find an element
44+
## Find an Element
4245

4346
```powershell
4447
$Driver = Start-SeFirefox
4548
Enter-SeUrl https://www.poshud.com -Driver $Driver
4649
$Element = Find-SeElement -Driver $Driver -Id "myControl"
4750
```
4851

49-
## Click on a button
52+
## Click on an Element/Button
5053

5154
```powershell
5255
$Driver = Start-SeFirefox
@@ -74,13 +77,29 @@ $Driver = Start-SeChrome -Headless
7477
$Driver = Start-SeChrome -Incognito
7578
7679
# Run Chrome with alternative download folder
77-
$Driver = Start-SeChrome -DefaultDownloadPath c:\temp
80+
$Driver = Start-SeChrome -DefaultDownloadPath C:\Temp
81+
82+
# Run Chrome and go to a URL in one command
83+
$Driver = Start-SeChrome -StartURL 'https://www.google.com/ncr'
84+
85+
# Run Chrome with multiple Arguments
86+
$Driver = Start-SeChrome -Arguments @('Incognito','start-maximized')
87+
88+
# Run Chrome with an existing profile.
89+
# The default profile paths are as follows:
90+
# Windows: C:\Users\<username>\AppData\Local\Google\Chrome\User Data
91+
# Linux: /home/<username>/.config/google-chrome
92+
# MacOS: /Users/<username>/Library/Application Support/Google/Chrome
93+
$Driver = Start-SeChrome -ProfileDirectoryPath '/home/<username>/.config/google-chrome'
94+
7895
```
7996

80-
## Wait for an element
97+
## Find and Wait for an element
8198
```powershell
8299
$Driver = Start-SeChrome
83-
Enter-SeUrl https://www.google.com -Driver $Driver
100+
Enter-SeUrl 'https://www.google.com/ncr' -Driver $Driver
101+
102+
# Please note that with the -Wait parameter only one element can be returned at a time.
84103
Find-SeElement -Driver $d -Wait -Timeout 10 -Css input[name='q']
85104
Find-SeElement -Driver $d -Wait -Timeout 10 -Name q
86105
```

Selenium.psm1

Lines changed: 124 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,94 @@ function Start-SeChrome {
1313
[Parameter(Mandatory = $false)]
1414
[array]$Arguments,
1515
[switch]$HideVersionHint,
16+
[string]$StartURL,
1617
[System.IO.FileInfo]$DefaultDownloadPath,
18+
[System.IO.FileInfo]$ProfileDirectoryPath,
1719
[bool]$DisableBuiltInPDFViewer=$true,
1820
[switch]$Headless,
1921
[switch]$Incognito,
20-
[switch]$Maximized
22+
[switch]$Maximized,
23+
[switch]$Minimized,
24+
[switch]$Fullscreen
2125
)
2226

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+
}
3243
}
44+
PROCESS{
45+
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
3346

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+
}
3763

38-
if ($Incognito) {
39-
$Chrome_Options.AddArguments('Incognito')
40-
}
64+
if ($Incognito) {
65+
$Chrome_Options.AddArguments('Incognito')
66+
}
4167

42-
if ($Maximized) {
43-
$Chrome_Options.AddArguments('start-maximized')
44-
}
68+
if ($Maximized) {
69+
$Chrome_Options.AddArguments('start-maximized')
70+
}
4571

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+
}
5392

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+
}
56101
}
57-
else{
58-
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
102+
END{
103+
return $Driver
59104
}
60105
}
61106

@@ -264,20 +309,29 @@ function Get-SeCookie {
264309
}
265310

266311
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+
}
270324
}
271325

272326
function Set-SeCookie {
273327
param(
274-
$Driver,
275-
[string]$Name,
328+
[ValidateNotNull()]$Driver,
329+
[string]$Name,
276330
[string]$Value,
277331
[string]$Path,
278332
[string]$Domain,
279-
[datetime]$ExpiryDate
280-
)
333+
$ExpiryDate
334+
)
281335

282336
<# Selenium Cookie Information
283337
Cookie(String, String)
@@ -289,37 +343,44 @@ function Set-SeCookie {
289343
Cookie(String, String, String, String, Nullable<DateTime>)
290344
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
291345
#>
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+
}
301350
}
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+
}
305369
}
306370
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."
308380
}
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-
}
321381

322-
$Driver.Manage().Cookies.AddCookie($cookie)
382+
$Driver.Manage().Cookies.AddCookie($cookie)
383+
}
323384
}
324385

325386
function Get-SeElementAttribute {

0 commit comments

Comments
 (0)