Set-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
function Set-BrowserLocation
{
    <#
    .Synopsis
        Sets the location of an open browser
    .Description
        Sets the location of a browser, and waits for the new location to load
    .Example
        Open-Browser |
            Set-BrowserLocation -Url http://start-automating.com
    .Link
        Open-Browser
    .Link
        Test-BrowserLocation
     
    #>

    [OutputType([PSObject])]
    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 URL to visit
    [Parameter(Mandatory=$true)]    
    [Uri]$Url,
    
    # The page load timeout
    [Timespan]$Timeout = "0:0:30",
    
    # The sleep time between waits for the page to load
    [Timespan]$SleepTime = "0:0:0.01",
        
    # If set, will not wait for the page to load
    [switch]$DoNotWait
    )
    
    process {
        #region Construct Navigate2 Method Arguments
        $Navigate2Args = @("$url", 2)
        if ($targetFrame) {
            $Navigate2Args +=$targetFrame
        } else {
            $Navigate2Args +=$null
        }
        if ($postData) {
            $Navigate2Args +=$postData
        } else {
            $Navigate2Args +=$null
        }
        if ($headers) {
            $Navigate2Args +=$headers
        } else {
            $Navigate2Args +=$null
        }
        #endregion Construct Navigate2 Method Arguments
        
        #region Invoke and Wait
        $ie.Navigate2.Invoke($Navigate2Args )
        if (-not $doNotWait) {
            $ie | Wait-Browser -Timeout $Timeout -Sleeptime $sleepTime
        } else {
            $ie
        }
        #endregion Invoke and Wait
    }
}