Skip to content

Commit c3ce393

Browse files
authored
Merge pull request #20 from the-mentor/master
Enchance the Wait-SeElementExists/Start-SeChrome
2 parents 7489941 + 9c5446e commit c3ce393

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

Selenium.psm1

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@ 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+
14+
if($DefaultDownloadPath){
15+
Write-Host "Setting Default Download directory: $DefaultDownloadPath"
16+
$Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
17+
}
18+
19+
if($DisableBuiltInPDFViewer){
20+
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
21+
}
22+
923
if ($Arguments) {
10-
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
1124
$Chrome_Options.AddArguments($Arguments)
1225
}
26+
1327
if (!$HideVersionHint) {
1428
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
1529
}
@@ -62,10 +76,16 @@ function Find-SeElement {
6276
$ClassName,
6377
[Parameter(ParameterSetName = "ByLinkText")]
6478
$LinkText,
79+
[Parameter(ParameterSetName = "ByPartialLinkText")]
80+
$PartialLinkText,
6581
[Parameter(ParameterSetName = "ByTagName")]
6682
$TagName,
6783
[Parameter(ParameterSetName = "ByXPath")]
68-
$XPath)
84+
$XPath,
85+
[Parameter(ParameterSetName = "ByCss")]
86+
$Css
87+
)
88+
6989

7090
Process {
7191

@@ -94,6 +114,10 @@ function Find-SeElement {
94114
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
95115
}
96116

117+
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
118+
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
119+
}
120+
97121
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
98122
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
99123
}
@@ -105,6 +129,7 @@ function Find-SeElement {
105129
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
106130
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
107131
}
132+
108133
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
109134
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
110135
}
@@ -205,23 +230,35 @@ function Save-SeScreenshot {
205230
}
206231
}
207232

208-
function Wait-SeElementExists {
233+
function Wait-SeElementExists{
209234
param(
210235
$Driver,
211236
$Timeout = 30,
212237
$Id,
213-
$Name
238+
$Name,
239+
$TagName,
240+
$ClassName
214241
)
215242
if ($Id) {
216243
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
217244
}
218245
elseif ($Name) {
219246
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
220247
}
221-
else {
222-
throw "Please specify -Id or -Name"
248+
elseif($TagName)
249+
{
250+
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
251+
}
252+
elseif($ClassName)
253+
{
254+
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
223255
}
224-
$WebDriverWait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait $Driver, ($Timeout * 10000000) # ticks
256+
else
257+
{
258+
throw "Please specify -Id or -Name or -TagName or -ClassName"
259+
}
260+
261+
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
225262
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
226263
$WebDriverWait.Until($Condition)
227264
}

0 commit comments

Comments
 (0)