Skip to content

Commit f030a19

Browse files
authored
Merge pull request #35 from the-mentor/master
Version Bump to 2.0 and Enchanced Start-SeFirefox with many new features
2 parents b76093f + a9d41ee commit f030a19

12 files changed

+153
-43
lines changed

Diff for: Examples/A-Simple-Google-Search.ps1

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ if($Driver){
4040

4141
# The $AllInputElements contains all the input elements on the page if we want to find the specific element for the google search box we will need to loop through each input element in the $AllInputElements array and get the attibute we want in this case we're looking for the title attribute.
4242
# And we only want to return the element that has a title equal to Search we can find this out based on the html code on the page.
43-
$SearchBoxElement = $AllInputElements|%{if($_.GetAttribute('title') -eq 'Search'){return $_}}
43+
$SearchBoxElement = $AllInputElements|ForEach-Object{if($_.GetAttribute('title') -eq 'Search'){return $_}}
4444

4545
# Now for the fun part after finding the element we want to send keyboard input to it. this will allow us to automate the search process
4646
# We can get the list of all special keyboard keys like enter/backspace etc using the Get-SeKeys command
@@ -51,15 +51,15 @@ if($Driver){
5151
# You can send special key strokes to the SearchBoxElement, you should use the Selenium Keys enum. For example, if we wanted to send an enter key stroke, you could do it like this
5252
Send-SeKeys -Element $SearchBoxElement -Keys ([OpenQA.Selenium.Keys]::Enter)
5353

54-
# When working with dynamic websites, its often necessary to wait awhile for elements to appear on the page. By default, Selenium wont wait and youll receive $null from Find-SeElement because the element isnt there yet. There are a couple ways to work around this.
55-
# The first is to use the Wait-SeElementExists cmdlet to wait for the existence of an element in the document.
56-
# The Wait-SeElementExists will also return the element once it is found so you can use in order to wait and then find elements on the page.
54+
# When working with dynamic websites, it's often necessary to wait awhile for elements to appear on the page. By default, Selenium won't wait and you'll receive $null from Find-SeElement because the element isnt there yet. There are a couple ways to work around this.
55+
# The first is to use the Find-SeElement cmdlet with the -Wait switch to wait for the existence of an element in the document.
56+
# When using the Find-SeElement with the -Wait please take into account that only 1 element can be returned unlike the without the -Wait switch where multiple elements can be returned.
5757

5858
# This command will wait for the img elements for 10 seconds and then return it to you or time out if the element wasn't found on.
59-
$ImageElements = Wait-SeElementExists -Driver $Driver -TagName 'img' -Timeout 10
60-
59+
$LogoImageElement = Find-SeElement -Driver $Driver -Wait -Timeout 10 -Id 'logo'
60+
6161
# Once we have the image element we can simulate a mouse click on it using the Invoke-SeClick command.
62-
Invoke-SeClick -Driver $Driver -Element $ImageElements
62+
Invoke-SeClick -Driver $Driver -Element $LogoImageElement
6363

6464
# Once we are done with the web driver and we finished with all our testing/automation we can release the driver by running the Stop-SeDriver command.
6565
Stop-SeDriver -Driver $Driver

Diff for: Selenium.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = '.\Selenium.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.4.0'
15+
ModuleVersion = '2.0.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

Diff for: Selenium.psm1

+82-27
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function Start-SeChrome {
4444
)
4545

4646
BEGIN{
47-
if($Maximized -ne $false -and $Minimized -ne $false) {
47+
if($Maximized -ne $false -and $Minimized -ne $false){
4848
throw 'Maximized and Minimized may not be specified together.'
4949
}
5050
elseif($Maximized -ne $false -and $Fullscreen -ne $false){
@@ -76,29 +76,29 @@ function Start-SeChrome {
7676
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
7777
}
7878

79-
if ($Headless) {
79+
if($Headless){
8080
$Chrome_Options.AddArguments('headless')
8181
}
8282

83-
if ($Incognito) {
83+
if($Incognito){
8484
$Chrome_Options.AddArguments('Incognito')
8585
}
8686

87-
if ($Maximized) {
87+
if($Maximized){
8888
$Chrome_Options.AddArguments('start-maximized')
8989
}
9090

91-
if ($Fullscreen) {
91+
if($Fullscreen){
9292
$Chrome_Options.AddArguments('start-fullscreen')
9393
}
9494

95-
if ($Arguments) {
95+
if($Arguments){
9696
foreach ($Argument in $Arguments){
9797
$Chrome_Options.AddArguments($Argument)
9898
}
9999
}
100100

101-
if (!$HideVersionHint) {
101+
if(!$HideVersionHint){
102102
Write-Verbose "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'"
103103
}
104104

@@ -109,20 +109,18 @@ function Start-SeChrome {
109109
$Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
110110
}
111111

112-
if($Minimized){
112+
if($Minimized -and $Driver){
113113
$driver.Manage().Window.Minimize();
114-
115114
}
116115

117-
if($Headless -and $DefaultDownloadPath) {
116+
if($Headless -and $DefaultDownloadPath -and $Driver){
118117
$HeadlessDownloadParams = New-Object 'system.collections.generic.dictionary[[System.String],[System.Object]]]'
119118
$HeadlessDownloadParams.Add('behavior', 'allow')
120119
$HeadlessDownloadParams.Add('downloadPath', $DefaultDownloadPath.FullName)
121-
122120
$Driver.ExecuteChromeCommand('Page.setDownloadBehavior', $HeadlessDownloadParams)
123121
}
124122

125-
if($StartURL){
123+
if($StartURL -and $Driver){
126124
Enter-SeUrl -Driver $Driver -Url $StartURL
127125
}
128126
}
@@ -142,30 +140,87 @@ function Start-SeEdge {
142140
}
143141

144142
function Start-SeFirefox {
145-
param([Switch]$Profile)
143+
param(
144+
[array]$Arguments,
145+
[string]$StartURL,
146+
[System.IO.FileInfo]$DefaultDownloadPath,
147+
[switch]$Headless,
148+
[switch]$PrivateBrowsing,
149+
[switch]$Maximized,
150+
[switch]$Minimized,
151+
[switch]$Fullscreen
152+
)
146153

147-
if ($Profile) {
148-
#Doesn't work....
149-
$ProfilePath = Join-Path $PSScriptRoot "Assets\ff-profile\rust_mozprofile.YwpEBLY3hCRX"
150-
$firefoxProfile = New-Object OpenQA.Selenium.Firefox.FirefoxProfile -ArgumentList ($ProfilePath)
151-
$firefoxProfile.WriteToDisk()
152-
if($IsLinux -or $IsMacOS){
153-
New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver" -ArgumentList $AssembliesPath,$firefoxProfile
154+
BEGIN{
155+
if($Maximized -ne $false -and $Minimized -ne $false){
156+
throw 'Maximized and Minimized may not be specified together.'
154157
}
155-
else{
156-
New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver" -ArgumentList $firefoxProfile
158+
elseif($Maximized -ne $false -and $Fullscreen -ne $false){
159+
throw 'Maximized and Fullscreen may not be specified together.'
160+
}
161+
elseif($Minimized -ne $false -and $Fullscreen -ne $false){
162+
throw 'Minimized and Fullscreen may not be specified together.'
163+
}
164+
165+
if($StartURL){
166+
if(![system.uri]::IsWellFormedUriString($StartURL,[System.UriKind]::Absolute)){
167+
throw 'Incorrect StartURL please make sure the URL starts with http:// or https://'
168+
}
157169
}
158170
}
159-
else {
171+
PROCESS{
172+
$Firefox_Options = New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxOptions"
173+
174+
if($Headless){
175+
$Firefox_Options.AddArguments('-headless')
176+
}
177+
178+
if($DefaultDownloadPath){
179+
Write-Verbose "Setting Default Download directory: $DefaultDownloadPath"
180+
$Firefox_Options.setPreference("browser.download.folderList",2);
181+
$Firefox_Options.SetPreference("browser.download.dir", "$DefaultDownloadPath");
182+
}
183+
184+
if($PrivateBrowsing){
185+
$Firefox_Options.SetPreference("browser.privatebrowsing.autostart", $true)
186+
}
187+
188+
if($Arguments){
189+
foreach ($Argument in $Arguments){
190+
$Firefox_Options.AddArguments($Argument)
191+
}
192+
}
193+
160194
if($IsLinux -or $IsMacOS){
161-
$Driver = New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver" -ArgumentList $AssembliesPath
195+
$Driver = New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver" -ArgumentList $AssembliesPath,$Firefox_Options
162196
}
163197
else{
164-
$Driver = New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver"
198+
$Driver = New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver" -ArgumentList $Firefox_Options
165199
}
200+
201+
if($Driver){
202+
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromSeconds(10)
203+
}
204+
205+
if($Minimized -and $Driver){
206+
$Driver.Manage().Window.Minimize()
207+
}
208+
209+
if($Maximized -and $Driver){
210+
$Driver.Manage().Window.Maximize()
211+
}
212+
213+
if($Fullscreen -and $Driver){
214+
$Driver.Manage().Window.FullScreen()
215+
}
216+
217+
if($StartURL -and $Driver){
218+
Enter-SeUrl -Driver $Driver -Url $StartURL
219+
}
220+
}
221+
END{
222+
return $Driver
166223
}
167-
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromSeconds(10)
168-
$Driver
169224
}
170225

171226
function Stop-SeDriver {

Diff for: Selenium.tests.ps1

+59-4
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ Describe "Verify the Binaries SHA256 Hash" {
1515

1616
It "Check ChromeDriver.exe Hash" {
1717
# The ChromeDriver.exe was extracted from https://chromedriver.storage.googleapis.com/76.0.3809.68/chromedriver_win32.zip its VirusTotal Scan URL - https://www.virustotal.com/gui/url/69ffe387a3fa4fbf8a108391580f1a0befb8b96b82486da4417cfcdab4add4d4/detection
18-
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/66cfa645f83fde41720beac7061a559fd57b6f5caa83d7918f44de0f4dd27845/detection
18+
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/297e5f1be088a60b0fbfd67bf6b6e9d3aff4acbe228cddd77642a9e270cb4c30/detection
1919
$Hash = (Get-FileHash -Algorithm SHA256 -Path $PSScriptRoot\assemblies\chromedriver.exe).Hash
2020
$Hash |Should Be (Get-Content -Path $PSScriptRoot\assemblies\chromedriver.exe.sha256)
2121
}
2222

2323
It "Check ChromeDriver Linux Hash" {
24-
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/3da69344b8b2b3b7e1497378672231a179eed6b3a0fdccbfacd3d053612e2547/detection
24+
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/da1ff4d52963446f679b2175fc05af020fd4e02b92b7b3447ed51fab8f4f4d28/detection
2525
$Hash = (Get-FileHash -Algorithm SHA256 -Path $PSScriptRoot\assemblies\linux\chromedriver).Hash
2626
$Hash |Should Be (Get-Content -Path $PSScriptRoot\assemblies\linux\chromedriver.sha256)
2727
}
2828

2929
It "Check ChromeDriver MacOS Hash" {
30-
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/57097bb65200f003152906c831ccd226ebbd5a9fd47df46f18adc29f7d01f2f0/detection
30+
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/e19d7c2806adaea658c3f5465da0231c090e354d1339d1544a9f92501964a471/detection
3131
$Hash = (Get-FileHash -Algorithm SHA256 -Path $PSScriptRoot\assemblies\macos\chromedriver).Hash
3232
$Hash |Should Be (Get-Content -Path $PSScriptRoot\assemblies\macos\chromedriver.sha256)
3333
}
3434

3535
It "Check GeckoDriver.exe Hash" {
36-
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/1ae81b2a6f40f7d11be3c91c4d83977ae0c0897bd5d154c02a6d869b58866b58/detection
36+
# VirusTotal Scan URL = https://www.virustotal.com/gui/file/3104a5ba26ff22962d0d75536506c081939bcd7580ba16503d4f3ce5507d06d2/detection
3737
$Hash = (Get-FileHash -Algorithm SHA256 -Path $PSScriptRoot\assemblies\geckodriver.exe).Hash
3838
$Hash |Should Be (Get-Content -Path $PSScriptRoot\assemblies\geckodriver.exe.sha256)
3939
}
@@ -72,11 +72,21 @@ Describe "Start-SeChrome" {
7272

7373
Describe "Start-SeChrome with Options" {
7474
Context "Should Start Chrome Driver with different startup options" {
75+
It "Start Chrome with StartURL" {
76+
$Driver = Start-SeChrome -StartURL 'https://github.com/adamdriscoll/selenium-powershell'
77+
Stop-SeDriver $Driver
78+
}
79+
7580
It "Start Chrome in Headless mode" {
7681
$Driver = Start-SeChrome -Headless
7782
Stop-SeDriver $Driver
7883
}
7984

85+
It "Start Chrome Minimize" {
86+
$Driver = Start-SeChrome -Minimize
87+
Stop-SeDriver $Driver
88+
}
89+
8090
It "Start Chrome Maximized" {
8191
$Driver = Start-SeChrome -Maximized
8292
Stop-SeDriver $Driver
@@ -99,6 +109,7 @@ Describe "Start-SeChrome with Options" {
99109

100110
It "Start Chrome with Multiple arguments" {
101111
$Driver = Start-SeChrome -Arguments @('Incognito','start-maximized')
112+
Stop-SeDriver $Driver
102113
}
103114
}
104115
}
@@ -110,6 +121,50 @@ Describe "Start-SeFirefox"{
110121
}
111122
}
112123

124+
Describe "Start-SeFirefox with Options" {
125+
Context "Should Start Firefox Driver with different startup options" {
126+
It "Start Firefox with StartURL" {
127+
$Driver = Start-SeFirefox -StartURL 'https://github.com/adamdriscoll/selenium-powershell'
128+
Stop-SeDriver $Driver
129+
}
130+
131+
It "Start Firefox in Headless mode" {
132+
$Driver = Start-SeFirefox -Headless
133+
Stop-SeDriver $Driver
134+
}
135+
136+
It "Start Firefox Minimize" {
137+
$Driver = Start-SeFirefox -Minimize
138+
Stop-SeDriver $Driver
139+
}
140+
141+
It "Start Firefox Maximized" {
142+
$Driver = Start-SeFirefox -Maximized
143+
Stop-SeDriver $Driver
144+
}
145+
146+
It "Start Firefox PrivateBrowsing (Incognito)" {
147+
$Driver = Start-SeFirefox -PrivateBrowsing
148+
Stop-SeDriver $Driver
149+
}
150+
151+
It "Start Firefox Fullscreen" {
152+
$Driver = Start-SeFirefox -Fullscreen
153+
Stop-SeDriver $Driver
154+
}
155+
156+
It "Start Firefox Maximized and PrivateBrowsing (Incognito)" {
157+
$Driver = Start-SeFirefox -Maximized -PrivateBrowsing
158+
Stop-SeDriver $Driver
159+
}
160+
161+
It "Start Firefox with Multiple arguments" {
162+
$Driver = Start-SeFirefox -Arguments @('-headless','-private')
163+
Stop-SeDriver $Driver
164+
}
165+
}
166+
}
167+
113168
Describe "Start-SeEdge" {
114169
Context "Should Start Edge Driver" {
115170
if(!$IsLinux -and !$IsMacOS){

Diff for: assemblies/chromedriver.exe

512 Bytes
Binary file not shown.

Diff for: assemblies/chromedriver.exe.sha256

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
66CFA645F83FDE41720BEAC7061A559FD57B6F5CAA83D7918F44DE0F4DD27845
1+
297E5F1BE088A60B0FBFD67BF6B6E9D3AFF4ACBE228CDDD77642A9E270CB4C30

Diff for: assemblies/geckodriver.exe

-2.38 MB
Binary file not shown.

Diff for: assemblies/geckodriver.exe.sha256

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1AE81B2A6F40F7D11BE3C91C4D83977AE0C0897BD5D154C02A6D869B58866B58
1+
3104A5BA26FF22962D0D75536506C081939BCD7580BA16503D4F3CE5507D06D2

Diff for: assemblies/linux/chromedriver

0 Bytes
Binary file not shown.

Diff for: assemblies/linux/chromedriver.sha256

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3DA69344B8B2B3B7E1497378672231A179EED6B3A0FDCCBFACD3D053612E2547
1+
DA1FF4D52963446F679B2175FC05AF020FD4E02B92B7B3447ED51FAB8F4F4D28

Diff for: assemblies/macos/chromedriver

0 Bytes
Binary file not shown.

Diff for: assemblies/macos/chromedriver.sha256

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
57097BB65200F003152906C831CCD226EBBD5A9FD47DF46F18ADC29F7D01F2F0
1+
E19D7C2806ADAEA658C3F5465DA0231C090E354D1339D1544A9F92501964A471

0 commit comments

Comments
 (0)