SHARE
TWEET

Untitled

a guest Mar 21st, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Wait until the browser loads the expected element was some Text data.
  4. #>
  5. function Wait-UntilElementLoaded {
  6.     [CmdletBinding()]
  7.     param(
  8.         [Parameter(Mandatory = $true)]
  9.         [OpenQA.Selenium.IWebDriver] $Driver,
  10.         [Parameter(Mandatory = $true, ParameterSetName = "Id")]
  11.         [string] $Id,
  12.         [Parameter(Mandatory = $true, ParameterSetName = "ClassName")]
  13.         [string] $ClassName,
  14.         [Parameter(Mandatory = $true, ParameterSetName = "Name")]
  15.         [string] $Name,
  16.         [Parameter(Mandatory = $true, ParameterSetName = "TagName")]
  17.         [string] $TagName,
  18.         [Parameter(Mandatory = $true, ParameterSetName = "LinkText")]
  19.         [string] $LinkText,
  20.         [Parameter(Mandatory = $true, ParameterSetName = "XPath")]
  21.         [string] $XPath,
  22.         [int] $TimeOutSeconds = 10
  23.     )
  24.         Write-Verbose ("Wait-UntilElementLoaded: Searching on {0} '{1}'" -f $PSCmdlet.ParameterSetName, (iex "`$$($PSCmdlet.ParameterSetName)"))
  25.    
  26.         switch($PSCmdlet.ParameterSetName) {
  27.             "Id" { $element = Find-SeElement -Driver $Driver -Id $Id }
  28.             "ClassName" { $element = Find-SeElement -Driver $Driver -ClassName $ClassName }
  29.             "Name" { $element = Find-SeElement -Driver $Driver -Name $Name }
  30.             "TagName" { $element = Find-SeElement -Driver $Driver -TagName $TagName }
  31.             "LinkText" { $element = Find-SeElement -Driver $Driver -LinkText $LinkText }
  32.             "XPath" { $element = Find-SeElement -Driver $Driver -XPath $XPath }
  33.         }
  34.    
  35.         $retry = 0
  36.         Write-Verbose "Wait-UntilElementLoaded: $($retry): [string]::IsNullOrWhiteSpace(`$element.Text) = $([string]::IsNullOrWhiteSpace($element.Text))"
  37.         Write-Verbose "Wait-UntilElementLoaded: $($retry): `$element.Text = $($element.Text)"
  38.         while([string]::IsNullOrWhiteSpace($element.Text) -and $retry -lt $TimeOutSeconds) {
  39.             Start-Sleep 1
  40.    
  41.             switch($PSCmdlet.ParameterSetName) {
  42.                 "Id" { $element = Find-SeElement -Driver $Driver -Id $Id }
  43.                 "ClassName" { $element = Find-SeElement -Driver $Driver -ClassName $ClassName }
  44.                 "Name" { $element = Find-SeElement -Driver $Driver -Name $Name }
  45.                 "TagName" { $element = Find-SeElement -Driver $Driver -TagName $TagName }
  46.                 "LinkText" { $element = Find-SeElement -Driver $Driver -LinkText $LinkText }
  47.                 "XPath" { $element = Find-SeElement -Driver $Driver -XPath $XPath }
  48.             }
  49.    
  50.             $retry++
  51.             Write-Verbose "Wait-UntilElementLoaded: $($retry): [string]::IsNullOrWhiteSpace(`$element.Text) = $([string]::IsNullOrWhiteSpace($element.Text))"
  52.             Write-Verbose "Wait-UntilElementLoaded: $($retry): `$element.Text = $($element.Text)"
  53.         }
  54.        
  55.         if([string]::IsNullOrWhiteSpace($element.Text) -and $retry -eq $TimeOutSeconds) {
  56.             $messageFormat = "Element {0} = '{1}' could not be loaded before the timeout ({2} seconds) ran out. Please ensure the element can load in time."
  57.             $message = $messageFormat -f $PSCmdlet.ParameterSetName, (iex "`$$($PSCmdlet.ParameterSetName)"), $TimeOutSeconds
  58.             throw $message
  59.         }
  60.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top