Skip to content

Commit

Permalink
Merge pull request #51 from Badgerati/develop
Browse files Browse the repository at this point in the history
v1.3.1
  • Loading branch information
Badgerati committed Feb 13, 2020
2 parents ba80464 + 1bf6f8e commit b2b98fb
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ The following is a list of available functions in Monocle:
* Clear-MonocleElementValue
* Close-MonocleBrowser
* Edit-MonocleUrl
* Enter-MonocleFrame
* Get-Monocle2FACode
* Get-MonocleElement
* Get-MonocleElementAttribute
Expand Down Expand Up @@ -115,6 +116,7 @@ The following is a list of available functions in Monocle:
* Test-MonocleElementCSS
* Test-MonocleElementVisible
* Wait-MonocleElement
* Wait-MonocleElementVisible
* Wait-MonocleUrl
* Wait-MonocleUrlDifferent
* Wait-MonocleValue
Expand Down
2 changes: 1 addition & 1 deletion examples/google.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Start-MonocleFlow -Name 'Google Search' -Browser $browser -ScriptBlock {
Get-MonocleElement -Id 'logo' | Invoke-MonocleElementClick

# ensure we're back home
Wait-MonocleElement -Id 'q'
Wait-MonocleElement -Id 'q' | Out-Null

} -CloseBrowser
33 changes: 33 additions & 0 deletions examples/iframes.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
param(
[Parameter(Mandatory=$true)]
[string]
$Url,

[Parameter(Mandatory=$true)]
[string]
$Postcode,

[Parameter(Mandatory=$true)]
[string]
$Address
)

$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$path = "$($path)/src/Monocle.psm1"
Import-Module $path -Force -ErrorAction Stop

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

# Monocle runs commands in web flows, for easy disposal and test tracking
Start-MonocleFlow -Name 'Elements in iFrames' -Browser $browser -ScriptBlock {

Set-MonocleUrl -Url $Url

Get-MonocleElement -Id 'fillform-frame-1' | Enter-MonocleFrame -ScriptBlock {
Get-MonocleElement -Id 'postcode_search' | Set-MonocleElementValue -Value $Postcode
Get-MonocleElement -Id 'findAddressbtn' | Invoke-MonocleElementClick
Get-MonocleElement -Id 'yourAddress' -WaitVisible | Set-MonocleElementValue -Value $Address
}

} -CloseBrowser
30 changes: 30 additions & 0 deletions examples/scrolling.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$path = "$($path)/src/Monocle.psm1"
Import-Module $path -Force -ErrorAction Stop

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

# Monocle runs commands in web flows, for easy disposal and test tracking
Start-MonocleFlow -Name 'Scrolling' -Browser $browser -ScriptBlock {

# navigate to google
Set-MonocleUrl -Url 'https://en.wikipedia.org/wiki/PowerShell' -Force

# move to the middle of the page
Move-MonoclePage -To Middle
Start-MonocleSleep -Seconds 2

# move to a specific position
Move-MonoclePage -Position 2000
Start-MonocleSleep -Seconds 2

# move to the top of the page
Move-MonoclePage -To Top
Start-MonocleSleep -Seconds 2

# move to the footer - by element
Get-MonocleElement -Id 'footer' | Move-MonoclePage
Start-MonocleSleep -Seconds 2

} -CloseBrowser
2 changes: 1 addition & 1 deletion examples/youtube.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Start-MonocleFlow -Name 'Load YouTube' -Browser $browser -ScriptBlock {
#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'
Wait-MonocleElement -Id 'search-icon-legacy' | Out-Null
Get-MonocleElement -Id 'search-icon-legacy' | Invoke-MonocleElementClick

# Though all commands sleep when the page is busy, some buttons use javascript
Expand Down
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.3.0'
ModuleVersion = '1.3.1'

# ID used to uniquely identify this module
GUID = '9dc3c8a1-664d-4253-a5d2-920250d3a15f'
Expand Down
12 changes: 11 additions & 1 deletion src/Private/Tools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,14 @@ function Test-MonocleUrl
function Get-MonocleCustomDriverPath
{
return './custom_drivers'
}
}

function Add-MonocleOutputDepth
{
$env:MONOCLE_OUTPUT_DEPTH = [string](([int]$env:MONOCLE_OUTPUT_DEPTH) + 1)
}

function Remove-MonocleOutputDepth
{
$env:MONOCLE_OUTPUT_DEPTH = [string](([int]$env:MONOCLE_OUTPUT_DEPTH) - 1)
}
107 changes: 104 additions & 3 deletions src/Public/Elements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,16 @@ function Wait-MonocleElement

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

[switch]
$WaitVisible,

[switch]
$All
)

Get-MonocleElementInternal `
$result = Get-MonocleElementInternal `
-FilterType $PSCmdlet.ParameterSetName `
-Id $Id `
-TagName $TagName `
Expand All @@ -329,7 +335,23 @@ function Wait-MonocleElement
-ElementValue $ElementValue `
-XPath $XPath `
-Selector $Selector `
-Timeout $Timeout | Out-Null
-Timeout $Timeout `
-All:$All

# set the meta id on the element
@($result.Element) | ForEach-Object {
Set-MonocleElementId -Element $_ -Id $result.Id
}

# wait for the elements to be visible
if ($WaitVisible) {
@($result.Element) | ForEach-Object {
$_ | Wait-MonocleElementVisible | Out-Null
}
}

# return the element
return $result.Element
}

function Get-MonocleElement
Expand Down Expand Up @@ -365,6 +387,9 @@ function Get-MonocleElement
[string]
$Selector,

[switch]
$WaitVisible,

[switch]
$All
)
Expand All @@ -386,10 +411,49 @@ function Get-MonocleElement
Set-MonocleElementId -Element $_ -Id $result.Id
}

# wait for the elements to be visible
if ($WaitVisible) {
@($result.Element) | ForEach-Object {
$_ | Wait-MonocleElementVisible | Out-Null
}
}

# return the element
return $result.Element
}

function Wait-MonocleElementVisible
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[OpenQA.Selenium.IWebElement]
$Element,

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

# get the meta id of the element
$id = Get-MonocleElementId -Element $Element
Write-MonocleHost -Message "Waiting for element to be visible: $($id)"

# wait for the element to be visible
$seconds = 0

while ($seconds -le $Timeout) {
if (($Element | Test-MonocleElementVisible)) {
return $Element
}

$seconds++
Start-Sleep -Seconds 1
}

throw "Element '$($id)' was not visible after $($Timeout) seconds"
}

function Measure-MonocleElement
{
[CmdletBinding(DefaultParameterSetName='Id')]
Expand Down Expand Up @@ -683,4 +747,41 @@ function Test-MonocleElementCSS
)

return (Invoke-MonocleJavaScript -Script 'arguments[0].style[arguments[1]] == arguments[2]' -Arguments $Element, $Name, $Value)
}

function Enter-MonocleFrame
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[OpenQA.Selenium.IWebElement]
$Element,

[Parameter(Mandatory=$true)]
[scriptblock]
$ScriptBlock
)

# get the meta id of the element
$id = Get-MonocleElementId -Element $Element

try {
# enter the iframe
Write-MonocleHost -Message "Entering iFrame: $($id)"
$Browser.SwitchTo().Frame($Element) | Out-Null

# update the depth of output
Add-MonocleOutputDepth

# run the scriptblock
. $ScriptBlock
}
finally {
# reset the depth
Remove-MonocleOutputDepth

# exit the iframe back to the default frame
Write-MonocleHost -Message "Exiting iFrame: $($id)"
$Browser.SwitchTo().ParentFrame() | Out-Null
}
}
4 changes: 2 additions & 2 deletions src/Public/Flow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function Invoke-MonocleRetryScript
}

# update the depth of output
$env:MONOCLE_OUTPUT_DEPTH = [string](([int]$env:MONOCLE_OUTPUT_DEPTH) + 1)
Add-MonocleOutputDepth

# attempt the logic
$attempt = 1
Expand All @@ -187,5 +187,5 @@ function Invoke-MonocleRetryScript
}

# reset the depth
$env:MONOCLE_OUTPUT_DEPTH = [string](([int]$env:MONOCLE_OUTPUT_DEPTH) - 1)
Remove-MonocleOutputDepth
}
38 changes: 27 additions & 11 deletions src/Public/Page.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,36 @@ function Move-MonoclePage

[Parameter(ParameterSetName='Position')]
[int]
$Position
$Position,

[Parameter(ParameterSetName='Element', ValueFromPipeline=$true)]
[OpenQA.Selenium.IWebElement]
$Element
)

if ($PSCmdlet.ParameterSetName -ieq 'to') {
$size = Get-MonoclePageSize
$Position = (@{
Bottom = $size.Height
Middle = $size.Height * 0.5
Top = 0
})[$To]
}
switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) {
# literal positions
{ @('to', 'position') -icontains $_ } {
if ($PSCmdlet.ParameterSetName -ieq 'to') {
$size = Get-MonoclePageSize
$Position = (@{
Bottom = $size.Height
Middle = $size.Height * 0.5
Top = 0
})[$To]
}

Write-MonocleHost -Message "Scrolling to: $Position"
Invoke-MonocleJavaScript -Arguments $Position -Script 'window.scrollTo(0, arguments[0])' | Out-Null
}

Write-MonocleHost -Message "Scrolling to: $Position"
Invoke-MonocleJavaScript -Arguments $Position -Script 'window.scrollTo(0, arguments[0])' | Out-Null
# elements
'element' {
$id = Get-MonocleElementId -Element $Element
Write-MonocleHost -Message "Moving page to element: $($id)"
Invoke-MonocleJavaScript -Arguments $Element -Script 'arguments[0].scrollIntoView(true)' | Out-Null
}
}
}

function Get-MonoclePageSize
Expand Down

0 comments on commit b2b98fb

Please sign in to comment.