Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Import-Module "{FullPath}\selenium-powershell\Selenium.psm1"
```

# Usage
`Note: in order to use a specific driver you will need to have the brower of the driver installed on your system.
For example if you use Start-SeChrome you will need to have either a Chrome or Chromium browser installed
`

## Start a Browser Driver
```powershell
Expand All @@ -38,15 +41,15 @@ $Driver = Start-SeFirefox
Enter-SeUrl https://www.poshud.com -Driver $Driver
```

## Find an element
## Find an Element

```powershell
$Driver = Start-SeFirefox
Enter-SeUrl https://www.poshud.com -Driver $Driver
$Element = Find-SeElement -Driver $Driver -Id "myControl"
```

## Click on a button
## Click on an Element/Button

```powershell
$Driver = Start-SeFirefox
Expand Down Expand Up @@ -74,13 +77,29 @@ $Driver = Start-SeChrome -Headless
$Driver = Start-SeChrome -Incognito

# Run Chrome with alternative download folder
$Driver = Start-SeChrome -DefaultDownloadPath c:\temp
$Driver = Start-SeChrome -DefaultDownloadPath C:\Temp

# Run Chrome and go to a URL in one command
$Driver = Start-SeChrome -StartURL 'https://www.google.com/ncr'

# Run Chrome with multiple Arguments
$Driver = Start-SeChrome -Arguments @('Incognito','start-maximized')

# Run Chrome with an existing profile.
# The default profile paths are as follows:
# Windows: C:\Users\<username>\AppData\Local\Google\Chrome\User Data
# Linux: /home/<username>/.config/google-chrome
# MacOS: /Users/<username>/Library/Application Support/Google/Chrome
$Driver = Start-SeChrome -ProfileDirectoryPath '/home/<username>/.config/google-chrome'

```

## Wait for an element
## Find and Wait for an element
```powershell
$Driver = Start-SeChrome
Enter-SeUrl https://www.google.com -Driver $Driver
Enter-SeUrl 'https://www.google.com/ncr' -Driver $Driver

# Please note that with the -Wait parameter only one element can be returned at a time.
Find-SeElement -Driver $d -Wait -Timeout 10 -Css input[name='q']
Find-SeElement -Driver $d -Wait -Timeout 10 -Name q
```
206 changes: 143 additions & 63 deletions Selenium.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,118 @@ elseif($IsMacOS){
$AssembliesPath = "$PSScriptRoot/assemblies/macos"
}

# Grant Execution permission to assemblies on Linux and MacOS
if($IsLinux -or $IsMacOS){
# Check if powershell is NOT running as root
$AssemblieFiles = Get-ChildItem -Path $AssembliesPath |Where-Object{$_.Name -eq 'chromedriver' -or $_.Name -eq 'geckodriver'}
foreach($AssemblieFile in $AssemblieFiles){
if($IsLinux){
$FileMod = stat -c "%a" $AssemblieFile.fullname
}
elseif($IsMacOS){
$FileMod = /usr/bin/stat -f "%A" $AssemblieFile.fullname
}

if($FileMod[2] -ne '5' -and $FileMod[2] -ne '7' ){
Write-Host "Granting $($AssemblieFile.fullname) Execution Permissions ..."
chmod +x $AssemblieFile.fullname
}
}
}

function Start-SeChrome {
Param(
[Parameter(Mandatory = $false)]
[array]$Arguments,
[switch]$HideVersionHint,
[string]$StartURL,
[System.IO.FileInfo]$DefaultDownloadPath,
[System.IO.FileInfo]$ProfileDirectoryPath,
[bool]$DisableBuiltInPDFViewer=$true,
[switch]$Headless,
[switch]$Incognito,
[switch]$Maximized
[switch]$Maximized,
[switch]$Minimized,
[switch]$Fullscreen
)

$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"

if($DefaultDownloadPath){
Write-Host "Setting Default Download directory: $DefaultDownloadPath"
$Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
}

if($DisableBuiltInPDFViewer){
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
BEGIN{
if($Maximized -ne $false -and $Minimized -ne $false) {
throw 'Maximized and Minimized may not be specified together.'
}
elseif($Maximized -ne $false -and $Fullscreen -ne $false){
throw 'Maximized and Fullscreen may not be specified together.'
}
elseif($Minimized -ne $false -and $Fullscreen -ne $false){
throw 'Minimized and Fullscreen may not be specified together.'
}

if($StartURL){
if(![system.uri]::IsWellFormedUriString($StartURL,[System.UriKind]::Absolute)){
throw 'Incorrect StartURL please make sure the URL starts with http:// or https://'
}
}
}
PROCESS{
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"

if ($Headless) {
$Chrome_Options.AddArguments('headless')
}
if($DefaultDownloadPath){
Write-Host "Setting Default Download directory: $DefaultDownloadPath"
$Chrome_Options.AddUserProfilePreference('download', @{'default_directory' = $($DefaultDownloadPath.FullName); 'prompt_for_download' = $false; })
}
if($ProfileDirectoryPath){
Write-Host "Setting Profile directory: $ProfileDirectoryPath"
$Chrome_Options.AddArgument("user-data-dir=$ProfileDirectoryPath")
}

if($DisableBuiltInPDFViewer){
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
}

if ($Headless) {
$Chrome_Options.AddArguments('headless')
}

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

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

if ($Arguments) {
$Chrome_Options.AddArguments($Arguments)
}

if (!$HideVersionHint) {
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
}
if ($Fullscreen) {
$Chrome_Options.AddArguments('start-fullscreen')
}

if ($Arguments) {
foreach ($Argument in $Arguments){
$Chrome_Options.AddArguments($Argument)
}
}

if (!$HideVersionHint) {
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
}

if($IsLinux -or $IsMacOS){
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $AssembliesPath,$Chrome_Options
if($IsLinux -or $IsMacOS){
$Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $AssembliesPath,$Chrome_Options
}
else{
$Driver = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
}

if($Minimized){
$driver.Manage().Window.Minimize();

}

if($StartURL){
Enter-SeUrl -Driver $Driver -Url $StartURL
}
}
else{
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
END{
return $Driver
}
}

Expand Down Expand Up @@ -264,20 +328,29 @@ function Get-SeCookie {
}

function Remove-SeCookie {
param($Driver)

$Driver.Manage().Cookies.DeleteAllCookies()
param(
$Driver,
[switch]$DeleteAllCookies,
[string]$Name
)

if($DeleteAllCookies){
$Driver.Manage().Cookies.DeleteAllCookies()
}
else{
$Driver.Manage().Cookies.DeleteCookieNamed($Name)
}
}

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

<# Selenium Cookie Information
Cookie(String, String)
Expand All @@ -289,37 +362,44 @@ function Set-SeCookie {
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
Begin{
if($ExpiryDate -ne $null -and $ExpiryDate.GetType().Name -ne 'DateTime'){
throw '$ExpiryDate can only be $null or TypeName: System.DateTime'
}
}
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

Process {
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 $Domain -and (!$ExpiryDate -or $ExpiryDate)){
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 'In order to set the cookie the browser needs to be on the cookie domain URL'
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."
}
}
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."
}

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

function Get-SeElementAttribute {
Expand Down
Loading