Commit 9e3916fc 9e3916fc4562b22495ce02394cf6b372346c58d7 by Nicolas Perriault

added new 'onTimeout' option to Casper for setting a callback when script execut…

…ion timeout is reached; also, a script will now die() on timeout by default
1 parent 885122db
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
48 onError: null, 48 onError: null,
49 onLoadError: null, 49 onLoadError: null,
50 onPageInitialized: null, 50 onPageInitialized: null,
51 onTimeout: null,
51 page: null, 52 page: null,
52 pageSettings: { userAgent: DEFAULT_USER_AGENT }, 53 pageSettings: { userAgent: DEFAULT_USER_AGENT },
53 timeout: null, 54 timeout: null,
...@@ -637,9 +638,13 @@ ...@@ -637,9 +638,13 @@
637 } 638 }
638 this.started = true; 639 this.started = true;
639 if (isType(this.options.timeout, "number") && this.options.timeout > 0) { 640 if (isType(this.options.timeout, "number") && this.options.timeout > 0) {
640 this.log("execution timeout set to " + this.options.timeout + 'ms', "info"); 641 this.log("Execution timeout set to " + this.options.timeout + 'ms', "info");
641 setTimeout(function(self) { 642 setTimeout(function(self) {
642 self.log("timeout of " + self.options.timeout + "ms exceeded", "info").exit(); 643 if (isType(self.options.onTimeout, "function")) {
644 self.options.onTimeout(self);
645 } else {
646 self.die("Timeout of " + self.options.timeout + "ms exceeded, exiting.");
647 }
643 }, this.options.timeout, this); 648 }, this.options.timeout, this);
644 } 649 }
645 if (isType(this.options.onPageInitialized, "function")) { 650 if (isType(this.options.onPageInitialized, "function")) {
......
1 phantom.injectJs('casper.js');
2
3 var casper = new phantom.Casper({
4 logLevel: "debug",
5 verbose: true,
6 timeout: 2000,
7 onTimeout: function(self) {
8 self.die('script execution timeout exceeded');
9 }
10 });
11
12 casper.start('http://google.com/', function(self) {
13 self.log('google is loaded');
14 });
15
16 casper.run(function(self) {
17 self.log('oops, forgot to call Casper.exit()');
18 });
...\ No newline at end of file ...\ No newline at end of file