Skip to content

Instantly share code, notes, and snippets.

@patrickperrone
Last active August 31, 2017 13:43
Show Gist options
  • Save patrickperrone/0d34d91a682d2021eb9cd0ae19918dbd to your computer and use it in GitHub Desktop.
Save patrickperrone/0d34d91a682d2021eb9cd0ae19918dbd to your computer and use it in GitHub Desktop.
Example Functional Test using LayoutSimulator integrated with Gulp
#Copy this file to the root of your Habitat repo
function Invoke-FunctionalTest
{
[CmdletBinding()]
param
(
[parameter(Position=0, Mandatory=$true)]
[string]$HostName,
[parameter(Position=1, Mandatory=$true)]
[string]$JsonBody,
[parameter(Mandatory=$false, HelpMessage="Make web service request using HTTPS scheme.")]
[switch]$UseHttps,
[parameter(Mandatory=$false, HelpMessage="Duration to wait in seconds before timing out the request.")]
[int]$Timeout = 600
)
process
{
$Timeout = $Timeout * 1000
$scheme = "http"
if ($UseHttps)
{
$scheme = "https"
}
$servicePath = "/sitecore/api/ssc/LayoutSimulator/Simulator/1/Simulate"
$serviceUrl = "{0}://{1}{2}" -f $scheme,$HostName,$servicePath
Invoke-WebRequest $serviceUrl -ContentType "application/json" -Method POST -Body $JsonBody
}
}
function Test-Rendering
{
$testData = @"
{
"LayoutToSimulate" : "<r xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsd=\"http://www.w3.org/2001/XMLSchema\">
<d id=\"{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}\" l=\"{ED043C36-CD48-4EDF-A288-EC404EE1CD42}\">
<r uid=\"{762DFB70-5D70-4205-BE54-C9F364C88E7C}\" ds=\"{EE7C0E8A-37F1-41D7-A1DC-9990A97B93CE}\" id=\"{A25C9850-AAB7-46A6-84B7-0DFAB32FC357}\" par=\"\" ph=\"/page-layout/section_f4fe27af-b867-4e72-8808-843d5781a355/col-wide-1_0f43a3b5-7236-40e6-b86e-5a52b75bbd8b\" />
<r uid=\"{0F43A3B5-7236-40E6-B86E-5A52B75BBD8B}\" id=\"{B21C8A38-51BC-4866-9589-6062D6390247}\" ph=\"/page-layout/section_f4fe27af-b867-4e72-8808-843d5781a355\" />
#<r uid=\"{F4FE27AF-B867-4E72-8808-843D5781A355}\" ds=\"\" id=\"{1149A3C7-C69A-4E6B-A6C3-32687EC2F8D4}\" par=\"UseStaticPlaceholderNames&amp;amp;Type=bg-secondary&amp;amp;Media&amp;amp;Parallax\" ph=\"page-layout\" />
</d></r>"
}
"@
$result = Invoke-FunctionalTest -HostName "habitat.82u4.dev" -JsonBody $testData
#examine result to decide if we passed test
$target = ($result.ParsedHtml.getElementsByTagName("a") | select -ExpandProperty target)
if ($target -ne "_blank")
{
throw "Target must be _blank"
}
}
Test-Rendering
/*****************************
Add the following task to you Habitat gulpfile.js
*****************************/
var spawn = require("child_process").spawn;
gulp.task('07-Run-Functional-Test', function (callback) {
child = spawn("powershell.exe", [". .\\functional-test.ps1"]);
child.stdout.on("data", function (data) {
console.log("Powershell Data: " + data);
});
child.stderr.on("data", function (data) {
console.log("Powershell Errors: " + data);
});
child.on("exit", function () {
console.log("Powershell Script finished");
});
child.stdin.end(); //end input
callback();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment