Skip to content

Commit 9c63144

Browse files
committed
More cmdlets
1 parent caaf2a2 commit 9c63144

File tree

2 files changed

+66
-155
lines changed

2 files changed

+66
-155
lines changed

Selenium.psm1

Lines changed: 56 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,220 +1,121 @@
11
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\WebDriver.dll")
22
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\WebDriver.Support.dll")
33

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-
#>
144
function Start-SeChrome {
155
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver"
166
}
177

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-
#>
288
function Start-SeFirefox {
299
New-Object -TypeName "OpenQA.Selenium.Firefox.FirefoxDriver"
3010
}
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.
6311

64-
.DESCRIPTION
65-
Stops a Selenium driver.
66-
67-
.PARAMETER Driver
68-
The driver to stop.
69-
#>
7012
function Stop-SeDriver {
71-
param([OpenQA.Selenium.IWebDriver]$Driver)
13+
param($Driver)
7214

7315
$Driver.Dispose()
7416
}
7517

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-
9318
function Enter-SeUrl {
94-
param([OpenQA.Selenium.IWebDriver]$Driver, $Url)
19+
param($Driver, $Url)
9520

9621
$Driver.Navigate().GoToUrl($Url)
9722
}
9823

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-
#>
12424
function Find-SeElement {
12525
param(
12626
[Parameter()]
127-
[OpenQA.Selenium.IWebDriver]$Driver,
27+
$Driver,
28+
[Parameter()]
29+
$Element,
12830
[Parameter(ParameterSetName = "ByName")]
12931
$Name,
13032
[Parameter(ParameterSetName = "ById")]
13133
$Id,
13234
[Parameter(ParameterSetName = "ByClassName")]
13335
$ClassName,
13436
[Parameter(ParameterSetName = "ByLinkText")]
135-
$LinkText)
37+
$LinkText,
38+
[Parameter(ParameterSetName = "ByTagName")]
39+
$TagName)
13640

13741
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+
13856
if ($PSCmdlet.ParameterSetName -eq "ByName") {
139-
$Driver.FindElement([OpenQA.Selenium.By]::Name($Name))
57+
$Target.FindElements([OpenQA.Selenium.By]::Name($Name))
14058
}
14159

14260
if ($PSCmdlet.ParameterSetName -eq "ById") {
143-
$Driver.FindElement([OpenQA.Selenium.By]::Id($Id))
61+
$Target.FindElements([OpenQA.Selenium.By]::Id($Id))
14462
}
14563

14664
if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
147-
$Driver.FindElement([OpenQA.Selenium.By]::LinkText($LinkText))
65+
$Target.FindElements([OpenQA.Selenium.By]::LinkText($LinkText))
14866
}
14967

15068
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))
15274
}
15375
}
15476
}
15577

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-
#>
16978
function Invoke-SeClick {
17079
param([OpenQA.Selenium.IWebElement]$Element)
17180

17281
$Element.Click()
17382
}
17483

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)
17986

180-
$Driver.Navigate.Back()
87+
$Element.SendKeys($Keys)
18188
}
18289

183-
function Invoke-SeNavigateForward {
184-
param(
185-
[Parameter()]
186-
[OpenQA.Selenium.IWebDriver]$Driver)
90+
function Get-SeCookie {
91+
param($Driver)
18792

188-
$Driver.Navigate.Forward()
93+
$Driver.Manage().Cookies.AllCookies.GetEnumerator()
18994
}
19095

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()
197100
}
198101

102+
function Set-SeCookie {
103+
param($Driver, $name, $value)
199104

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+
}
212109

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+
)
218117

219-
$Element.SendKeys($Keys)
118+
Process {
119+
$Element.GetAttribute($Attribute)
120+
}
220121
}

Selenium.tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Import-Module (Join-Path $PSScriptRoot "Selenium.psm1")
2+
3+
Describe "Get-SeCookie" {
4+
Context "Should get cookies from google" {
5+
$Driver = Start-SeFirefox
6+
Enter-SeUrl -Driver $Driver -Url "http://www.google.com"
7+
8+
Get-SeCookie $Driver
9+
}
10+
}

0 commit comments

Comments
 (0)