Skip to content

Commit 195dd77

Browse files
authored
Merge branch 'master' into master
2 parents 69d4155 + c3ce393 commit 195dd77

10 files changed

+136
-13
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
Install-Module Selenium
1111
```
1212

13+
OR
14+
15+
```
16+
Import-Module "{FullPath}\selenium-powershell\Selenium.psm1"
17+
```
18+
1319
# Usage
1420

1521
## Navigate to a URL
@@ -43,4 +49,18 @@ $Driver = Start-SeFirefox
4349
Enter-SeUrl https://www.poshud.com -Driver $Driver
4450
$Element = Find-SeElement -Driver $Driver -Id "txtEmail"
4551
Send-SeKeys -Element $Element -Keys "[email protected]"
52+
```
53+
54+
## Run Chrome with options
55+
56+
```powershell
57+
$Driver = Start-SeChrome -Arguments "headless","incognito"
58+
```
59+
60+
## Wait for an element
61+
```powershell
62+
$Driver = Start-SeChrome
63+
Enter-SeUrl https://www.google.com -Driver $Driver
64+
Wait-SeElementExists -Driver $Driver -Timeout 3 -Id "q"
65+
Wait-SeElementExists -Driver $Driver -Timeout 3 -Name "q"
4666
```

Selenium.psd1

-4.43 KB
Binary file not shown.

Selenium.psm1

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,33 @@
22
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\assemblies\WebDriver.Support.dll")
33

44
function Start-SeChrome {
5-
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver"
5+
Param(
6+
[Parameter(Mandatory = $false)]
7+
[array]$Arguments,
8+
[switch]$HideVersionHint,
9+
[System.IO.FileInfo]$DefaultDownloadPath,
10+
[bool]$DisableBuiltInPDFViewer=$true
11+
)
12+
13+
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
14+
15+
if($DefaultDownloadPath){
16+
Write-Host "Setting Default Download directory: $DefaultDownloadPath"
17+
$Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
18+
}
19+
20+
if($DisableBuiltInPDFViewer){
21+
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
22+
}
23+
24+
if ($Arguments) {
25+
$Chrome_Options.AddArguments($Arguments)
26+
}
27+
28+
if (!$HideVersionHint) {
29+
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
30+
}
31+
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
632
}
733

834
function Start-SeIe {
@@ -45,6 +71,8 @@ function Find-SeElement {
4571
$Driver,
4672
[Parameter()]
4773
$Element,
74+
[Parameter(ParameterSetName = "ByCss")]
75+
$Css,
4876
[Parameter(ParameterSetName = "ByName")]
4977
$Name,
5078
[Parameter(ParameterSetName = "ById")]
@@ -53,10 +81,16 @@ function Find-SeElement {
5381
$ClassName,
5482
[Parameter(ParameterSetName = "ByLinkText")]
5583
$LinkText,
84+
[Parameter(ParameterSetName = "ByPartialLinkText")]
85+
$PartialLinkText,
5686
[Parameter(ParameterSetName = "ByTagName")]
5787
$TagName,
5888
[Parameter(ParameterSetName = "ByXPath")]
59-
$XPath)
89+
$XPath,
90+
[Parameter(ParameterSetName = "ByCss")]
91+
$Css
92+
)
93+
6094

6195
Process {
6296

@@ -85,6 +119,10 @@ function Find-SeElement {
85119
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
86120
}
87121

122+
if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
123+
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
124+
}
125+
88126
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
89127
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
90128
}
@@ -96,6 +134,10 @@ function Find-SeElement {
96134
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
97135
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
98136
}
137+
138+
if ($PSCmdlet.ParameterSetName -eq "ByCss") {
139+
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
140+
}
99141
}
100142
}
101143

@@ -107,19 +149,29 @@ function Invoke-SeClick {
107149
[Switch]$JavaScriptClick,
108150
[Parameter()]
109151
$Driver
110-
)
152+
)
111153

112154
if ($JavaScriptClick) {
113155
$Driver.ExecuteScript("arguments[0].click()", $Element)
114-
} else {
156+
}
157+
else {
115158
$Element.Click()
116159
}
117160

118161
}
119162

163+
function Get-SeKeys {
164+
165+
[OpenQA.Selenium.Keys] | Get-Member -MemberType Property -Static | Select-Object -Property Name, @{N = "ObjectString"; E = { "[OpenQA.Selenium.Keys]::$($_.Name)" } }
166+
}
167+
120168
function Send-SeKeys {
121169
param([OpenQA.Selenium.IWebElement]$Element, [string]$Keys)
122-
170+
171+
foreach ($Key in @(Get-SeKeys).Name) {
172+
$Keys = $Keys -replace "{{$Key}}", [OpenQA.Selenium.Keys]::$Key
173+
}
174+
123175
$Element.SendKeys($Keys)
124176
}
125177

@@ -138,16 +190,16 @@ function Remove-SeCookie {
138190
function Set-SeCookie {
139191
param($Driver, $name, $value)
140192

141-
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$value
193+
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name, $value
142194

143195
$Driver.Manage().Cookies.AddCookie($cookie)
144196
}
145197

146198
function Get-SeElementAttribute {
147199
param(
148-
[Parameter(ValueFromPipeline=$true, Mandatory = $true)]
200+
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
149201
[OpenQA.Selenium.IWebElement]$Element,
150-
[Parameter(Mandatory=$true)]
202+
[Parameter(Mandatory = $true)]
151203
[string]$Attribute
152204
)
153205

@@ -160,9 +212,12 @@ function Invoke-SeScreenshot {
160212
param($Driver, [Switch]$AsBase64EncodedString)
161213

162214
$Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions]::TakeScreenshot($Driver)
163-
if ($AsBase64String) {
215+
if ($AsBase64EncodedString) {
164216
$Screenshot.AsBase64EncodedString
165217
}
218+
else {
219+
$Screenshot
220+
}
166221
}
167222

168223
function Save-SeScreenshot {
@@ -174,7 +229,42 @@ function Save-SeScreenshot {
174229
[Parameter()]
175230
[OpenQA.Selenium.ScreenshotImageFormat]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat]::Png)
176231

177-
Process {
178-
$Screenshot.SaveAsFile($Path, $ImageFormat)
179-
}
232+
Process {
233+
$Screenshot.SaveAsFile($Path, $ImageFormat)
234+
}
235+
}
236+
237+
function Wait-SeElementExists{
238+
param(
239+
$Driver,
240+
$Timeout = 30,
241+
$Id,
242+
$Name,
243+
$TagName,
244+
$ClassName
245+
)
246+
if ($Id) {
247+
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
248+
}
249+
elseif ($Name) {
250+
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
251+
}
252+
elseif($TagName)
253+
{
254+
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
255+
}
256+
elseif($ClassName)
257+
{
258+
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
259+
}
260+
else
261+
{
262+
throw "Please specify -Id or -Name or -TagName or -ClassName"
263+
}
264+
265+
$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
266+
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
267+
$WebDriverWait.Until($Condition)
180268
}
269+
270+

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
}

assemblies/IEDriverServer.exe

3.15 MB
Binary file not shown.

assemblies/InternetExplorerDriver.exe

3.15 MB
Binary file not shown.

assemblies/WebDriver.Support.dll

512 Bytes
Binary file not shown.

assemblies/WebDriver.dll

12.5 KB
Binary file not shown.

assemblies/chromedriver.exe

-242 KB
Binary file not shown.

assemblies/geckodriver.exe

10.4 MB
Binary file not shown.

0 commit comments

Comments
 (0)