22[System.Reflection.Assembly ]::LoadFrom(" $PSScriptRoot \assemblies\WebDriver.Support.dll" )
33
44function 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
834function 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+
120168function 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 {
138190function 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
146198function 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
168223function 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+
0 commit comments