Commit 8723ef39 8723ef39e7ac89bdb4009cf20168ab26f97d6281 by Nicolas Perriault

closes #12 - added a new Casper 'httpStatusHandlers option object which can cont…

…ain dedicated HTTP status code handlers, one per code (404, 500, 403, etc.)'
1 parent 9e3916fc
...@@ -41,18 +41,19 @@ ...@@ -41,18 +41,19 @@
41 } 41 }
42 // default options 42 // default options
43 this.defaults = { 43 this.defaults = {
44 clientScripts: [], 44 clientScripts: [],
45 faultTolerant: true, 45 faultTolerant: true,
46 logLevel: "error", 46 logLevel: "error",
47 onDie: null, 47 httpStatusHandlers: {},
48 onError: null, 48 onDie: null,
49 onLoadError: null, 49 onError: null,
50 onPageInitialized: null, 50 onLoadError: null,
51 onTimeout: null, 51 onPageInitialized: null,
52 page: null, 52 onTimeout: null,
53 pageSettings: { userAgent: DEFAULT_USER_AGENT }, 53 page: null,
54 timeout: null, 54 pageSettings: { userAgent: DEFAULT_USER_AGENT },
55 verbose: false 55 timeout: null,
56 verbose: false
56 }; 57 };
57 // privates 58 // privates
58 // local properties 59 // local properties
...@@ -1664,8 +1665,11 @@ ...@@ -1664,8 +1665,11 @@
1664 casper.loadInProgress = false; 1665 casper.loadInProgress = false;
1665 }; 1666 };
1666 page.onResourceReceived = function(resource) { 1667 page.onResourceReceived = function(resource) {
1667 if (resource.url === casper.requestUrl) { 1668 if (resource.url === casper.requestUrl && resource.stage === "start") {
1668 casper.currentHTTPStatus = resource.status; 1669 casper.currentHTTPStatus = resource.status;
1670 if (isType(casper.options.httpStatusHandlers, "object") && resource.status in casper.options.httpStatusHandlers) {
1671 casper.options.httpStatusHandlers[resource.status](casper, resource);
1672 }
1669 casper.currentUrl = resource.url; 1673 casper.currentUrl = resource.url;
1670 } 1674 }
1671 }; 1675 };
......
1 phantom.injectJs('casper.js');
2
3 var links = [];
4 var casper = new phantom.Casper({
5 httpStatusHandlers: {
6 404: function(self, resource) {
7 self.echo('Resource at ' + resource.url + ' not found (404)');
8 }
9 },
10 verbose: true
11 });
12
13 casper.start('http://www.google.com/plop', function(self) {
14 self.echo('Done.').exit();
15 });
16 casper.run();
...\ No newline at end of file ...\ No newline at end of file