forked from adamdriscoll/selenium-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelenium.psm1
More file actions
226 lines (186 loc) · 6.03 KB
/
Copy pathSelenium.psm1
File metadata and controls
226 lines (186 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\assemblies\WebDriver.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\assemblies\WebDriver.Support.dll")
function Start-SeChrome {
Param(
[Parameter(Mandatory = $false)]
[array]$Arguments,
[switch]$HideVersionHint
)
if($Arguments) {
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
$Chrome_Options.AddArguments($Arguments)
}
if(!$HideVersionHint)
{
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
}
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
}
function Start-SeFirefox {
param([Switch]$Profile)
if ($Profile) {
#Doesn't work....
$ProfilePath = Join-Path $PSScriptRoot "Assets\ff-profile\rust_mozprofile.YwpEBLY3hCRX"
$firefoxProfile = New-Object OpenQA.Selenium.Firefox.FirefoxProfile -ArgumentList ($ProfilePath)
$firefoxProfile.WriteToDisk()
New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver" -ArgumentList $firefoxProfile
}
else {
$Driver = New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver"
$Driver.Manage().Timeouts().ImplicitWait = [TimeSpan]::FromSeconds(10)
$Driver
}
}
function Stop-SeDriver {
param($Driver)
$Driver.Dispose()
}
function Enter-SeUrl {
param($Driver, $Url)
$Driver.Navigate().GoToUrl($Url)
}
function Find-SeElement {
param(
[Parameter()]
$Driver,
[Parameter()]
$Element,
[Parameter(ParameterSetName = "ByName")]
$Name,
[Parameter(ParameterSetName = "ById")]
$Id,
[Parameter(ParameterSetName = "ByClassName")]
$ClassName,
[Parameter(ParameterSetName = "ByLinkText")]
$LinkText,
[Parameter(ParameterSetName = "ByTagName")]
$TagName,
[Parameter(ParameterSetName = "ByXPath")]
$XPath)
Process {
if ($Driver -ne $null -and $Element -ne $null) {
throw "Driver and Element may not be specified together."
}
elseif ($Driver -ne $Null) {
$Target = $Driver
}
elseif ($Element -ne $Null) {
$Target = $Element
}
else {
"Driver or element must be specified"
}
if ($PSCmdlet.ParameterSetName -eq "ByName") {
$Target.FindElements([OpenQA.Selenium.By]::Name($Name))
}
if ($PSCmdlet.ParameterSetName -eq "ById") {
$Target.FindElements([OpenQA.Selenium.By]::Id($Id))
}
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
}
if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
}
if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
$Target.FindElements([OpenQA.Selenium.By]::TagName($TagName))
}
if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
}
}
}
function Invoke-SeClick {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[OpenQA.Selenium.IWebElement]$Element,
[Parameter()]
[Switch]$JavaScriptClick,
[Parameter()]
$Driver
)
if ($JavaScriptClick) {
$Driver.ExecuteScript("arguments[0].click()", $Element)
} else {
$Element.Click()
}
}
function Get-SeKeys {
[OpenQA.Selenium.Keys] | Get-Member -MemberType Property -Static | Select-Object -Property Name,@{N = "ObjectString"; E = {"[OpenQA.Selenium.Keys]::$($_.Name)"}}
}
function Send-SeKeys {
param([OpenQA.Selenium.IWebElement]$Element, [string]$Keys)
foreach($Key in @(Get-SeKeys).Name)
{
$Keys = $Keys -replace "{{$Key}}", [OpenQA.Selenium.Keys]::$Key
}
$Element.SendKeys($Keys)
}
function Get-SeCookie {
param($Driver)
$Driver.Manage().Cookies.AllCookies.GetEnumerator()
}
function Remove-SeCookie {
param($Driver)
$Driver.Manage().Cookies.DeleteAllCookies()
}
function Set-SeCookie {
param($Driver, $name, $value)
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$value
$Driver.Manage().Cookies.AddCookie($cookie)
}
function Get-SeElementAttribute {
param(
[Parameter(ValueFromPipeline=$true, Mandatory = $true)]
[OpenQA.Selenium.IWebElement]$Element,
[Parameter(Mandatory=$true)]
[string]$Attribute
)
Process {
$Element.GetAttribute($Attribute)
}
}
function Invoke-SeScreenshot {
param($Driver, [Switch]$AsBase64EncodedString)
$Screenshot = [OpenQA.Selenium.Support.Extensions.WebDriverExtensions]::TakeScreenshot($Driver)
if ($AsBase64EncodedString) {
$Screenshot.AsBase64EncodedString
} else {
$Screenshot
}
}
function Save-SeScreenshot {
param(
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[OpenQA.Selenium.Screenshot]$Screenshot,
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter()]
[OpenQA.Selenium.ScreenshotImageFormat]$ImageFormat = [OpenQA.Selenium.ScreenshotImageFormat]::Png)
Process {
$Screenshot.SaveAsFile($Path, $ImageFormat)
}
}
function Wait-SeElementExists {
param(
$Driver,
$Timeout = 30,
$Id,
$Name
)
if($Id)
{
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
}
elseif($Name)
{
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
}
else
{
throw "Please specify -Id or -Name"
}
$WebDriverWait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait $Driver, ($Timeout * 10000000) # ticks
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
$WebDriverWait.Until($Condition)
}