Skip to content

Commit 9a54117

Browse files
committed
Enchance the Wait-SeElementExists/Start-SeChrome
I'm new to Source control please take it easy on me. 1) I've enchanced the Wait-SeElementExists function by adding the option to wait for elements by TagName/ClassName and I've also also converted the tickes to a timespan for more consistency. 2) I've enchanced the Start-SeChrome function by adding the ability yo set the browser default download path and the option to disable the built in pdf viewer.
1 parent 7120bef commit 9a54117

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

Selenium.psm1

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ function Start-SeChrome {
44
Param(
55
[Parameter(Mandatory = $false)]
66
[array]$Arguments,
7-
[switch]$HideVersionHint
7+
[switch]$HideVersionHint,
8+
[System.IO.FileInfo]$DefaultDownloadPath,
9+
[bool]$DisableBuiltInPDFViewer=$true
810
)
11+
12+
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
13+
if($DefaultDownloadPath){
14+
Write-Host "Setting Default Download directory: $DefaultDownloadPath"
15+
$Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
16+
}
17+
if($DisableBuiltInPDFViewer){
18+
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
19+
}
920
if($Arguments) {
10-
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
1121
$Chrome_Options.AddArguments($Arguments)
1222
}
1323
if(!$HideVersionHint)
@@ -199,12 +209,14 @@ function Save-SeScreenshot {
199209
}
200210
}
201211

202-
function Wait-SeElementExists {
212+
function Wait-SeElementExists{
203213
param(
204214
$Driver,
205215
$Timeout = 30,
206216
$Id,
207-
$Name
217+
$Name,
218+
$TagName,
219+
$ClassName
208220
)
209221
if($Id)
210222
{
@@ -214,11 +226,20 @@ function Wait-SeElementExists {
214226
{
215227
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
216228
}
229+
elseif($TagName)
230+
{
231+
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
232+
}
233+
elseif($ClassName)
234+
{
235+
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
236+
}
217237
else
218238
{
219-
throw "Please specify -Id or -Name"
239+
throw "Please specify -Id or -Name or -TagName or -ClassName"
220240
}
221-
$WebDriverWait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait $Driver, ($Timeout * 10000000) # ticks
241+
242+
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
222243
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
223244
$WebDriverWait.Until($Condition)
224245
}

0 commit comments

Comments
 (0)