Skip to content

Commit 7489941

Browse files
authored
Merge pull request #19 from vitalics/find-by-css
Add finding by css
2 parents 7120bef + 72a8c23 commit 7489941

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

Selenium.psm1

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ function Start-SeChrome {
66
[array]$Arguments,
77
[switch]$HideVersionHint
88
)
9-
if($Arguments) {
9+
if ($Arguments) {
1010
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
1111
$Chrome_Options.AddArguments($Arguments)
1212
}
13-
if(!$HideVersionHint)
14-
{
13+
if (!$HideVersionHint) {
1514
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
1615
}
1716
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
@@ -53,6 +52,8 @@ function Find-SeElement {
5352
$Driver,
5453
[Parameter()]
5554
$Element,
55+
[Parameter(ParameterSetName = "ByCss")]
56+
$Css,
5657
[Parameter(ParameterSetName = "ByName")]
5758
$Name,
5859
[Parameter(ParameterSetName = "ById")]
@@ -104,6 +105,9 @@ function Find-SeElement {
104105
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
105106
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
106107
}
108+
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
109+
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
110+
}
107111
}
108112
}
109113

@@ -115,11 +119,12 @@ function Invoke-SeClick {
115119
[Switch]$JavaScriptClick,
116120
[Parameter()]
117121
$Driver
118-
)
122+
)
119123

120124
if ($JavaScriptClick) {
121125
$Driver.ExecuteScript("arguments[0].click()", $Element)
122-
} else {
126+
}
127+
else {
123128
$Element.Click()
124129
}
125130

@@ -128,16 +133,16 @@ function Invoke-SeClick {
128133

129134
function Get-SeKeys {
130135

131-
[OpenQA.Selenium.Keys] | Get-Member -MemberType Property -Static | Select-Object -Property Name,@{N = "ObjectString"; E = {"[OpenQA.Selenium.Keys]::$($_.Name)"}}
136+
[OpenQA.Selenium.Keys] | Get-Member -MemberType Property -Static | Select-Object -Property Name, @{N = "ObjectString"; E = { "[OpenQA.Selenium.Keys]::$($_.Name)" } }
132137
}
133138

134139
function Send-SeKeys {
135140
param([OpenQA.Selenium.IWebElement]$Element, [string]$Keys)
136141

137-
foreach($Key in @(Get-SeKeys).Name)
138-
{
142+
foreach ($Key in @(Get-SeKeys).Name) {
139143
$Keys = $Keys -replace "{{$Key}}", [OpenQA.Selenium.Keys]::$Key
140144
}
145+
141146
$Element.SendKeys($Keys)
142147
}
143148

@@ -156,16 +161,16 @@ function Remove-SeCookie {
156161
function Set-SeCookie {
157162
param($Driver, $name, $value)
158163

159-
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$value
164+
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name, $value
160165

161166
$Driver.Manage().Cookies.AddCookie($cookie)
162167
}
163168

164169
function Get-SeElementAttribute {
165170
param(
166-
[Parameter(ValueFromPipeline=$true, Mandatory = $true)]
171+
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
167172
[OpenQA.Selenium.IWebElement]$Element,
168-
[Parameter(Mandatory=$true)]
173+
[Parameter(Mandatory = $true)]
169174
[string]$Attribute
170175
)
171176

@@ -180,7 +185,8 @@ function Invoke-SeScreenshot {
180185
$Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions]::TakeScreenshot($Driver)
181186
if ($AsBase64EncodedString) {
182187
$Screenshot.AsBase64EncodedString
183-
} else {
188+
}
189+
else {
184190
$Screenshot
185191
}
186192
}
@@ -194,9 +200,9 @@ function Save-SeScreenshot {
194200
[Parameter()]
195201
[OpenQA.Selenium.ScreenshotImageFormat]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat]::Png)
196202

197-
Process {
198-
$Screenshot.SaveAsFile($Path, $ImageFormat)
199-
}
203+
Process {
204+
$Screenshot.SaveAsFile($Path, $ImageFormat)
205+
}
200206
}
201207

202208
function Wait-SeElementExists {
@@ -206,16 +212,13 @@ function Wait-SeElementExists {
206212
$Id,
207213
$Name
208214
)
209-
if($Id)
210-
{
215+
if ($Id) {
211216
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
212217
}
213-
elseif($Name)
214-
{
218+
elseif ($Name) {
215219
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
216220
}
217-
else
218-
{
221+
else {
219222
throw "Please specify -Id or -Name"
220223
}
221224
$WebDriverWait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait $Driver, ($Timeout * 10000000) # ticks

Selenium.tests.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
Import-Module (Join-Path $PSScriptRoot "Selenium.psm1")
22

33
Describe "Get-SeCookie" {
4+
$Driver = Start-SeFirefox
45
Context "Should get cookies from google" {
5-
$Driver = Start-SeFirefox
66
Enter-SeUrl -Driver $Driver -Url "http://www.google.com"
77

88
Get-SeCookie $Driver
99
}
10+
Stop-SeDriver $Driver
11+
}
12+
13+
Describe "Send-SeKeys" {
14+
$Driver = Start-SeFirefox
15+
Enter-SeUrl -Driver $Driver -Url "http://www.google.com"
16+
Context "Find-SeElement" {
17+
It "By Css" {
18+
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
19+
Send-SeKeys -Element $SearchInput -Keys "test"
20+
}
21+
}
22+
Stop-SeDriver $Driver
1023
}

0 commit comments

Comments
 (0)