Skip to content
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

Adds support for waiting for element to be visible #48

Merged
merged 4 commits into from
Feb 12, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,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
15 changes: 11 additions & 4 deletions examples/iframes.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
param(
[Parameter(Mandatory=$true)]
[string]
$Url
$Url,

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

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

$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Expand All @@ -17,10 +25,9 @@ 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 'NN1 1AA'
Get-MonocleElement -Id 'postcode_search' | Set-MonocleElementValue -Value $Postcode
Get-MonocleElement -Id 'findAddressbtn' | Invoke-MonocleElementClick
Get-MonocleElement -Id 'yourAddress' -WaitVisible | Set-MonocleElementValue -Value $Address
}

Start-Sleep -Seconds 5

} -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
72 changes: 69 additions & 3 deletions src/Public/Elements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function Set-MonocleElementValue
$select.SelectByText($Value)
}
catch {
$select | Out-Default
$select.Options | Out-Default
$select.SelectByValue($Value)
}
}
Expand Down Expand Up @@ -317,10 +319,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 +337,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 +389,9 @@ function Get-MonocleElement
[string]
$Selector,

[switch]
$WaitVisible,

[switch]
$All
)
Expand All @@ -386,10 +413,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