Commit 5fcb2361 5fcb23612efeae87ce63ad5b87d9676f291d11e3 by Brandon Bethke Committed by Nicolas Perriault

added hook for webpage.onResourceError

1 parent 9c546d59
......@@ -176,6 +176,17 @@ Proxy method for PhantomJS' WebPage#onResourceRequested() callback, but the curr
.. index:: Step stack
``onResourceError``
-------------------------------------------------------------------------------
**Type:** ``Function``
**Default:** ``null``
Proxy method for PhantomJS' WebPage#onResourceError() callback, but the current Casper instance is passed as first argument.
.. index:: Step stack
``onStepComplete``
-------------------------------------------------------------------------------
......
......@@ -97,6 +97,7 @@ var Casper = function Casper(options) {
onPageInitialized: null,
onResourceReceived: null,
onResourceRequested: null,
onResourceError: null,
onRunComplete: function _onRunComplete() {
this.exit();
},
......@@ -2544,6 +2545,12 @@ function createPage(casper) {
casper.options.onResourceRequested.call(casper, casper, requestData, request);
}
};
page.onResourceError = function onResourceError(resourceError) {
casper.emit('resource.error', resourceError);
if (utils.isFunction(casper.options.onResourceError)) {
casper.options.onResourceError.call(casper, casper, resourceError);
}
};
page.onUrlChanged = function onUrlChanged(url) {
casper.log(f('url changed to "%s"', url), "debug");
casper.navigationRequested = false;
......
......@@ -50,6 +50,25 @@ casper.test.begin('onResourceRequested() & onResourceReceived() hook tests', 6,
});
});
casper.test.begin('onResourceError() hook tests', 3, function(test) {
var error = null;
casper.options.onResourceError = function(self, resourceError) {
error = resourceError;
};
casper.start('tests/site/non-existant.html', function() {
var expectedPath = '/tests/site/non-existant.html';
test.assert(error !== null, 'onResourceError() called with error information');
test.assert(error.errorCode === 203, 'onResourceError() error code is correct');
test.assert((error.url.indexOf(expectedPath, error.url.length - expectedPath.length) !== -1), 'onResourceError() url is correct');
})
casper.run(function() {
this.options.onResourceError = undefined;
test.done();
});
});
casper.test.begin('onAlert() hook tests', 1, function(test) {
var message;
casper.options.onAlert = function(self, msg) {
......