Test-BrowserLocation.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function Test-BrowserLocation
{
    <#
    .Synopsis
        Tests if the browser is at a specific page.
    .Description
        Tests if the browser is at a specific URL or page name.
    .Example
        Open-Browser -Url http://start-automating.com |
            Test-BrowserLocation -Name Start-Automating
    .Link
        Set-BrowserLocation
    .Link
        Open-Browser
    #>

    [CmdletBinding(DefaultParameterSetName='LocationUrl')]    
    [OutputType([Boolean])]
    param(
    # The Browser Object.
    [Parameter(Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
    [ValidateScript({
        if ($_.psobject.typenames -notcontains 'System.__ComObject' -and -not $_.Quit) {
            throw "Not IE"
        }
        $true
    })]
    $IE,

    # The Expected URL
    [Parameter(Mandatory=$true, ParameterSetName="LocationUrl", Position =0)]    
    [Uri]$Url,
    
    # The Expected Name
    [Parameter(Mandatory=$true, ParameterSetName="LocationName", Position =0)]    
    [string]$Name,
    
    # If set, will perform a wildcard match on the location name or URL
    [switch]$Like,
    # If set, will perform a regular expression match on the location name or URL
    [switch]$Match,
    
    # This script will be executed when the location is determined to be correct
    [ScriptBlock]$On_True,    
    # This script will be executed when the location is determined to be incorrect
    [ScriptBlock]$On_False,
    
    # If set, will try again
    [switch]$TryAgain
    )
    
    process { 
        $triedOnce = $false
        while (-not $triedOnce) {       
            $problem = $false
            if ($psCmdlet.ParameterSetName -eq 'LocationUrl') {
                if ($like) {
                    if ($ie.LocationUrl -notlike $Url) {
                        $problem = $true
                    }
                } elseif ($match ) {
                    if ($ie.LocationUrl -notmatch $Url) {
                        $problem = $true
                    }
                } else {
                    if ($ie.LocationUrl -ne $Url) {
                        $problem = $true                    
                    }                
                }
            } elseif ($psCmdlet.ParameterSetName -eq 'LocationName') {
                if ($like) {
                    if ($ie.LocationName -notlike $name) {
                        $problem = $true
                    }
                } elseif ($match ) {
                    if ($ie.LocationName -notmatch $name) {
                        $problem = $true
                    }
                } else {
                    if ($ie.LocationName -ne $name) {
                        $problem = $true                    
                    }                
                }
            } 
        
            if (-not $problem) {
                if ($psBoundParameters.On_True) 
                {
                    $ie = $ie
                    $sb = [scriptBlock]::Create('param($ie)' + $psBoundParameters.On_True)
                    $null = . $sb $ie
                }                
                $triedOnce = $true
                continue
            } else {
                
                if ($psBoundParameters.On_False) {
                    $ie = $ie
                    $sb = [scriptBlock]::Create('param($ie)' + $psBoundParameters.On_False)
                    $null = . $sb $ie                    
                    if ($TryAgain -and -not $triedOnce) {
                        $triedOnce = $true
                        continue                   
                    } else {
                        break
                    }
                } else {
                    $triedOnce = $true                    
                }
            }
        }
        
        $problem
    }
}