Last active
August 29, 2015 14:26
-
-
Save FabienArcellier/0f7fe8aa859195772cc4 to your computer and use it in GitHub Desktop.
This file contains 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
// Check loading of Single Page Application | |
// | |
// It checks the count of requests equals the count of response | |
// of loading of SPA | |
var page = require('webpage').create(); | |
var system = require('system'); | |
var lastReceived = new Date().getTime(); | |
var requestCount = 0; | |
var responseCount = 0; | |
var requestIds = []; | |
var status = 'OK' | |
page.onResourceReceived = function (response) { | |
if (response.status >= 400) { | |
status = 'KO' | |
} | |
if(requestIds.indexOf(response.id) !== -1) { | |
lastReceived = new Date().getTime(); | |
responseCount++; | |
requestIds[requestIds.indexOf(response.id)] = null; | |
} | |
}; | |
page.onResourceRequested = function (request) { | |
if(requestIds.indexOf(request.id) === -1) { | |
requestIds.push(request.id); | |
requestCount++; | |
} | |
}; | |
if (system.args.length < 2) { | |
console.log("Usage : phantomjs " +system.args[0] + " url [timeout]") | |
console.log(" url : http://www.google.fr") | |
console.log(" timeout : wait x ms after the last request") | |
phantom.exit() | |
} | |
var url = system.args[1]; | |
var timeout = system.args.length > 2 ? system.args[2] : 1000; // Default value 1s | |
// Open the page | |
page.open(url, function () {}); | |
var checkComplete = function () { | |
if(status == 'KO') { | |
console.log('[' + date.toISOString() + '] - Fail to load application : Bad status code'); | |
phantom.exit(1); | |
} | |
if(new Date().getTime() - lastReceived > timeout) { | |
clearInterval(checkCompleteInterval); | |
date = new Date() | |
if(requestCount !== responseCount) { | |
console.log('[' + date.toISOString() + '] - Fail to load application : Timeout'); | |
phantom.exit(1); | |
} else { | |
console.log('[' + date.toISOString() + '] - Success'); | |
phantom.exit(); | |
} | |
} | |
} | |
// Let us check to see if the page is finished rendering | |
var checkCompleteInterval = setInterval(checkComplete, 1); | |
if ( !Date.prototype.toISOString ) { | |
( function() { | |
function pad(number) { | |
var r = String(number); | |
if ( r.length === 1 ) { | |
r = '0' + r; | |
} | |
return r; | |
} | |
Date.prototype.toISOString = function() { | |
return this.getUTCFullYear() | |
+ '-' + pad( this.getUTCMonth() + 1 ) | |
+ '-' + pad( this.getUTCDate() ) | |
+ 'T' + pad( this.getUTCHours() ) | |
+ ':' + pad( this.getUTCMinutes() ) | |
+ ':' + pad( this.getUTCSeconds() ) | |
+ '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 ) | |
+ 'Z'; | |
}; | |
}() ); | |
} |
This file contains 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
#!/bin/bash | |
phantomjs check_spa.js http://www.google.fr | |
phantomjs check_spa.js http://www.google.fr 5000 # Wait 5 seconds after the last response | |
while true | |
do | |
phantomjs check_spa.js http://www.google.fr 5000 | |
sleep 60 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment