Skip to content

Commit

Permalink
Merge pull request #32 from Badgerati/develop
Browse files Browse the repository at this point in the history
v1.2.0
  • Loading branch information
Badgerati authored Feb 6, 2020
2 parents 908c328 + efc9dfb commit 2c2586c
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Task 'Selenium' {
$packages = @{
'Selenium.WebDriver' = '3.141.0'
'Selenium.Support' = '3.141.0'
'Selenium.WebDriver.ChromeDriver' = '78.0.3904.10500'
'Selenium.WebDriver.ChromeDriver' = '80.0.3987.1600'
'Selenium.WebDriver.IEDriver' = '3.150.1'
'Selenium.WebDriver.GeckoDriver' = '0.26.0'
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ The following is a list of available functions in Monocle:
* Submit-MonocleForm
* Test-MonocleElement
* Test-MonocleElementAttribute
* Wait-MonocleElement
* Wait-MonocleUrl
* Wait-MonocleUrlDifferent
* Wait-MonocleValue
Expand Down
18 changes: 10 additions & 8 deletions examples/youtube.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
#$path = "$($path)/src/Monocle.psm1"
#Import-Module $path -Force -ErrorAction Stop
Import-Module -Name Monocle -Force -ErrorAction Stop
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$path = "$($path)/src/Monocle.psm1"
Import-Module $path -Force -ErrorAction Stop
#Import-Module -Name Monocle -Force -ErrorAction Stop

# Create a browser object
$browser = New-MonocleBrowser -Type Chrome
#$browser = New-MonocleBrowser -Type Firefox

# Monocle runs commands in web flows, for easy disposal and test tracking
# Each flow needs a name
Start-MonocleFlow -Name 'Load YouTube' -Browser $browser -ScriptBlock {
Start-MonocleFlow -Name 'Load YouTube' <#-Browser $browser#> -ScriptBlock {

# Tell the browser which URL to navigate to, will sleep while page is loading
Set-MonocleUrl -Url 'https://www.youtube.com'

# Sets the search bar element to the passed value to query
Get-MonocleElement -Id 'search_query' | Set-MonocleElementValue -Value 'Beerus Madness (Extended)'
Get-MonocleElement -Selector 'input[name=search_query]' | Set-MonocleElementValue -Value 'Beerus Madness (Extended)'
#Get-MonocleElement -Id 'search_query' | Set-MonocleElementValue -Value 'Beerus Madness (Extended)'

# Tells the browser to click the search button
Wait-MonocleElement -Id 'search-icon-legacy'
Get-MonocleElement -Id 'search-icon-legacy' | Invoke-MonocleElementClick

# Though all commands sleep when the page is busy, some buttons use javascript
Expand All @@ -35,5 +37,5 @@ Start-MonocleFlow -Name 'Load YouTube' -Browser $browser -ScriptBlock {

} -CloseBrowser -ScreenshotOnFail

# or close the browser manually:
# or close the browser manually if not using default:
#Close-MonocleBrowser -Browser $browser
2 changes: 1 addition & 1 deletion src/Monocle.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'Monocle.psm1'

# Version number of this module.
ModuleVersion = '1.1.2'
ModuleVersion = '1.2.0'

# ID used to uniquely identify this module
GUID = '9dc3c8a1-664d-4253-a5d2-920250d3a15f'
Expand Down
47 changes: 44 additions & 3 deletions src/Private/Elements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Get-MonocleElementInternal
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[ValidateSet('Id', 'Tag', 'XPath')]
[ValidateSet('Id', 'Tag', 'XPath', 'Selector')]
[string]
$FilterType,

Expand Down Expand Up @@ -59,11 +59,22 @@ function Get-MonocleElementInternal
[string]
$XPath,

[Parameter()]
[string]
$Selector,

[Parameter()]
[int]
$Timeout = 0,

[switch]
$NoThrow
)

$timeout = Get-MonocleTimeout
if ($Timeout -le 0) {
$Timeout = Get-MonocleTimeout
}

$seconds = 0

while ($true) {
Expand All @@ -85,12 +96,16 @@ function Get-MonocleElementInternal
'xpath' {
return (Get-MonocleElementByXPath -XPath $XPath -NoThrow:$NoThrow)
}

'selector' {
return (Get-MonocleElementBySelector -Selector $Selector -NoThrow:$NoThrow)
}
}
}
catch {
$seconds++

if ($seconds -ge $timeout) {
if ($seconds -ge $Timeout) {
throw $_.Exception
}

Expand Down Expand Up @@ -238,4 +253,30 @@ function Get-MonocleElementByXPath
Element = $element
Id = "<$($XPath)>"
}
}

function Get-MonocleElementBySelector
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]
$Selector,

[switch]
$NoThrow
)

Write-Verbose -Message "Finding element with selector '$Selector'"
$element = Invoke-MonocleJavaScript -Script 'return document.querySelector(arguments[0])' -Arguments $Selector

# throw error if can't find element
if (($null -eq $element) -and !$NoThrow) {
throw "Element with selector of '$Selector' not found"
}

return @{
Element = $element
Id = "<$($Selector)>"
}
}
67 changes: 63 additions & 4 deletions src/Public/Elements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ function Test-MonocleElement

[Parameter(ParameterSetName='XPath')]
[string]
$XPath
$XPath,

[Parameter(ParameterSetName='Selector')]
[string]
$Selector
)

$result = $null
Expand All @@ -205,13 +209,63 @@ function Test-MonocleElement
-AttributeName $AttributeName `
-AttributeValue $AttributeValue `
-ElementValue $ElementValue `
-XPath $XPath
-XPath $XPath `
-Selector $Selector
}
catch { }

return (($null -ne $result) -and ($null -ne $result.Element))
}

function Wait-MonocleElement
{
[CmdletBinding(DefaultParameterSetName='Id')]
param (
[Parameter(Mandatory=$true, ParameterSetName='Id')]
[string]
$Id,

[Parameter(Mandatory=$true, ParameterSetName='Tag')]
[string]
$TagName,

[Parameter(ParameterSetName='Tag')]
[string]
$AttributeName,

[Parameter(ParameterSetName='Tag')]
[string]
$AttributeValue,

[Parameter(ParameterSetName='Tag')]
[string]
$ElementValue,

[Parameter(ParameterSetName='XPath')]
[string]
$XPath,

[Parameter(ParameterSetName='Selector')]
[string]
$Selector,

[Parameter()]
[int]
$Timeout = 600
)

Get-MonocleElementInternal `
-FilterType $PSCmdlet.ParameterSetName `
-Id $Id `
-TagName $TagName `
-AttributeName $AttributeName `
-AttributeValue $AttributeValue `
-ElementValue $ElementValue `
-XPath $XPath `
-Selector $Selector `
-Timeout $Timeout | Out-Null
}

function Get-MonocleElement
{
[CmdletBinding(DefaultParameterSetName='Id')]
Expand Down Expand Up @@ -239,7 +293,11 @@ function Get-MonocleElement

[Parameter(ParameterSetName='XPath')]
[string]
$XPath
$XPath,

[Parameter(ParameterSetName='Selector')]
[string]
$Selector
)

# attempt to get the monocle element
Expand All @@ -250,7 +308,8 @@ function Get-MonocleElement
-AttributeName $AttributeName `
-AttributeValue $AttributeValue `
-ElementValue $ElementValue `
-XPath $XPath
-XPath $XPath `
-Selector $Selector

# set the meta id on the element
Set-MonocleElementId -Element $result.Element -Id $result.Id
Expand Down
10 changes: 8 additions & 2 deletions src/Public/Flow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ function Start-MonocleFlow
[string]
$ScreenshotPath,

[Parameter(Mandatory=$true)]
[Parameter()]
[OpenQA.Selenium.Remote.RemoteWebDriver]
$Browser,
$Browser = $null,

[Parameter(ParameterSetName='Screenshot')]
[switch]
Expand All @@ -84,6 +84,12 @@ function Start-MonocleFlow
# set the output depth
$env:MONOCLE_OUTPUT_DEPTH = '1'

# if no browser, set chrome as default
if ($null -eq $Browser) {
$CloseBrowser = $true
$Browser = New-MonocleBrowser -Type Chrome
}

# invoke the logic
try {
Write-MonocleHost -Message "`nFlow: $Name" -NoIndent
Expand Down
Binary file modified src/lib/Browsers/linux/chromedriver
Binary file not shown.
Binary file modified src/lib/Browsers/mac/chromedriver
Binary file not shown.
Binary file modified src/lib/Browsers/win/chromedriver.exe
Binary file not shown.

0 comments on commit 2c2586c

Please sign in to comment.