Skip to content

Commit

Permalink
Merge pull request #8 from Badgerati/develop
Browse files Browse the repository at this point in the history
v0.12.3
  • Loading branch information
Badgerati committed Oct 4, 2019
2 parents dcb8f72 + c755894 commit 990d93c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
Binary file modified src/Monocle.psd1
Binary file not shown.
68 changes: 46 additions & 22 deletions src/Private/Tools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ function Test-MonocleUrl
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string]
$Url
$Url,

[Parameter()]
[int]
$Attempts = 1
)

# truncate the URL of any query parameters
Expand All @@ -92,37 +96,57 @@ function Test-MonocleUrl
$code = 200
$message = [string]::Empty

try {
if ($PSVersionTable.PSVersion.Major -le 5) {
$result = Invoke-WebRequest -Uri $Url -TimeoutSec 30 -UseBasicParsing
$attempt = 1
while ($attempt -le $Attempts) {
try {
Write-MonocleHost -Message "Testing: $url [attempt: $($attempt)]"

if ($PSVersionTable.PSVersion.Major -le 5) {
$result = Invoke-WebRequest -Uri $Url -TimeoutSec 30 -UseBasicParsing -ErrorAction Stop
}
else {
$result = Invoke-WebRequest -Uri $Url -TimeoutSec 30 -ErrorAction Stop
}

$code = [int]$result.StatusCode
$message = $result.StatusDescription
}
else {
$result = Invoke-WebRequest -Uri $Url -TimeoutSec 30
catch [System.Net.WebException] {
$ex = $_.Exception

# if the exception doesn't contain a Response, then either the
# host doesn't exist, there were SSL issues, or something else went wrong
if ($null -eq $ex.Response) {
$code = -1
$message = $ex.Message
}
else {
$code = [int]$ex.Response.StatusCode.Value__
$message = $ex.Response.StatusDescription
}
}

$code = [int]$result.StatusCode
$message = $result.StatusDescription
}
catch [System.Net.WebException]
{
$ex = $_.Exception

# if the exception doesn't contain a Response, then either the
# host doesn't exist, there were SSL issues, or something else went wrong
if ($null -eq $ex.Response) {
catch {
$code = -1
$message = $ex.Message
$message = $_.Exception.Message
}

if (($code -eq -1) -or ($code -ge 400)) {
$attempt++
Start-Sleep -Seconds 1

if ($attempt -gt $Attempts) {
break
}
}
else {
$code = [int]$ex.Response.StatusCode.Value__
$message = $ex.Response.StatusDescription
break
}
}

# anything that is 1xx-2xx is normally successful, anything that's
# 300+ is normally always a failure to load
# 400+ is normally always a failure to load
# -1 is a fatal error (SSL, invalid host, etc)
if (($code -eq -1) -or ($code -ge 300)) {
if (($code -eq -1) -or ($code -ge 400)) {
throw "Failed to load URL: '$Url'`nStatus: $code`nMessage: $message"
}

Expand Down
6 changes: 4 additions & 2 deletions src/Public/Flow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ function Start-MonocleFlow
[scriptblock]
$ScriptBlock,

[Parameter(ParameterSetName='Screenshot')]
[Parameter()]
[string]
$ScreenshotPath,

[switch]
$Visible,

[Parameter(ParameterSetName='Screenshot')]
[switch]
$ScreenshotOnFail,

Expand All @@ -27,7 +29,7 @@ function Start-MonocleFlow
# create a new browser
$Browser = New-Object -ComObject InternetExplorer.Application
if (!$? -or ($null -eq $Browser)) {
throw 'Failed to create Browser for IE.'
throw 'Failed to create Browser for IE'
}

$Browser.Visible = [bool]$Visible
Expand Down Expand Up @@ -98,7 +100,7 @@ function Invoke-MonocleRetryScript

# attempt the logic
$attempt = 1
while ($attempt -lt $Attempts) {
while ($attempt -le $Attempts) {
Write-MonocleHost -Message "Invoking '$($Name)' logic [attempt: $($attempt)]" -Backdent

try {
Expand Down
20 changes: 20 additions & 0 deletions src/Public/Misc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,24 @@ function Restart-MonocleBrowser
$Browser.Refresh()
Start-MonocleSleepWhileBusy
Start-Sleep -Seconds 2
}

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

$content = $Browser.Document.IHTMLDocument3_documentElement.outerHTML

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
}
12 changes: 6 additions & 6 deletions src/Public/Url.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ function Set-MonocleUrl
$Force
)

# Test the URL first, ensure it exists
$code = 0
if (!$Force) {
$code = Test-MonocleUrl -Url $Url
}

# ensure attempts is >=1
if ($Attempts -le 0) {
$Attempts = 1
}

# Test the URL first, ensure it exists
$code = 0
if (!$Force) {
$code = Test-MonocleUrl -Url $Url -Attempts $Attempts
}

# Browse to the URL and wait till it loads
$attempt = 1
while ($attempt -le $Attempts) {
Expand Down

0 comments on commit 990d93c

Please sign in to comment.