From b770c23b8bf1db12092c4e0f9072f61855f2a541 Mon Sep 17 00:00:00 2001 From: Craig McGregor Date: Sun, 19 Jan 2020 05:18:14 +1300 Subject: [PATCH] Add custom DriverPath to Start-SeChrome (#59) * Add DriverPath to Start-SeChrome to enable loading a specific version of ChromeDriver. * clean up scoping debug test --- Selenium.psm1 | 12 +++++++++--- Selenium.tests.ps1 | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/Selenium.psm1 b/Selenium.psm1 index bcd73f3..a7d26c6 100644 --- a/Selenium.psm1 +++ b/Selenium.psm1 @@ -49,7 +49,10 @@ function Start-SeChrome { [switch]$Maximized, [switch]$Minimized, [switch]$Fullscreen, - [System.IO.FileInfo]$ChromeBinaryPath + [System.IO.FileInfo]$ChromeBinaryPath, + + [ValidateScript({ if ($_) {Test-Path -Path $_ -PathType Container} })] + [string]$DriverPath ) BEGIN{ @@ -118,11 +121,14 @@ function Start-SeChrome { Write-Verbose "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" } - if($IsLinux -or $IsMacOS){ + if ($DriverPath){ + $Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $DriverPath,$Chrome_Options + } + elseif($IsLinux -or $IsMacOS){ $Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $AssembliesPath,$Chrome_Options } else{ - $Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options + $Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options } if($Minimized -and $Driver){ diff --git a/Selenium.tests.ps1 b/Selenium.tests.ps1 index ed5c9db..0233df5 100644 --- a/Selenium.tests.ps1 +++ b/Selenium.tests.ps1 @@ -113,6 +113,48 @@ Describe "Start-SeChrome with Options" { } } +Describe 'Start-SeChrome with Custom DriverPath' { + BeforeEach { + # It is unknown which alternate drivers are present on CI agent, so use existing driver as a custom DriverPath for tests + if ($IsLinux) { + $driverPath = Join-Path $PSScriptRoot 'assemblies/macos' + } + elseif($IsMacOS) { + $driverPath = Join-Path $PSScriptRoot 'assemblies/linux' + } + else { #windows + $driverPath = Join-Path $PSScriptRoot 'assemblies' + } + + $badDriverPath = Join-Path $PSScriptRoot 'bad-assembly-path' + } + + Context 'DriverPath With no additional arguments' { + It 'Start-SeChrome from Custom DriverPath with no arguments' { + $Driver = Start-SeChrome -DriverPath $driverPath + $Driver | Should Not BeNullOrEmpty + Stop-SeDriver $Driver + } + + It 'Start-SeChrome should fail if custom DriverPath does not exist' { + { Start-SeChrome -DriverPath $badDriverPath } | Should Throw + } + } + + Context 'DriverPath With additional arguments' { + It 'Start-SeChrome from Custom DriverPath with Multiple arguments' { + $Driver = Start-SeChrome -DriverPath $driverPath -Arguments @('Incognito','start-maximized') + $Driver | Should Not BeNullOrEmpty + Stop-SeDriver $Driver + } + + It 'Start-SeChrome with multiple arguments should fail if custom DriverPath does not exist' { + { + Start-SeChrome -DriverPath $badDriverPath -Arguments @('Incognito','start-maximized') } | Should Throw + } + } +} + Describe "Start-SeFirefox"{ Context "Should Start Firefox Driver" { $Driver = Start-SeFirefox