Skip to content

BREAKING CHANGE - Find-SeElement -Wait / Improved Set-SeCookie #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Send-SeKeys -Element $Element -Keys "adam@poshtools.com"
$Driver = Start-SeChrome -Headless

# Run Chrome in incognito mode
$Driver = Start-SeChrome -Arguments "incognito"
$Driver = Start-SeChrome -Incognito

# Run Chrome with alternative download folder
$Driver = Start-SeChrome -DefaultDownloadPath c:\temp
Expand All @@ -81,6 +81,6 @@ $Driver = Start-SeChrome -DefaultDownloadPath c:\temp
```powershell
$Driver = Start-SeChrome
Enter-SeUrl https://www.google.com -Driver $Driver
Wait-SeElementExists -Driver $Driver -Timeout 3 -Id "q"
Wait-SeElementExists -Driver $Driver -Timeout 3 -Name "q"
Find-SeElement -Driver $d -Wait -Timeout 10 -Css input[name='q']
Find-SeElement -Driver $d -Wait -Timeout 10 -Name q
```
5 changes: 3 additions & 2 deletions Selenium.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\Selenium.psm1'

# Version number of this module.
ModuleVersion = '1.3.0'
ModuleVersion = '1.4.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -86,7 +86,8 @@ FunctionsToExport = @(
"Start-SeInternetExplorer",
"Start-SeEdge",
"Stop-SeDriver",
"Wait-SeElementExists"
"Get-SeWindow",
"Switch-SeWindow"
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
208 changes: 146 additions & 62 deletions Selenium.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ function Start-SeChrome {
[switch]$HideVersionHint,
[System.IO.FileInfo]$DefaultDownloadPath,
[bool]$DisableBuiltInPDFViewer=$true,
[switch]$Headless
[switch]$Headless,
[switch]$Incognito,
[switch]$Maximized
)

$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
Expand All @@ -33,6 +35,14 @@ function Start-SeChrome {
$Chrome_Options.AddArguments('headless')
}

if ($Incognito) {
$Chrome_Options.AddArguments('Incognito')
}

if ($Maximized) {
$Chrome_Options.AddArguments('start-maximized')
}

if ($Arguments) {
$Chrome_Options.AddArguments($Arguments)
}
Expand All @@ -50,7 +60,9 @@ function Start-SeChrome {
}

function Start-SeInternetExplorer {
New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerDriver"
$InternetExplorer_Options = New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerOptions"
$InternetExplorer_Options.IgnoreZoomLevel = $true
New-Object -TypeName "OpenQA.Selenium.IE.InternetExplorerDriver" -ArgumentList $InternetExplorer_Options
}

function Start-SeEdge {
Expand Down Expand Up @@ -102,6 +114,8 @@ function Find-SeElement {
$Driver,
[Parameter()]
$Element,
[Parameter()][Switch]$Wait,
[Parameter()]$Timeout = 30,
[Parameter(ParameterSetName = "ByCss")]
$Css,
[Parameter(ParameterSetName = "ByName")]
Expand Down Expand Up @@ -136,36 +150,75 @@ function Find-SeElement {
"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 "ByPartialLinkText") {
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
}

if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
$Target.FindElements([OpenQA.Selenium.By]::ClassName($ClassName))
if($Wait){
if ($PSCmdlet.ParameterSetName -eq "ByName") {
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
}

if ($PSCmdlet.ParameterSetName -eq "ById") {
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
}

if ($PSCmdlet.ParameterSetName -eq "ByLinkText") {
$TargetElement = [OpenQA.Selenium.By]::LinkText($LinkText)
}

if ($PSCmdlet.ParameterSetName -eq "ByPartialLinkText") {
$TargetElement = [OpenQA.Selenium.By]::PartialLinkText($PartialLinkText)
}

if ($PSCmdlet.ParameterSetName -eq "ByClassName") {
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
}

if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
}

if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
$TargetElement = [OpenQA.Selenium.By]::XPath($XPath)
}

if ($PSCmdlet.ParameterSetName -eq "ByCss") {
$TargetElement = [OpenQA.Selenium.By]::CssSelector($Css)
}

$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
$WebDriverWait.Until($Condition)
}

if ($PSCmdlet.ParameterSetName -eq "ByTagName") {
$Target.FindElements([OpenQA.Selenium.By]::TagName($TagName))
}

if ($PSCmdlet.ParameterSetName -eq "ByXPath") {
$Target.FindElements([OpenQA.Selenium.By]::XPath($XPath))
}

if ($PSCmdlet.ParameterSetName -eq "ByCss") {
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
else{
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 "ByPartialLinkText") {
$Target.FindElements([OpenQA.Selenium.By]::PartialLinkText($PartialLinkText))
}

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

if ($PSCmdlet.ParameterSetName -eq "ByCss") {
$Target.FindElements([OpenQA.Selenium.By]::CssSelector($Css))
}
}
}
}
Expand Down Expand Up @@ -217,10 +270,55 @@ function Remove-SeCookie {
}

function Set-SeCookie {
param($Driver, $name, $value)
param(
$Driver,
[string]$Name,
[string]$Value,
[string]$Path,
[string]$Domain,
[datetime]$ExpiryDate
)

<# Selenium Cookie Information
Cookie(String, String)
Initializes a new instance of the Cookie class with a specific name and value.
Cookie(String, String, String)
Initializes a new instance of the Cookie class with a specific name, value, and path.
Cookie(String, String, String, Nullable<DateTime>)
Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
Cookie(String, String, String, String, Nullable<DateTime>)
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date.
#>

if($Name -and $Value -and (!$Path -and !$Domain -and !$ExpiryDate)){
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value
}
Elseif($Name -and $Value -and $Path -and (!$Domain -and !$ExpiryDate)){
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value,$Path
}
Elseif($Name -and $Value -and $Path -and $ExpiryDate -and !$Domain){
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value,$Path,$ExpiryDate
}
Elseif($Name -and $Value -and $Path -and $ExpiryDate -and $Domain){
if($Driver.Url -match $Domain){
$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name,$Value,$Domain,$Path,$ExpiryDate
}
else{
Throw 'In order to set the cookie the browser needs to be on the cookie domain URL'
}
}
else{
Throw "Incorrect Cookie Layout:
Cookie(String, String)
Initializes a new instance of the Cookie class with a specific name and value.
Cookie(String, String, String)
Initializes a new instance of the Cookie class with a specific name, value, and path.
Cookie(String, String, String, Nullable<DateTime>)
Initializes a new instance of the Cookie class with a specific name, value, path and expiration date.
Cookie(String, String, String, String, Nullable<DateTime>)
Initializes a new instance of the Cookie class with a specific name, value, domain, path and expiration date."
}

$cookie = New-Object -TypeName OpenQA.Selenium.Cookie -ArgumentList $Name, $value

$Driver.Manage().Cookies.AddCookie($cookie)
}

Expand Down Expand Up @@ -263,37 +361,23 @@ function Save-SeScreenshot {
}
}

function Wait-SeElementExists{
function Get-SeWindow {
param(
$Driver,
$Timeout = 30,
$Id,
$Name,
$TagName,
$ClassName
[Parameter(Mandatory = $true)][OpenQA.Selenium.IWebDriver]$Driver
)
if ($Id) {
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
}
elseif ($Name) {
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
}
elseif($TagName)
{
$TargetElement = [OpenQA.Selenium.By]::TagName($TagName)
}
elseif($ClassName)
{
$TargetElement = [OpenQA.Selenium.By]::ClassName($ClassName)
}
else
{
throw "Please specify -Id or -Name or -TagName or -ClassName"
}

$WebDriverWait = New-Object -TypeName OpenQA.Selenium.Support.UI.WebDriverWait($Driver, (New-TimeSpan -Seconds $Timeout))
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
$WebDriverWait.Until($Condition)
Process {
$Driver.WindowHandles
}
}

function Switch-SeWindow {
param(
[Parameter(Mandatory = $true)][OpenQA.Selenium.IWebDriver]$Driver,
[Parameter(Mandatory = $true)]$Window
)

Process {
$Driver.SwitchTo().Window($Window)|Out-Null
}
}
47 changes: 45 additions & 2 deletions Selenium.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ Describe "Start-SeChrome headless" {
}
}

Describe "Start-SeChrome with Options" {
Context "Should Start Chrome Driver with different startup options" {
It "Start Chrome in Headless mode"{
$Driver = Start-SeChrome -Headless
Stop-SeDriver $Driver
}

It "Start Chrome Maximized "{
$Driver = Start-SeChrome -Maximized
Stop-SeDriver $Driver
}

It "Start Chrome Incognito "{
$Driver = Start-SeChrome -Incognito
Stop-SeDriver $Driver
}

It "Start Chrome Maximized and Incognito "{
$Driver = Start-SeChrome -Maximized -Incognito
Stop-SeDriver $Driver
}
}
}

Describe "Start-SeFirefox" {
Context "Should Start Firefox Driver" {
$Driver = Start-SeFirefox
Expand Down Expand Up @@ -114,12 +138,31 @@ Describe "Get-SeCookie" {

Describe "Send-SeKeys" {
$Driver = Start-SeFirefox
Enter-SeUrl -Driver $Driver -Url "http://www.google.com"
Enter-SeUrl -Driver $Driver -Url "http://www.google.com/ncr"
Context "Find-SeElement" {
It "By Css" {
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
Send-SeKeys -Element $SearchInput -Keys "test"
}
}
Stop-SeDriver $Driver
}
}

Describe "Find-SeElement Firefox" {
$Driver = Start-SeFirefox
Enter-SeUrl -Driver $Driver -Url "http://www.google.com/ncr"
Context "Find-SeElement" {
It "By Css" {
$SearchInput = Find-SeElement -Driver $Driver -Css "input[name='q']"
}
}

Context "Find-SeElement -Wait" {
It "By Name"{
$SearchInput = Find-SeElement -Driver $Driver -Wait -Name q -Timeout 60
}
}

Stop-SeDriver $Driver
}