2
2
[System.Reflection.Assembly ]::LoadFrom(" $PSScriptRoot \assemblies\WebDriver.Support.dll" )
3
3
4
4
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
6
32
}
7
33
8
34
function Start-SeIe {
@@ -45,6 +71,8 @@ function Find-SeElement {
45
71
$Driver ,
46
72
[Parameter ()]
47
73
$Element ,
74
+ [Parameter (ParameterSetName = " ByCss" )]
75
+ $Css ,
48
76
[Parameter (ParameterSetName = " ByName" )]
49
77
$Name ,
50
78
[Parameter (ParameterSetName = " ById" )]
@@ -53,10 +81,16 @@ function Find-SeElement {
53
81
$ClassName ,
54
82
[Parameter (ParameterSetName = " ByLinkText" )]
55
83
$LinkText ,
84
+ [Parameter (ParameterSetName = " ByPartialLinkText" )]
85
+ $PartialLinkText ,
56
86
[Parameter (ParameterSetName = " ByTagName" )]
57
87
$TagName ,
58
88
[Parameter (ParameterSetName = " ByXPath" )]
59
- $XPath )
89
+ $XPath ,
90
+ [Parameter (ParameterSetName = " ByCss" )]
91
+ $Css
92
+ )
93
+
60
94
61
95
Process {
62
96
@@ -85,6 +119,10 @@ function Find-SeElement {
85
119
$Target.FindElements ([OpenQA.Selenium.By ]::LinkText($LinkText ))
86
120
}
87
121
122
+ if ($PSCmdlet.ParameterSetName -eq " ByPartialLinkText" ) {
123
+ $Target.FindElements ([OpenQA.Selenium.By ]::PartialLinkText($PartialLinkText ))
124
+ }
125
+
88
126
if ($PSCmdlet.ParameterSetName -eq " ByClassName" ) {
89
127
$Target.FindElements ([OpenQA.Selenium.By ]::ClassName($ClassName ))
90
128
}
@@ -96,6 +134,10 @@ function Find-SeElement {
96
134
if ($PSCmdlet.ParameterSetName -eq " ByXPath" ) {
97
135
$Target.FindElements ([OpenQA.Selenium.By ]::XPath($XPath ))
98
136
}
137
+
138
+ if ($PSCmdlet.ParameterSetName -eq " ByCss" ) {
139
+ $Target.FindElements ([OpenQA.Selenium.By ]::CssSelector($Css ))
140
+ }
99
141
}
100
142
}
101
143
@@ -107,19 +149,29 @@ function Invoke-SeClick {
107
149
[Switch ]$JavaScriptClick ,
108
150
[Parameter ()]
109
151
$Driver
110
- )
152
+ )
111
153
112
154
if ($JavaScriptClick ) {
113
155
$Driver.ExecuteScript (" arguments[0].click()" , $Element )
114
- } else {
156
+ }
157
+ else {
115
158
$Element.Click ()
116
159
}
117
160
118
161
}
119
162
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
+
120
168
function Send-SeKeys {
121
169
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
+
123
175
$Element.SendKeys ($Keys )
124
176
}
125
177
@@ -138,16 +190,16 @@ function Remove-SeCookie {
138
190
function Set-SeCookie {
139
191
param ($Driver , $name , $value )
140
192
141
- $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $value
193
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $value
142
194
143
195
$Driver.Manage ().Cookies.AddCookie($cookie )
144
196
}
145
197
146
198
function Get-SeElementAttribute {
147
199
param (
148
- [Parameter (ValueFromPipeline = $true , Mandatory = $true )]
200
+ [Parameter (ValueFromPipeline = $true , Mandatory = $true )]
149
201
[OpenQA.Selenium.IWebElement ]$Element ,
150
- [Parameter (Mandatory = $true )]
202
+ [Parameter (Mandatory = $true )]
151
203
[string ]$Attribute
152
204
)
153
205
@@ -160,9 +212,12 @@ function Invoke-SeScreenshot {
160
212
param ($Driver , [Switch ]$AsBase64EncodedString )
161
213
162
214
$Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions ]::TakeScreenshot($Driver )
163
- if ($AsBase64String ) {
215
+ if ($AsBase64EncodedString ) {
164
216
$Screenshot.AsBase64EncodedString
165
217
}
218
+ else {
219
+ $Screenshot
220
+ }
166
221
}
167
222
168
223
function Save-SeScreenshot {
@@ -174,7 +229,42 @@ function Save-SeScreenshot {
174
229
[Parameter ()]
175
230
[OpenQA.Selenium.ScreenshotImageFormat ]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat ]::Png)
176
231
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 )
180
268
}
269
+
270
+
0 commit comments