Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CasperJS custom assertion example using resource requests
casper.onResourceRequestFor = function(requestRegexp, callback) {
var removeResourceListener = function () {
casper.removeListener('resource.requested', resourceListener);
};
var resourceListener = function(request) {
if (request.url.match(requestRegexp)) {
removeResourceListener();
callback();
}
};
this.on('resource.requested', resourceListener);
};
casper.assertResourceRequestAfterStep = function(urlRegexp, step) {
var testStatus = 'fail';
var passTest = function() {
casper.test.assert(true, "Request made for " + urlRegexp);
};
var failTest = function() {
casper.test.assert(false, "No request made for " + urlRegexp);
};
this.onResourceRequestFor(urlRegexp, function() {
testStatus = 'pass';
});
step();
casper.waitFor(function() { return testStatus == 'pass'; }, passTest, failTest);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment