Skip to content

Commit aa7bb89

Browse files
authored
Merge pull request #31 from the-mentor/master
BREAKING CHANGE - Find-SeElement -Wait / Improved Set-SeCookie
2 parents fb75924 + b153741 commit aa7bb89

File tree

4 files changed

+197
-69
lines changed

4 files changed

+197
-69
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.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = '.\Selenium.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.3.0'
15+
ModuleVersion = '1.4.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -86,7 +86,8 @@ FunctionsToExport = @(
8686
"Start-SeInternetExplorer",
8787
"Start-SeEdge",
8888
"Stop-SeDriver",
89-
"Wait-SeElementExists"
89+
"Get-SeWindow",
90+
"Switch-SeWindow"
9091
)
9192

9293
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.

Selenium.psm1

Lines changed: 146 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ function Start-SeChrome {
1515
[switch]$HideVersionHint,
1616
[System.IO.FileInfo]$DefaultDownloadPath,
1717
[bool]$DisableBuiltInPDFViewer=$true,
18-
[switch]$Headless
18+
[switch]$Headless,
19+
[switch]$Incognito,
20+
[switch]$Maximized
1921
)
2022

2123
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
@@ -33,6 +35,14 @@ function Start-SeChrome {
3335
$Chrome_Options.AddArguments('headless')
3436
}
3537

38+
if ($Incognito) {
39+
$Chrome_Options.AddArguments('Incognito')
40+
}
41+
42+
if ($Maximized) {
43+
$Chrome_Options.AddArguments('start-maximized')
44+
}
45+
3646
if ($Arguments) {
3747
$Chrome_Options.AddArguments($Arguments)
3848
}
@@ -50,7 +60,9 @@ function Start-SeChrome {
5060
}
5161

5262
function Start-SeInternetExplorer {
53-
New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerDriver"
63+
$InternetExplorer_Options = New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerOptions"
64+
$InternetExplorer_Options.IgnoreZoomLevel = $true
65+
New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerDriver" -ArgumentList $InternetExplorer_Options
5466
}
5567

5668
function Start-SeEdge {
@@ -102,6 +114,8 @@ function Find-SeElement {
102114
$Driver,
103115
[Parameter()]
104116
$Element,
117+
[Parameter()][Switch]$Wait,
118+
[Parameter()]$Timeout = 30,
105119
[Parameter(ParameterSetName = "ByCss")]
106120
$Css,
107121
[Parameter(ParameterSetName = "ByName")]
@@ -136,36 +150,75 @@ function Find-SeElement {
136150
"Driver or element must be specified"
137151
}
138152

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))
153-
}
154-
155-
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
156-
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
153+
if($Wait){
154+
if ($PSCmdlet.ParameterSetName -eq "ByName") {
155+
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
156+
}
157+
158+
if ($PSCmdlet.ParameterSetName -eq "ById") {
159+
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
160+
}
161+
162+
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
163+
$TargetElement = [OpenQA.Selenium.By]::LinkText($LinkText)
164+
}
165+
166+
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
167+
$TargetElement = [OpenQA.Selenium.By]::PartialLinkText($PartialLinkText)
168+
}
169+
170+
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
171+
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
172+
}
173+
174+
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
175+
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
176+
}
177+
178+
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
179+
$TargetElement = [OpenQA.Selenium.By]::XPath($XPath)
180+
}
181+
182+
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
183+
$TargetElement = [OpenQA.Selenium.By]::CssSelector($Css)
184+
}
185+
186+
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
187+
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
188+
$WebDriverWait.Until($Condition)
157189
}
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))
190+
else{
191+
if ($PSCmdlet.ParameterSetName -eq "ByName") {
192+
$Target.FindElements([OpenQA.Selenium.By]::Name($Name))
193+
}
194+
195+
if ($PSCmdlet.ParameterSetName -eq "ById") {
196+
$Target.FindElements([OpenQA.Selenium.By]::Id($Id))
197+
}
198+
199+
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
200+
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
201+
}
202+
203+
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
204+
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
205+
}
206+
207+
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
208+
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
209+
}
210+
211+
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
212+
$Target.FindElements([OpenQA.Selenium.By]::TagName($TagName))
213+
}
214+
215+
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
216+
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
217+
}
218+
219+
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
220+
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
221+
}
169222
}
170223
}
171224
}
@@ -217,10 +270,55 @@ function Remove-SeCookie {
217270
}
218271

219272
function Set-SeCookie {
220-
param($Driver, $name, $value)
273+
param(
274+
$Driver,
275+
[string]$Name,
276+
[string]$Value,
277+
[string]$Path,
278+
[string]$Domain,
279+
[datetime]$ExpiryDate
280+
)
281+
282+
<# Selenium Cookie Information
283+
Cookie(String, String)
284+
Initializes a new instance of the Cookie class with a specific name and value.
285+
Cookie(String, String, String)
286+
Initializes a new instance of the Cookie class with a specific name, value, and path.
287+
Cookie(String, String, String, Nullable<DateTime>)
288+
Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
289+
Cookie(String, String, String, String, Nullable<DateTime>)
290+
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
291+
#>
292+
293+
if($Name -and $Value -and (!$Path -and !$Domain -and !$ExpiryDate)){
294+
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value
295+
}
296+
Elseif($Name -and $Value -and $Path -and (!$Domain -and !$ExpiryDate)){
297+
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value,$Path
298+
}
299+
Elseif($Name -and $Value -and $Path -and $ExpiryDate -and !$Domain){
300+
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value,$Path,$ExpiryDate
301+
}
302+
Elseif($Name -and $Value -and $Path -and $ExpiryDate -and $Domain){
303+
if($Driver.Url -match $Domain){
304+
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value,$Domain,$Path,$ExpiryDate
305+
}
306+
else{
307+
Throw 'In order to set the cookie the browser needs to be on the cookie domain URL'
308+
}
309+
}
310+
else{
311+
Throw "Incorrect Cookie Layout:
312+
Cookie(String, String)
313+
Initializes a new instance of the Cookie class with a specific name and value.
314+
Cookie(String, String, String)
315+
Initializes a new instance of the Cookie class with a specific name, value, and path.
316+
Cookie(String, String, String, Nullable<DateTime>)
317+
Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
318+
Cookie(String, String, String, String, Nullable<DateTime>)
319+
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date."
320+
}
221321

222-
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name, $value
223-
224322
$Driver.Manage().Cookies.AddCookie($cookie)
225323
}
226324

@@ -263,37 +361,23 @@ function Save-SeScreenshot {
263361
}
264362
}
265363

266-
function Wait-SeElementExists{
364+
function Get-SeWindow {
267365
param(
268-
$Driver,
269-
$Timeout = 30,
270-
$Id,
271-
$Name,
272-
$TagName,
273-
$ClassName
366+
[Parameter(Mandatory = $true)][OpenQA.Selenium.IWebDriver]$Driver
274367
)
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-
}
293368

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)
369+
Process {
370+
$Driver.WindowHandles
371+
}
297372
}
298373

374+
function Switch-SeWindow {
375+
param(
376+
[Parameter(Mandatory = $true)][OpenQA.Selenium.IWebDriver]$Driver,
377+
[Parameter(Mandatory = $true)]$Window
378+
)
299379

380+
Process {
381+
$Driver.SwitchTo().Window($Window)|Out-Null
382+
}
383+
}

Selenium.tests.ps1

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

80+
Describe "Start-SeChrome with Options" {
81+
Context "Should Start Chrome Driver with different startup options" {
82+
It "Start Chrome in Headless mode"{
83+
$Driver = Start-SeChrome -Headless
84+
Stop-SeDriver $Driver
85+
}
86+
87+
It "Start Chrome Maximized "{
88+
$Driver = Start-SeChrome -Maximized
89+
Stop-SeDriver $Driver
90+
}
91+
92+
It "Start Chrome Incognito "{
93+
$Driver = Start-SeChrome -Incognito
94+
Stop-SeDriver $Driver
95+
}
96+
97+
It "Start Chrome Maximized and Incognito "{
98+
$Driver = Start-SeChrome -Maximized -Incognito
99+
Stop-SeDriver $Driver
100+
}
101+
}
102+
}
103+
80104
Describe "Start-SeFirefox" {
81105
Context "Should Start Firefox Driver" {
82106
$Driver = Start-SeFirefox
@@ -114,12 +138,31 @@ Describe "Get-SeCookie" {
114138

115139
Describe "Send-SeKeys" {
116140
$Driver = Start-SeFirefox
117-
Enter-SeUrl -Driver $Driver -Url "http://www.google.com"
141+
Enter-SeUrl -Driver $Driver -Url "http://www.google.com/ncr"
118142
Context "Find-SeElement" {
119143
It "By Css" {
120144
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
121145
Send-SeKeys -Element $SearchInput -Keys "test"
122146
}
123147
}
124148
Stop-SeDriver $Driver
125-
}
149+
}
150+
151+
Describe "Find-SeElement Firefox" {
152+
$Driver = Start-SeFirefox
153+
Enter-SeUrl -Driver $Driver -Url "http://www.google.com/ncr"
154+
Context "Find-SeElement" {
155+
It "By Css" {
156+
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
157+
}
158+
}
159+
160+
Context "Find-SeElement -Wait" {
161+
It "By Name"{
162+
$SearchInput = Find-SeElement -Driver $Driver -Wait -Name q -Timeout 60
163+
}
164+
}
165+
166+
Stop-SeDriver $Driver
167+
}
168+

0 commit comments

Comments
 (0)