1
1
[System.Reflection.Assembly ]::LoadFrom(" $PSScriptRoot \WebDriver.dll" )
2
2
[System.Reflection.Assembly ]::LoadFrom(" $PSScriptRoot \WebDriver.Support.dll" )
3
3
4
- <#
5
- . SYNOPSIS
6
- Starts a Selenium Chrome driver
7
-
8
- . DESCRIPTION
9
- Starts a Selenium Chrome driver
10
-
11
- . EXAMPLE
12
- Start-SeChrome
13
- #>
14
4
function Start-SeChrome {
15
5
New-Object - TypeName " OpenQA.Selenium.Chrome.ChromeDriver"
16
6
}
17
7
18
- <#
19
- . SYNOPSIS
20
- Starts a Selenium Firefox driver
21
-
22
- . DESCRIPTION
23
- Starts a Selenium Firefox driver
24
-
25
- . EXAMPLE
26
- Start-SeFirefox
27
- #>
28
8
function Start-SeFirefox {
29
9
New-Object - TypeName " OpenQA.Selenium.Firefox.FirefoxDriver"
30
10
}
31
- <#
32
- . SYNOPSIS
33
- Starts a Selenium Edge driver
34
-
35
- . DESCRIPTION
36
- Starts a Selenium Edge driver
37
-
38
- . EXAMPLE
39
- Start-SeEdge
40
- #>
41
- function Start-SeEdge {
42
- New-Object - TypeName " OpenQA.Selenium.Edge.EdgeDriver"
43
- }
44
-
45
- <#
46
- . SYNOPSIS
47
- Starts a Selenium Internet Explorer driver
48
-
49
- . DESCRIPTION
50
- Starts a Selenium Internet Explorer driver
51
-
52
- . EXAMPLE
53
- Start-SeInternetExplorer
54
- #>
55
- function Start-SeInternetExplorer {
56
- New-Object - TypeName " OpenQA.Selenium.IE.InternetExplorerDriver"
57
- }
58
-
59
-
60
- <#
61
- . SYNOPSIS
62
- Stops a Selenium driver.
63
11
64
- . DESCRIPTION
65
- Stops a Selenium driver.
66
-
67
- . PARAMETER Driver
68
- The driver to stop.
69
- #>
70
12
function Stop-SeDriver {
71
- param ([ OpenQA.Selenium.IWebDriver ] $Driver )
13
+ param ($Driver )
72
14
73
15
$Driver.Dispose ()
74
16
}
75
17
76
- <#
77
- . SYNOPSIS
78
- Navigates to a url.
79
-
80
- . DESCRIPTION
81
- Navigates to a url.
82
-
83
- . PARAMETER Driver
84
- The driver to navigate with. See Start-SeChrome and Start-SeFirefox.
85
-
86
- . PARAMETER Url
87
- The URL to navigate to.
88
-
89
- . EXAMPLE
90
- Enter-SeUrl -Url https://www.google.com -Driver (Start-SeChrome)
91
- #>
92
-
93
18
function Enter-SeUrl {
94
- param ([ OpenQA.Selenium.IWebDriver ] $Driver , $Url )
19
+ param ($Driver , $Url )
95
20
96
21
$Driver.Navigate ().GoToUrl($Url )
97
22
}
98
23
99
- <#
100
- . SYNOPSIS
101
- Find an element in the currently loaded page.
102
-
103
- . DESCRIPTION
104
- Find an element in the currently loaded page.
105
-
106
- . PARAMETER Driver
107
- The driver to navigate with. See Start-SeChrome and Start-SeFirefox.
108
-
109
- . PARAMETER Name
110
- The name of the element to find.
111
-
112
- . PARAMETER Id
113
- The Id of the element to find.
114
-
115
- . PARAMETER ClassName
116
- The ClassName of the element to find.
117
-
118
- . PARAMETER LinkText
119
- The LinkText of the element to find.
120
-
121
- . EXAMPLE
122
- $Element = Find-SeElement -Driver $Driver -Id "MyTextbox"
123
- #>
124
24
function Find-SeElement {
125
25
param (
126
26
[Parameter ()]
127
- [OpenQA.Selenium.IWebDriver ]$Driver ,
27
+ $Driver ,
28
+ [Parameter ()]
29
+ $Element ,
128
30
[Parameter (ParameterSetName = " ByName" )]
129
31
$Name ,
130
32
[Parameter (ParameterSetName = " ById" )]
131
33
$Id ,
132
34
[Parameter (ParameterSetName = " ByClassName" )]
133
35
$ClassName ,
134
36
[Parameter (ParameterSetName = " ByLinkText" )]
135
- $LinkText )
37
+ $LinkText ,
38
+ [Parameter (ParameterSetName = " ByTagName" )]
39
+ $TagName )
136
40
137
41
Process {
42
+
43
+ if ($Driver -ne $null -and $Element -ne $null ) {
44
+ throw " Driver and Element may not be specified together."
45
+ }
46
+ elseif ($Driver -ne $Null ) {
47
+ $Target = $Driver
48
+ }
49
+ elseif ($Element -ne $Null ) {
50
+ $Target = $Element
51
+ }
52
+ else {
53
+ " Driver or element must be specified"
54
+ }
55
+
138
56
if ($PSCmdlet.ParameterSetName -eq " ByName" ) {
139
- $Driver .FindElement ([OpenQA.Selenium.By ]::Name($Name ))
57
+ $Target .FindElements ([OpenQA.Selenium.By ]::Name($Name ))
140
58
}
141
59
142
60
if ($PSCmdlet.ParameterSetName -eq " ById" ) {
143
- $Driver .FindElement ([OpenQA.Selenium.By ]::Id($Id ))
61
+ $Target .FindElements ([OpenQA.Selenium.By ]::Id($Id ))
144
62
}
145
63
146
64
if ($PSCmdlet.ParameterSetName -eq " ByLinkText" ) {
147
- $Driver .FindElement ([OpenQA.Selenium.By ]::LinkText($LinkText ))
65
+ $Target .FindElements ([OpenQA.Selenium.By ]::LinkText($LinkText ))
148
66
}
149
67
150
68
if ($PSCmdlet.ParameterSetName -eq " ByClassName" ) {
151
- $Driver.FindElement ([OpenQA.Selenium.By ]::ClassName($ClassName ))
69
+ $Target.FindElements ([OpenQA.Selenium.By ]::ClassName($ClassName ))
70
+ }
71
+
72
+ if ($PSCmdlet.ParameterSetName -eq " ByTagName" ) {
73
+ $Target.FindElements ([OpenQA.Selenium.By ]::TagName($TagName ))
152
74
}
153
75
}
154
76
}
155
77
156
- <#
157
- . SYNOPSIS
158
- Clicks an element
159
-
160
- . DESCRIPTION
161
- Clicks an element
162
-
163
- . PARAMETER Element
164
- The element to click.
165
-
166
- . EXAMPLE
167
- Invoke-SeClick -Element $Element
168
- #>
169
78
function Invoke-SeClick {
170
79
param ([OpenQA.Selenium.IWebElement ]$Element )
171
80
172
81
$Element.Click ()
173
82
}
174
83
175
- function Invoke-SeNavigateBack {
176
- param (
177
- [Parameter ()]
178
- [OpenQA.Selenium.IWebDriver ]$Driver )
84
+ function Send-SeKeys {
85
+ param ([OpenQA.Selenium.IWebElement ]$Element , [string ]$Keys )
179
86
180
- $Driver .Navigate.Back ( )
87
+ $Element .SendKeys ( $Keys )
181
88
}
182
89
183
- function Invoke-SeNavigateForward {
184
- param (
185
- [Parameter ()]
186
- [OpenQA.Selenium.IWebDriver ]$Driver )
90
+ function Get-SeCookie {
91
+ param ($Driver )
187
92
188
- $Driver.Navigate.Forward ()
93
+ $Driver.Manage ().Cookies.AllCookies.GetEnumerator ()
189
94
}
190
95
191
- function Invoke-SeRefresh {
192
- param (
193
- [Parameter ()]
194
- [OpenQA.Selenium.IWebDriver ]$Driver )
195
-
196
- $Driver.Navigate.Refresh ()
96
+ function Remove-SeCookie {
97
+ param ($Driver )
98
+
99
+ $Driver.Manage ().Cookies.DeleteAllCookies()
197
100
}
198
101
102
+ function Set-SeCookie {
103
+ param ($Driver , $name , $value )
199
104
200
- <#
201
- . SYNOPSIS
202
- Sends keys to an element
203
-
204
- . DESCRIPTION
205
- Sends keys to an element
206
-
207
- . PARAMETER Element
208
- The element to send keys to.
209
-
210
- . PARAMETER Keys
211
- The keys to send.
105
+ $cookie = New-Object - TypeName OpenQA.Selenium.Cookie - ArgumentList $Name , $value
106
+
107
+ $Driver.Manage ().Cookies.AddCookie($cookie )
108
+ }
212
109
213
- . EXAMPLE
214
- Send-SeKeys -Element $Element -Keys "Hey, there!"
215
- #>
216
- function Send-SeKeys {
217
- param ([OpenQA.Selenium.IWebElement ]$Element , [string ]$Keys )
110
+ function Get-SeElementAttribute {
111
+ param (
112
+ [Parameter (ValueFromPipeline = $true , Mandatory = $true )]
113
+ [OpenQA.Selenium.IWebElement ]$Element ,
114
+ [Parameter (Mandatory = $true )]
115
+ [string ]$Attribute
116
+ )
218
117
219
- $Element.SendKeys ($Keys )
118
+ Process {
119
+ $Element.GetAttribute ($Attribute )
120
+ }
220
121
}
0 commit comments