added hook for webpage.onResourceError
Showing
3 changed files
with
37 additions
and
0 deletions
... | @@ -176,6 +176,17 @@ Proxy method for PhantomJS' WebPage#onResourceRequested() callback, but the curr | ... | @@ -176,6 +176,17 @@ Proxy method for PhantomJS' WebPage#onResourceRequested() callback, but the curr |
176 | 176 | ||
177 | .. index:: Step stack | 177 | .. index:: Step stack |
178 | 178 | ||
179 | ``onResourceError`` | ||
180 | ------------------------------------------------------------------------------- | ||
181 | |||
182 | **Type:** ``Function`` | ||
183 | |||
184 | **Default:** ``null`` | ||
185 | |||
186 | Proxy method for PhantomJS' WebPage#onResourceError() callback, but the current Casper instance is passed as first argument. | ||
187 | |||
188 | .. index:: Step stack | ||
189 | |||
179 | ``onStepComplete`` | 190 | ``onStepComplete`` |
180 | ------------------------------------------------------------------------------- | 191 | ------------------------------------------------------------------------------- |
181 | 192 | ... | ... |
... | @@ -97,6 +97,7 @@ var Casper = function Casper(options) { | ... | @@ -97,6 +97,7 @@ var Casper = function Casper(options) { |
97 | onPageInitialized: null, | 97 | onPageInitialized: null, |
98 | onResourceReceived: null, | 98 | onResourceReceived: null, |
99 | onResourceRequested: null, | 99 | onResourceRequested: null, |
100 | onResourceError: null, | ||
100 | onRunComplete: function _onRunComplete() { | 101 | onRunComplete: function _onRunComplete() { |
101 | this.exit(); | 102 | this.exit(); |
102 | }, | 103 | }, |
... | @@ -2544,6 +2545,12 @@ function createPage(casper) { | ... | @@ -2544,6 +2545,12 @@ function createPage(casper) { |
2544 | casper.options.onResourceRequested.call(casper, casper, requestData, request); | 2545 | casper.options.onResourceRequested.call(casper, casper, requestData, request); |
2545 | } | 2546 | } |
2546 | }; | 2547 | }; |
2548 | page.onResourceError = function onResourceError(resourceError) { | ||
2549 | casper.emit('resource.error', resourceError); | ||
2550 | if (utils.isFunction(casper.options.onResourceError)) { | ||
2551 | casper.options.onResourceError.call(casper, casper, resourceError); | ||
2552 | } | ||
2553 | }; | ||
2547 | page.onUrlChanged = function onUrlChanged(url) { | 2554 | page.onUrlChanged = function onUrlChanged(url) { |
2548 | casper.log(f('url changed to "%s"', url), "debug"); | 2555 | casper.log(f('url changed to "%s"', url), "debug"); |
2549 | casper.navigationRequested = false; | 2556 | casper.navigationRequested = false; | ... | ... |
... | @@ -50,6 +50,25 @@ casper.test.begin('onResourceRequested() & onResourceReceived() hook tests', 6, | ... | @@ -50,6 +50,25 @@ casper.test.begin('onResourceRequested() & onResourceReceived() hook tests', 6, |
50 | }); | 50 | }); |
51 | }); | 51 | }); |
52 | 52 | ||
53 | casper.test.begin('onResourceError() hook tests', 3, function(test) { | ||
54 | var error = null; | ||
55 | casper.options.onResourceError = function(self, resourceError) { | ||
56 | error = resourceError; | ||
57 | }; | ||
58 | |||
59 | casper.start('tests/site/non-existant.html', function() { | ||
60 | var expectedPath = '/tests/site/non-existant.html'; | ||
61 | test.assert(error !== null, 'onResourceError() called with error information'); | ||
62 | test.assert(error.errorCode === 203, 'onResourceError() error code is correct'); | ||
63 | test.assert((error.url.indexOf(expectedPath, error.url.length - expectedPath.length) !== -1), 'onResourceError() url is correct'); | ||
64 | }) | ||
65 | |||
66 | casper.run(function() { | ||
67 | this.options.onResourceError = undefined; | ||
68 | test.done(); | ||
69 | }); | ||
70 | }); | ||
71 | |||
53 | casper.test.begin('onAlert() hook tests', 1, function(test) { | 72 | casper.test.begin('onAlert() hook tests', 1, function(test) { |
54 | var message; | 73 | var message; |
55 | casper.options.onAlert = function(self, msg) { | 74 | casper.options.onAlert = function(self, msg) { | ... | ... |
-
Please register or sign in to post a comment