Skip to content

Commit

Permalink
#25: adds new Wait-MonocleElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Feb 4, 2020
1 parent f9ebc51 commit 0f79186
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
9 changes: 5 additions & 4 deletions examples/youtube.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#$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
Expand All @@ -17,6 +17,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'
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.1.2'
ModuleVersion = '1.2.0'

# ID used to uniquely identify this module
GUID = '9dc3c8a1-664d-4253-a5d2-920250d3a15f'
Expand Down
11 changes: 9 additions & 2 deletions src/Private/Elements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ function Get-MonocleElementInternal
[string]
$XPath,

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

[switch]
$NoThrow
)

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

$seconds = 0

while ($true) {
Expand All @@ -90,7 +97,7 @@ function Get-MonocleElementInternal
catch {
$seconds++

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

Expand Down
44 changes: 44 additions & 0 deletions src/Public/Elements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,50 @@ function Test-MonocleElement
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()]
[int]
$Timeout = 600
)

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

function Get-MonocleElement
{
[CmdletBinding(DefaultParameterSetName='Id')]
Expand Down

0 comments on commit 0f79186

Please sign in to comment.