Public/Misc.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function Start-MonocleSleep
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [int]
        $Seconds
    )

    Write-MonocleHost -Message "Sleeping for $Seconds second(s)"
    Start-Sleep -Seconds $Seconds
}

function Invoke-MonocleScreenshot
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $Name,

        [string]
        $Path
    )

    $screenshot = $Browser.GetScreenshot()

    if ([string]::IsNullOrWhiteSpace($Path)) {
        $Path = $pwd
    }

    $Name = ($Name -replace ' ', '_')
    $filepath = Join-Path $Path "$($Name).png"
    $screenshot.SaveAsFile($filepath, [OpenQA.Selenium.ScreenshotImageFormat]::Png)

    Write-MonocleHost -Message "Screenshot saved to: $filepath"
    return $filepath
}

function Save-MonocleImage
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [OpenQA.Selenium.IWebElement]
        $Element,

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

    # get the meta id of the element
    $id = Get-MonocleElementId -Element $Element
    Write-MonocleHost -Message "Downloading image from $($id)"

    $tag = $Element.TagName
    if (@('img', 'image') -inotcontains $tag) {
        throw "Element $($id) is not an image element: $tag"
    }

    $src = Get-MonocleElementAttribute -Element $Element -Name 'src'
    if ([string]::IsNullOrWhiteSpace($src)) {
        throw "Element $($id) has no src attribute"
    }

    Invoke-MonocleDownloadImage -Source $src -Path $FilePath
}

function Restart-MonocleBrowser
{
    [CmdletBinding()]
    param ()

    Write-MonocleHost -Message "Refreshing the Browser"
    $Browser.Navigate().Refresh()
    Start-MonocleSleepWhileBusy
    Start-Sleep -Seconds 2
}

function Get-MonocleHtml
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $FilePath
    )

    $content = $Browser.PageSource

    if ([string]::IsNullOrWhiteSpace($FilePath)) {
        Write-MonocleHost -Message "Retrieving the current page's HTML content"
        return $content
    }

    Write-MonocleHost -Message "Writing the current page's HTML to '$($FilePath)'"
    $content | Out-File -FilePath $FilePath -Force | Out-Null
}

function Invoke-MonocleJavaScript
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Script,

        [Parameter()]
        [object[]]
        $Arguments
    )

    $Browser.ExecuteScript($Script, $Arguments)
}

function Install-MonocleDriver
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateSet('IE', 'Chrome', 'Firefox')]
        [string]
        $Type,

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

    # create the custom driver folder
    $customDir = Get-MonocleCustomDriverPath
    New-Item -Path $customDir -ItemType Directory -Force -ErrorAction Stop | Out-Null

    # remove temp if it exists
    $tempDir = Join-Path $customDir 'temp'
    if (Test-Path $tempDir) {
        Remove-Item -Path $tempDir -Force -Recurse -ErrorAction Stop | Out-Null
    }

    # down the driver to a temp dir within custom
    $driverName = (@{
        Chrome = 'Selenium.WebDriver.ChromeDriver'
        IE = 'Selenium.WebDriver.IEDriver'
        Firefox = 'Selenium.WebDriver.GeckoDriver'
    })[$Type]

    Write-Host "Downloading $($Type) [$($Version)] driver..."
    nuget install $driverName -version $Version -outputdirectory $tempDir | Out-Null
    if (!$?) {
        throw "Failed to download the $($Type) [$($Version)] driver"
    }

    # create the os dirs
    $winDir = Join-Path $customDir 'win'
    $nixDir = Join-Path $customDir 'linux'
    $macDir = Join-Path $customDir 'mac'

    New-Item -Path $winDir -ItemType Directory -Force | Out-Null
    New-Item -Path $nixDir -ItemType Directory -Force | Out-Null
    New-Item -Path $macDir -ItemType Directory -Force | Out-Null

    # move the drivers into an appropraite structure
    switch ($Type.ToLowerInvariant()) {
        'ie' {
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/*" -Destination $winDir -Recurse -Force | Out-Null
        }

        'chrome' {
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/win32/*" -Destination $winDir -Recurse -Force | Out-Null
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/linux64/*" -Destination $nixDir -Recurse -Force | Out-Null
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/mac64/*" -Destination $macDir -Recurse -Force | Out-Null
        }

        'firefox' {
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/win64/*" -Destination $winDir -Recurse -Force | Out-Null
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/linux64/*" -Destination $nixDir -Recurse -Force | Out-Null
            Copy-Item -Path "$($tempDir)/$($driverName).$($Version)/driver/mac64/*" -Destination $macDir -Recurse -Force | Out-Null
        }
    }

    # remove the temp dir in custom
    if (Test-Path $tempDir) {
        Remove-Item -Path $tempDir -Force -Recurse -ErrorAction Stop | Out-Null
    }
}