diff --git a/README.md b/README.md index 00357f5..a33b89a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` \ No newline at end of file diff --git a/Selenium.psd1 b/Selenium.psd1 index 7e9d7a6..0b5f6c2 100644 --- a/Selenium.psd1 +++ b/Selenium.psd1 @@ -12,7 +12,7 @@ RootModule = '.\Selenium.psm1' # Version number of this module. -ModuleVersion = '1.3.0' +ModuleVersion = '1.4.0' # Supported PSEditions # CompatiblePSEditions = @() @@ -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. diff --git a/Selenium.psm1 b/Selenium.psm1 index 34979ca..d71f497 100644 --- a/Selenium.psm1 +++ b/Selenium.psm1 @@ -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" @@ -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) } @@ -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 { @@ -102,6 +114,8 @@ function Find-SeElement { $Driver, [Parameter()] $Element, + [Parameter()][Switch]$Wait, + [Parameter()]$Timeout = 30, [Parameter(ParameterSetName = "ByCss")] $Css, [Parameter(ParameterSetName = "ByName")] @@ -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)) + } } } } @@ -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) + Initializes a new instance of the Cookie class with a specific name, value, path and expiration date. + Cookie(String, String, String, String, Nullable) + 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) + Initializes a new instance of the Cookie class with a specific name, value, path and expiration date. + Cookie(String, String, String, String, Nullable) + 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) } @@ -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 + } +} diff --git a/Selenium.tests.ps1 b/Selenium.tests.ps1 index 17f7deb..9352e9e 100644 --- a/Selenium.tests.ps1 +++ b/Selenium.tests.ps1 @@ -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 @@ -114,7 +138,7 @@ 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']" @@ -122,4 +146,23 @@ Describe "Send-SeKeys" { } } Stop-SeDriver $Driver -} \ No newline at end of file +} + +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 +} +