Skip to content

Commit 0a44c97

Browse files
authored
Merge pull request #32 from the-mentor/master
Linux/MacOS assemblies permission fix, Start-Chrome/Remove-SeCookie enchancements, Set-SeCookie $ExpiryDate issue fix.
2 parents 85d3182 + 26e5410 commit 0a44c97

File tree

3 files changed

+190
-89
lines changed

3 files changed

+190
-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: 143 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,118 @@ elseif($IsMacOS){
88
$AssembliesPath = "$PSScriptRoot/assemblies/macos"
99
}
1010

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+
1130
function Start-SeChrome {
1231
Param(
1332
[Parameter(Mandatory = $false)]
1433
[array]$Arguments,
1534
[switch]$HideVersionHint,
35+
[string]$StartURL,
1636
[System.IO.FileInfo]$DefaultDownloadPath,
37+
[System.IO.FileInfo]$ProfileDirectoryPath,
1738
[bool]$DisableBuiltInPDFViewer=$true,
1839
[switch]$Headless,
1940
[switch]$Incognito,
20-
[switch]$Maximized
41+
[switch]$Maximized,
42+
[switch]$Minimized,
43+
[switch]$Fullscreen
2144
)
2245

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+
}
3262
}
63+
PROCESS{
64+
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
3365

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

38-
if ($Incognito) {
39-
$Chrome_Options.AddArguments('Incognito')
40-
}
83+
if ($Incognito) {
84+
$Chrome_Options.AddArguments('Incognito')
85+
}
4186

42-
if ($Maximized) {
43-
$Chrome_Options.AddArguments('start-maximized')
44-
}
87+
if ($Maximized) {
88+
$Chrome_Options.AddArguments('start-maximized')
89+
}
4590

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

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+
}
56120
}
57-
else{
58-
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
121+
END{
122+
return $Driver
59123
}
60124
}
61125

@@ -264,20 +328,29 @@ function Get-SeCookie {
264328
}
265329

266330
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+
}
270343
}
271344

272345
function Set-SeCookie {
273346
param(
274-
$Driver,
275-
[string]$Name,
347+
[ValidateNotNull()]$Driver,
348+
[string]$Name,
276349
[string]$Value,
277350
[string]$Path,
278351
[string]$Domain,
279-
[datetime]$ExpiryDate
280-
)
352+
$ExpiryDate
353+
)
281354

282355
<# Selenium Cookie Information
283356
Cookie(String, String)
@@ -289,37 +362,44 @@ function Set-SeCookie {
289362
Cookie(String, String, String, String, Nullable<DateTime>)
290363
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
291364
#>
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+
}
301369
}
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+
}
305388
}
306389
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."
308399
}
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-
}
321400

322-
$Driver.Manage().Cookies.AddCookie($cookie)
401+
$Driver.Manage().Cookies.AddCookie($cookie)
402+
}
323403
}
324404

325405
function Get-SeElementAttribute {

0 commit comments

Comments
 (0)