Last active
January 29, 2023 12:21
-
-
Save vince-geekcore/8a85d0c760bd6cc69ee3acabce001bc1 to your computer and use it in GitHub Desktop.
Sitecore Experience Editor 8.2-2 compatible warmup PhantomJS script to be triggered using Powershell (for example as Azure WebJob). Based on PhantomJS because it's already available on Sitecore 8 environments. Some paths are currently still hardcoded for webapps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var page = new WebPage(), testindex = 0, loadInProgress = false; | |
var args = require('system').args; | |
var host = args[1]; | |
var username = args[2]; | |
var password = args[3]; | |
var pageEditorUrl = args[4]; | |
var token; | |
var system = require('system'); | |
phantom.cookiesEnabled = true; | |
phantom.javascriptEnabled = true; | |
page.settings.javascriptEnabled = true; | |
page.settings.loadImages = false; | |
console.log("Launching Experience Editor Warmup script:"); | |
console.log("DIAG User:" + username); | |
console.log("DIAG Host:" + host); | |
console.log("DIAG PageEditorUrl:" + pageEditorUrl); | |
steps = [ | |
function () { | |
page.open(host + "/sitecore/login", function (status) { | |
if (status == 'fail') | |
{ | |
console.log('Sitecore login call failed') | |
phantom.exit(1); | |
} | |
}); | |
}, | |
function () { | |
var auth = { user: username, pass: password }; | |
page.evaluate(function (auth) { | |
var arr = document.getElementsByClassName("form-signin"); | |
var i; | |
for (i = 0; i < arr.length; i++) { | |
if (arr[i].getAttribute('method') == "post") { | |
arr[i].elements["UserName"].value = auth.user; | |
arr[i].elements["Password"].value = auth.pass; | |
return; | |
} | |
} | |
}, auth); | |
}, | |
function () { | |
page.evaluate(function () { | |
console.log("Submitting login"); | |
var submitbtn = document.querySelectorAll("input[type=submit]") | |
if (!submitbtn[0]) | |
{ | |
console.log('Sitecore login button not found') | |
phantom.exit(1); | |
} | |
submitbtn[0].click(); | |
}); | |
}, | |
function () { | |
page.evaluate(function () { | |
}); | |
}, | |
function () { | |
var url = host.concat(pageEditorUrl); | |
page.open(url, function (status) { | |
}); | |
}, | |
function () { | |
token = page.evaluate(function () { | |
console.log(document.querySelectorAll('html')[0].outerHTML); | |
var verificationToken = document.getElementsByName("__RequestVerificationToken"); | |
return verificationToken[0]; | |
}); | |
}, | |
function () { | |
var logoutUrl = host.concat('/sitecore/shell/api/sitecore/Authentication/Logout?sc_database=master'); | |
console.log("Logging out"); | |
var postData = "__RequestVerificationToken=".concat(token.value); | |
page.open(logoutUrl, 'POST', postData, function (status) { | |
}); | |
}, | |
function () { | |
var url = host.concat('/sitecore/login'); | |
page.open(url, function (status) { | |
}); | |
}, | |
function () { | |
page.evaluate(function () { | |
console.log(document.querySelectorAll('html')[0].outerHTML); | |
console.log("Logged out"); | |
}); | |
} | |
]; | |
interval = setInterval(executeRequestsStepByStep, 150); | |
function executeRequestsStepByStep() { | |
if (loadInProgress == false && typeof steps[testindex] == "function") { | |
steps[testindex](); | |
testindex++; | |
} | |
if (typeof steps[testindex] != "function") { | |
console.log("test complete!"); | |
phantom.exit(); | |
} | |
} | |
page.onLoadStarted = function () { | |
loadInProgress = true; | |
console.log('Loading started'); | |
}; | |
page.onLoadFinished = function () { | |
loadInProgress = false; | |
console.log('Loading finished'); | |
}; | |
page.onConsoleMessage = function (msg) { | |
console.log(msg); | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$CurrentDir = Split-Path -Parent $MyInvocation.MyCommand.Path | |
[xml]$ConfigFile = Get-Content $CurrentDir"\experience-editor.xml" | |
$settings = @{ | |
Username = $ConfigFile.Settings.Username | |
Password = $ConfigFile.Settings.Password | |
ExperienceEditorUrl = $ConfigFile.Settings.ExperienceEditorUrl | |
Hostname = $ConfigFile.Settings.Hostname | |
} | |
D:\home\site\wwwroot\App_Data\tools\phantomjs\phantomjs --ssl-protocol=any --ignore-ssl-errors=yes D:\home\site\wwwroot\script\ee-warmup.js $settings.Hostname $settings.Username $settings.Password $settings.ExperienceEditorUrl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<Settings> | |
<Hostname>https://mywebsite-cm.azurewebsites.net</Hostname> | |
<ExperienceEditorUrl>?sc_mode=edit&sc_itemid=%7bB4423450-C432-485B-A709-64BA1168A9D4%7d&sc_version=10&sc_lang=en&sc_site=MySite</ExperienceEditorUrl> | |
<Username>EeWarmupUser</Username> | |
<Password>SuperSecretPW</Password> | |
</Settings> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment