Skip to content

Commit 77d5b7e

Browse files
committed
Enchanced Start-SeFirefox with many new features
Added many features to Start-SeFirefox and appropriate tests -StartURL -DefaultDownloadPath -Maximized -Minimize -Fullscreen -Headless -PrivateBrowsing Removed profile support since its broken. Will re-evaluate at a later date.
1 parent cdef593 commit 77d5b7e

File tree

2 files changed

+126
-17
lines changed

2 files changed

+126
-17
lines changed

Selenium.psm1

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ function Start-SeChrome {
111111

112112
if($Minimized){
113113
$driver.Manage().Window.Minimize();
114-
115114
}
116115

117116
if($Headless -and $DefaultDownloadPath) {
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

@@ -142,32 +140,88 @@ 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
199+
}
200+
201+
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromSeconds(10)
202+
203+
if($Minimized){
204+
$Driver.Manage().Window.Minimize()
205+
}
206+
207+
if($Maximized){
208+
$Driver.Manage().Window.Maximize()
209+
}
210+
211+
if($Fullscreen){
212+
$Driver.Manage().Window.FullScreen()
213+
}
214+
215+
if($StartURL){
216+
Enter-SeUrl -Driver $Driver -Url $StartURL
165217
}
166218
}
167-
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromSeconds(10)
168-
$Driver
219+
END{
220+
return $Driver
221+
}
169222
}
170223

224+
171225
function Stop-SeDriver {
172226
param($Driver)
173227

Selenium.tests.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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){

0 commit comments

Comments
 (0)