Commit 061a8f8a 061a8f8a9b373d0d31552059477bc75cb401e1de by Nicolas Perriault

closes #13 - added new stepTimeout and onStepTimeout options to Casper

1 parent 80f7d479
......@@ -49,9 +49,11 @@
onError: null,
onLoadError: null,
onPageInitialized: null,
onStepTimeout: null,
onTimeout: null,
page: null,
pageSettings: { userAgent: DEFAULT_USER_AGENT },
stepTimeout: null,
timeout: null,
verbose: false
};
......@@ -187,23 +189,7 @@
checkStep: function(self, onComplete) {
var step = self.steps[self.step];
if (!self.loadInProgress && isType(step, "function")) {
var curStepNum = self.step + 1;
var stepInfo = "Step " + curStepNum + "/" + self.steps.length + ": ";
self.log(stepInfo + self.page.evaluate(function() {
return document.location.href;
}) + ' (HTTP ' + self.currentHTTPStatus + ')', "info");
try {
step(self);
} catch (e) {
if (self.options.faultTolerant) {
self.log("Step error: " + e, "error");
} else {
throw e;
}
}
var time = new Date().getTime() - self.startTime;
self.log(stepInfo + "done in " + time + "ms.", "info");
self.step++;
self.runStep(step);
}
if (!isType(step, "function") && !self.delayedExecution) {
self.result.time = new Date().getTime() - self.startTime;
......@@ -564,7 +550,8 @@
* @param String location The url to open
* @return Casper
*/
open: function(location) {
open: function(location, options) {
options = isType(options, "object") ? options : {};
this.requestUrl = location;
this.page.open(location);
return this;
......@@ -603,6 +590,46 @@
},
/**
* Runs a step.
*
* @param Function step
*/
runStep: function(step) {
var skipLog = isType(step.options, "object") && step.options.skipLog === true;
var stepInfo = "Step " + (this.step + 1) + "/" + this.steps.length;
if (!skipLog) {
this.log(stepInfo + ' ' + this.getCurrentUrl() + ' (HTTP ' + this.currentHTTPStatus + ')', "info");
}
if (isType(this.options.stepTimeout, "number") && this.options.stepTimeout > 0) {
var stepTimeoutCheckInterval = setInterval(function(self, start, stepNum) {
if (new Date().getTime() - start > self.options.stepTimeout) {
if (self.step == stepNum + 1) {
if (isType(self.options.onStepTimeout, "function")) {
self.options.onStepTimeout(self);
} else {
self.die("Maximum step execution timeout exceeded for step " + stepNum, "error");
}
}
clearInterval(stepTimeoutCheckInterval);
}
}, this.options.stepTimeout, this, new Date().getTime(), this.step);
}
try {
step(this);
} catch (e) {
if (this.options.faultTolerant) {
this.log("Step error: " + e, "error");
} else {
throw e;
}
}
if (!skipLog) {
this.log(stepInfo + ": done in " + (new Date().getTime() - this.startTime) + "ms.", "info");
}
this.step++;
},
/**
* Configures and starts Casper.
*
* @param String location An optional location to open on start
......@@ -721,9 +748,13 @@
* @see Casper#open
*/
thenOpen: function(location, then) {
this.then(function(self) {
var step = function(self) {
self.open(location);
});
};
step.options = {
skipLog: true
};
this.then(step);
return isType(then, "function") ? this.then(then) : this;
},
......
phantom.injectJs('casper.js');
if (phantom.args.length === 0) {
console.log('You must provide a timeout value')
phantom.exit(1);
} else {
var timeout = Number(phantom.args[0], 10);
if (timeout < 1) {
console.log('A timeout value must be a positive integer')
phantom.exit(1);
}
}
var casper = new phantom.Casper({
// verbose: true,
// logLevel: "debug",
stepTimeout: timeout,
onStepTimeout: function(self) {
self.echo(self.requestUrl + ' failed to load in less than ' + timeout + 'ms', 'ERROR');
}
});
var links = [
'http://google.com/',
'http://lemonde.fr/',
'http://liberation.fr/',
'http://cdiscount.fr/'
];
casper.start();
casper.each(links, function(self, link, i) {
self.test.comment('Adding ' + link + ' to test suite');
self.thenOpen(link, function(self) {
self.echo(self.requestUrl + ' loaded');
});
});
casper.run(function(self) {
self.test.renderResults(true);
self.exit();
});
\ No newline at end of file
/**
* Just a silly game.
*
* $ phantomjs samples/timeout.js 500
* Will google.com load in less than 500ms?
* NOPE.
* $ phantomjs samples/timeout.js 1000
* Will google.com load in less than 1000ms?
* NOPE.
* $ phantomjs samples/timeout.js 1500
* Will google.com load in less than 1500ms?
* NOPE.
* $ phantomjs samples/timeout.js 2000
* Will google.com load in less than 2000ms?
* YES!
*/
phantom.injectJs('casper.js');
if (phantom.args.length === 0) {
console.log('You must provide a timeout value')
phantom.exit(1);
} else {
var timeout = Number(phantom.args[0], 10);
if (timeout < 1) {
console.log('A timeout value must be a positive integer')
phantom.exit(1);
}
}
var casper = new phantom.Casper({
logLevel: "debug",
verbose: true,
timeout: 2000,
timeout: timeout,
onTimeout: function(self) {
self.die('script execution timeout exceeded');
self.echo('NOPE.', 'RED_BAR').exit();
}
});
casper.echo('Will google.com load in less than ' + timeout + 'ms?');
casper.start('http://google.com/', function(self) {
self.log('google is loaded');
self.echo('YES!', 'GREEN_BAR').exit();
});
casper.run(function(self) {
self.log('oops, forgot to call Casper.exit()');
});
\ No newline at end of file
casper.run();
\ No newline at end of file
......