Commit 86217d8d 86217d8da1eaeeb7c19277a3b5c34295b9e73d44 by Nicolas Perriault

added an 'onError' Casper setting

1 parent 5b590280
...@@ -222,13 +222,17 @@ phantom.injectJs('path/to/casper.js'); ...@@ -222,13 +222,17 @@ phantom.injectJs('path/to/casper.js');
222 222
223 var casper = new phantom.Casper({ 223 var casper = new phantom.Casper({
224 clientScripts: [ 224 clientScripts: [
225 'includes/jquery.js', 225 'includes/jquery.js', // These two scripts will be injected in remote
226 'includes/underscore.js' 226 'includes/underscore.js' // DOM on every request
227 ], 227 ],
228 logLevel: "info", 228 logLevel: "info", // Only "info" level messages will be logged
229 onError: function(self, m) { // Any "error" level message will be written
230 console.log('FATAL:' + m); // on the console output and PhantomJS will
231 self.exit(); // terminate
232 },
229 pageSettings: { 233 pageSettings: {
230 loadImages: false, 234 loadImages: false, // The WebPage instance used by Casper will
231 loadPlugins: false 235 loadPlugins: false // use these settings
232 } 236 }
233 }); 237 });
234 ``` 238 ```
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
31 * clientScripts | Array | [] | A collection of script filepaths to include to every page loaded 31 * clientScripts | Array | [] | A collection of script filepaths to include to every page loaded
32 * logLevel | String | "error" | Logging level (see logLevels for available values) 32 * logLevel | String | "error" | Logging level (see logLevels for available values)
33 * onDie | function | null | A function to be called when Casper#die() is called 33 * onDie | function | null | A function to be called when Casper#die() is called
34 * onError | function | null | A function to be called when an "error" level event occurs
34 * onPageInitialized | function | null | A function to be called after WebPage instance has been initialized 35 * onPageInitialized | function | null | A function to be called after WebPage instance has been initialized
35 * page | WebPage | null | An existing WebPage instance 36 * page | WebPage | null | An existing WebPage instance
36 * pageSettings | Object | {} | PhantomJS's WebPage settings object 37 * pageSettings | Object | {} | PhantomJS's WebPage settings object
...@@ -51,6 +52,7 @@ ...@@ -51,6 +52,7 @@
51 clientScripts: [], 52 clientScripts: [],
52 logLevel: "error", 53 logLevel: "error",
53 onDie: null, 54 onDie: null,
55 onError: null,
54 onPageInitialized: null, 56 onPageInitialized: null,
55 page: null, 57 page: null,
56 pageSettings: { userAgent: DEFAULT_USER_AGENT }, 58 pageSettings: { userAgent: DEFAULT_USER_AGENT },
...@@ -286,6 +288,9 @@ ...@@ -286,6 +288,9 @@
286 log: function(message, level, space) { 288 log: function(message, level, space) {
287 level = level && this.logLevels.indexOf(level) > -1 ? level : "debug"; 289 level = level && this.logLevels.indexOf(level) > -1 ? level : "debug";
288 space = space ? space : "phantom"; 290 space = space ? space : "phantom";
291 if (level === "error" && typeof(this.options.onError) === "function") {
292 this.options.onError(this, message, space);
293 }
289 if (this.logLevels.indexOf(level) < this.logLevels.indexOf(this.options.logLevel)) { 294 if (this.logLevels.indexOf(level) < this.logLevels.indexOf(this.options.logLevel)) {
290 return this; // skip logging 295 return this; // skip logging
291 } 296 }
......