Use the script below during the demo to follow along.

Don’t forget to go to http://bhurtdemo.cloudapp.net and create your own account so you can use your own credentials to follow along.
** This Jenkins instance does not use SSL. Don’t use any sensitive creds, only throw away demo passwords.


<#
Set up base configuration variables
#>
$baseUri = 'http://bhurtdemo.cloudapp.net'

# Chrome's Agent String
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"

$jobToInvoke = "Test Automation Step"

<#
Grab the home page. Note the user agent string. It can effect the page you get.
#>
$homepageResponse = Invoke-WebRequest `
-uri "$baseUri/login?from=%2F" `
-SessionVariable session `
-UserAgent $userAgent

<#
Examine the session variable.
#>
$session
$session.Cookies.GetCookies($baseUri)

<#
Call the response variable and look at some of the properties it gives us.
#>
$homepageResponse

<#
The forms are already parsed out of the page for you.
#>
$homepageResponse.Forms

<#
Below we set the form's field values.
#>

$loginForm = $homepageResponse.Forms['login']

$loginForm.Fields.j_username = 'bill'
$loginForm.Fields.j_password = 'nothing1'

$loginUri = "$baseUri/$($loginForm.Action)"

<#
Submit the form back to the server. Don't forget the session variable.
#>
$loginResponse = Invoke-WebRequest `
-Uri $loginUri `
-Method Post `
-Body $loginForm.Fields `
-WebSession $session `
-userAgent $userAgent

<#
Look at the cookies again. We now see our authorization cookie.
#>
$session.Cookies.GetCookies($baseUri)

<#
The links are also pre parsed out of the return for you. Examine them here.
#>
$loginResponse.Links | Format-Table innerText, href

<#
Use the Where() method to do method chaining and avoid pipelining and using $_. Here we get the link to the job we want
to kick off. We use this method in case the exact url ever changes. In Jenkins this is unlikely, but other systems might
not be so consistent.
#>
$jobUri = "$baseUri/$($loginResponse.Links.Where({$_.innerText -eq $jobToInvoke}).href)"
$jobPage = Invoke-WebRequest `
-Uri $jobUri `
-WebSession $session `
-userAgent $userAgent

<#
Note the latest build link and that we'll come back to it.
#>
$jobPage.Links | Format-Table innerText, href

<#
This syntax looks complex at first, but it's just string substitution using an expression.
#>
$buildUri = "$baseUri$($jobPage.Links.Where({$_.innerText -eq "Build Now"}).href)"

$buildResponse = Invoke-WebRequest `
-Uri $buildUri `
-WebSession $session `
-userAgent $userAgent `
-Method POST

$lastBuildConsoleUri = "$($jobUri)lastBuild/console"

$lastConsole = Invoke-WebRequest `
-Uri $lastBuildConsoleUri `
-WebSession $session `
-userAgent $userAgent

<#
Below, the $output variable will hold the console output of the last job invokation. You can inspect it to find out the
specifics of how your job went. This shows how you can target specific parts of the page easily, using PowerShell as a
query language for the html page if the elements you want aren't in a category already parsed out, like forms or links.
#>
$output = $lastConsole.ParsedHtml.body.getElementsByClassName("console-output")

<#
We find the output is easily parsed into a hashtable that we can easily inspect and run tests against if we want to.
#>
$props = ($output.item().outerText).Split() | Select-String "=" | ConvertFrom-CSV -Delimiter "=" -Header "key","value"

$hashtable = @{}
foreach($prop in $props)
{
$hashtable[$prop.key] = $prop.value
}
<#
Set up a pester test.
#>

Set-Location "$env:userprofile\documents"

New-Item -ItemType directory -Path .\PesterTests

Import-Module Pester

New-Fixture -Path .\PesterTests -Name Verify-JenkinsJobResult

This script is a cleaned up version for running the pester tests. Don’t forget to change the credentials.


function Verify-JenkinsJobResult {

$baseUri = 'http://bhurtdemo.cloudapp.net'

$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'

$jobToInvoke = "Test Automation Step"

$homepageResponse = Invoke-WebRequest `
-uri "$baseUri/login?from=%2F" `
-SessionVariable session `
-UserAgent $userAgent

$loginForm = $homepageResponse.Forms['login']

$loginForm.Fields.j_username = 'bill'
$loginForm.Fields.j_password = 'nothing1'

$loginUri = "$baseUri/$($loginForm.Action)"

$loginResponse = Invoke-WebRequest `
-Uri $loginUri `
-Method Post `
-Body $loginForm.Fields `
-WebSession $session `
-userAgent $userAgent

$jobUri = "$baseUri/$($loginResponse.Links.Where({$_.innerText -eq $jobToInvoke}).href)"
$jobPage = Invoke-WebRequest `
-Uri $jobUri `
-WebSession $session `
-userAgent $userAgent

$buildUri = "$baseUri$($jobPage.Links.Where({$_.innerText -eq "Build Now"}).href)"

$buildResponse = Invoke-WebRequest `
-Uri $buildUri `
-WebSession $session `
-userAgent $userAgent `
-Method POST

$lastBuildConsoleUri = "$($jobUri)lastBuild/console"

$lastConsole = Invoke-WebRequest `
-Uri $lastBuildConsoleUri `
-WebSession $session `
-userAgent $userAgent

$output = $lastConsole.ParsedHtml.body.getElementsByClassName("console-output")

$props = ($output.item().outerText).Split() | Select-String "=" | ConvertFrom-CSV -Delimiter "=" -Header "key","value"

$hashtable = @{}
foreach($prop in $props)
{
$hashtable[$prop.key] = $prop.value
}

Write-OutPut $hashtable
}

This script is the Pester tests.


$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "Verify-JenkinsJobResult" {
$result = Verify-JenkinsJobResult

It "returns results" {
$result | Should Not BeNullOrEmpty
}

it "has correct username" {
$result.USERNAME | Should Be "JENKINSDEMO$"
}

it "has no workspace" {
Test-Path ($result.WORKSPACE) | Should Be $false
}
}

Advertisements