Skip to content

Commit bd15333

Browse files
committed
BREAKING CHANGE - Find-SeElement -Wait
1) Added -Wait option to Find-SeElement to simplify the find and wait process. 2) Added -Incognito to Start-SeChrome 3) Tweaked README.md to reflect the changes.
1 parent e3c5306 commit bd15333

File tree

3 files changed

+107
-70
lines changed

3 files changed

+107
-70
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Send-SeKeys -Element $Element -Keys "adam@poshtools.com"
7171
$Driver = Start-SeChrome -Headless
7272
7373
# Run Chrome in incognito mode
74-
$Driver = Start-SeChrome -Arguments "incognito"
74+
$Driver = Start-SeChrome -Incognito
7575
7676
# Run Chrome with alternative download folder
7777
$Driver = Start-SeChrome -DefaultDownloadPath c:\temp
@@ -81,6 +81,6 @@ $Driver = Start-SeChrome -DefaultDownloadPath c:\temp
8181
```powershell
8282
$Driver = Start-SeChrome
8383
Enter-SeUrl https://www.google.com -Driver $Driver
84-
Wait-SeElementExists -Driver $Driver -Timeout 3 -Id "q"
85-
Wait-SeElementExists -Driver $Driver -Timeout 3 -Name "q"
84+
Find-SeElement -Driver $d -Wait -Timeout 10 -Css input[name='q']
85+
Find-SeElement -Driver $d -Wait -Timeout 10 -Name q
8686
```

Selenium.psm1

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function Start-SeChrome {
1515
[switch]$HideVersionHint,
1616
[System.IO.FileInfo]$DefaultDownloadPath,
1717
[bool]$DisableBuiltInPDFViewer=$true,
18-
[switch]$Headless
18+
[switch]$Headless,
19+
[switch]$Incognito
1920
)
2021

2122
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
@@ -33,6 +34,10 @@ function Start-SeChrome {
3334
$Chrome_Options.AddArguments('headless')
3435
}
3536

37+
if ($Incognito) {
38+
$Chrome_Options.AddArguments('Incognito')
39+
}
40+
3641
if ($Arguments) {
3742
$Chrome_Options.AddArguments($Arguments)
3843
}
@@ -102,6 +107,8 @@ function Find-SeElement {
102107
$Driver,
103108
[Parameter()]
104109
$Element,
110+
[Parameter()][Switch]$Wait,
111+
[Parameter()]$Timeout = 30,
105112
[Parameter(ParameterSetName = "ByCss")]
106113
$Css,
107114
[Parameter(ParameterSetName = "ByName")]
@@ -136,36 +143,75 @@ function Find-SeElement {
136143
"Driver or element must be specified"
137144
}
138145

139-
if ($PSCmdlet.ParameterSetName -eq "ByName") {
140-
$Target.FindElements([OpenQA.Selenium.By]::Name($Name))
141-
}
142-
143-
if ($PSCmdlet.ParameterSetName -eq "ById") {
144-
$Target.FindElements([OpenQA.Selenium.By]::Id($Id))
145-
}
146-
147-
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
148-
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
149-
}
150-
151-
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
152-
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
146+
if($Wait){
147+
if ($PSCmdlet.ParameterSetName -eq "ByName") {
148+
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
149+
}
150+
151+
if ($PSCmdlet.ParameterSetName -eq "ById") {
152+
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
153+
}
154+
155+
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
156+
$TargetElement = [OpenQA.Selenium.By]::LinkText($LinkText)
157+
}
158+
159+
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
160+
$TargetElement = [OpenQA.Selenium.By]::PartialLinkText($PartialLinkText)
161+
}
162+
163+
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
164+
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
165+
}
166+
167+
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
168+
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
169+
}
170+
171+
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
172+
$TargetElement = [OpenQA.Selenium.By]::XPath($XPath)
173+
}
174+
175+
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
176+
$TargetElement = [OpenQA.Selenium.By]::CssSelector($Css)
177+
}
178+
179+
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
180+
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
181+
$WebDriverWait.Until($Condition)
153182
}
154-
155-
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
156-
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
157-
}
158-
159-
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
160-
$Target.FindElements([OpenQA.Selenium.By]::TagName($TagName))
161-
}
162-
163-
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
164-
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
165-
}
166-
167-
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
168-
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
183+
else{
184+
if ($PSCmdlet.ParameterSetName -eq "ByName") {
185+
$Target.FindElements([OpenQA.Selenium.By]::Name($Name))
186+
}
187+
188+
if ($PSCmdlet.ParameterSetName -eq "ById") {
189+
$Target.FindElements([OpenQA.Selenium.By]::Id($Id))
190+
}
191+
192+
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
193+
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
194+
}
195+
196+
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
197+
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
198+
}
199+
200+
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
201+
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
202+
}
203+
204+
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
205+
$Target.FindElements([OpenQA.Selenium.By]::TagName($TagName))
206+
}
207+
208+
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
209+
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
210+
}
211+
212+
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
213+
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
214+
}
169215
}
170216
}
171217
}
@@ -262,38 +308,3 @@ function Save-SeScreenshot {
262308
$Screenshot.SaveAsFile($Path, $ImageFormat)
263309
}
264310
}
265-
266-
function Wait-SeElementExists{
267-
param(
268-
$Driver,
269-
$Timeout = 30,
270-
$Id,
271-
$Name,
272-
$TagName,
273-
$ClassName
274-
)
275-
if ($Id) {
276-
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
277-
}
278-
elseif ($Name) {
279-
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
280-
}
281-
elseif($TagName)
282-
{
283-
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
284-
}
285-
elseif($ClassName)
286-
{
287-
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
288-
}
289-
else
290-
{
291-
throw "Please specify -Id or -Name or -TagName or -ClassName"
292-
}
293-
294-
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
295-
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
296-
$WebDriverWait.Until($Condition)
297-
}
298-
299-

Selenium.tests.ps1

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ Describe "Start-SeChrome headless" {
7777
}
7878
}
7979

80+
Describe "Start-SeChrome incognito" {
81+
Context "Should Start Chrome Driver in headless mode" {
82+
$Driver = Start-SeChrome -Incognito
83+
Stop-SeDriver $Driver
84+
}
85+
}
86+
8087
Describe "Start-SeFirefox" {
8188
Context "Should Start Firefox Driver" {
8289
$Driver = Start-SeFirefox
@@ -114,12 +121,31 @@ Describe "Get-SeCookie" {
114121

115122
Describe "Send-SeKeys" {
116123
$Driver = Start-SeFirefox
117-
Enter-SeUrl -Driver $Driver -Url "http://www.google.com"
124+
Enter-SeUrl -Driver $Driver -Url "http://www.google.com/ncr"
118125
Context "Find-SeElement" {
119126
It "By Css" {
120127
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
121128
Send-SeKeys -Element $SearchInput -Keys "test"
122129
}
123130
}
124131
Stop-SeDriver $Driver
125-
}
132+
}
133+
134+
Describe "Find-SeElement Firefox" {
135+
$Driver = Start-SeFirefox
136+
Enter-SeUrl -Driver $Driver -Url "http://www.google.com/ncr"
137+
Context "Find-SeElement" {
138+
It "By Css" {
139+
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
140+
}
141+
}
142+
143+
Context "Find-SeElement -Wait" {
144+
It "By Name"{
145+
$SearchInput = Find-SeElement -Driver $Driver -Wait -Name q -Timeout 60
146+
}
147+
}
148+
149+
Stop-SeDriver $Driver
150+
}
151+

0 commit comments

Comments
 (0)