Commit 661c77ec 661c77ecc55453fbe8ef823d82d802b74bc52244 by Nicolas Perriault

fixes #383, #369: waiting process not stopped on timeout reached

only in test mode.
1 parent 8c7d533f
......@@ -1249,6 +1249,7 @@ Casper.prototype.reload = function reload(then) {
if (utils.isFunction(then)) {
this.then(this.createStep(then));
}
return this;
};
/**
......@@ -1758,6 +1759,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
var start = new Date().getTime();
var condition = false;
var interval = setInterval(function _check(self, testFx, timeout, onTimeout) {
/*jshint maxstatements:20*/
if ((new Date().getTime() - start < timeout) && !condition) {
condition = testFx.call(self, self);
return;
......@@ -1767,17 +1769,17 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
self.log("Casper.waitFor() timeout", "warning");
var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout;
self.emit('waitFor.timeout', timeout, onWaitTimeout);
clearInterval(interval); // refs #383
if (!utils.isFunction(onWaitTimeout)) {
throw new CasperError('Invalid timeout function, exiting.');
throw new CasperError('Invalid timeout function');
}
return onWaitTimeout.call(self, timeout);
}
onWaitTimeout.call(self, timeout);
} else {
self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info");
clearInterval(interval);
if (then) {
self.then(then);
}
}
clearInterval(interval);
}, 100, this, testFx, timeout, onTimeout);
});
};
......
......@@ -143,8 +143,16 @@ var Tester = function Tester(casper, options) {
// casper events
this.casper.on('error', function onCasperError(msg, backtrace) {
this.test.fail(msg, {
type: 'error',
var type = 'error', message = msg, match = /^(\w+)Error: (.*)/.exec(msg);
if (match) {
type = match[1].toLowerCase();
message = match[2];
}
if (type !== 'assertion') {
return this.test.uncaughtError(msg, this.currentTestFile, null, backtrace);
}
this.test.fail(message, {
type: type,
doThrow: false,
values: {
stack: backtrace
......@@ -1173,8 +1181,11 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
style = 'GREEN_BAR';
}
result = f('%s %s tests executed in %ss, %d passed, %d failed.',
statusText, total, utils.ms2seconds(this.suiteResults.calculateDuration()),
passed, failed);
statusText,
total,
utils.ms2seconds(this.suiteResults.calculateDuration()),
passed,
failed);
}
this.casper.echo(result, style, this.options.pad);
if (failed > 0) {
......@@ -1437,7 +1448,7 @@ TestSuiteResult.prototype.getAllResults = function getAllResults() {
TestSuiteResult.prototype.calculateDuration = function calculateDuration() {
"use strict";
return this.getAllResults().map(function(result) {
return result.time;
return ~~result.time;
}).reduce(function add(a, b) {
return a + b;
}, 0);
......
/*global casper*/
/*jshint strict:false*/
casper.test.begin('wait*() tests', 4, function(test) {
casper.test.begin('wait() tests', 1, function(test) {
var waitStart;
casper.start('tests/site/index.html', function() {
......@@ -12,8 +12,15 @@ casper.test.begin('wait*() tests', 4, function(test) {
'Casper.wait() can wait for a given amount of time');
});
casper.thenOpen('tests/site/waitFor.html', function() {
this.waitFor(function() {
casper.run(function() {
test.done();
});
});
casper.test.begin('waitFor() tests', 2, function(test) {
casper.start('tests/site/waitFor.html');
casper.waitFor(function() {
return this.evaluate(function() {
return document.querySelectorAll('li').length === 4;
});
......@@ -22,15 +29,64 @@ casper.test.begin('wait*() tests', 4, function(test) {
}, function() {
test.fail('Casper.waitFor() can wait for something to happen');
});
casper.reload().waitFor(function(){
return false;
}, function() {
test.fail('waitFor() processes onTimeout callback');
}, function() {
test.pass('waitFor() processes onTimeout callback');
}, 1000);
casper.run(function() {
test.done();
});
});
casper.test.begin('waitForResource() tests', 2, function(test) {
casper.start('tests/site/waitFor.html');
casper.waitForResource('phantom.png', function() {
test.pass('Casper.waitForResource() waits for a resource');
}, function() {
test.fail('Casper.waitForResource() waits for a resource');
});
casper.reload().waitForResource(/phantom\.png$/, function() {
test.pass('Casper.waitForResource() waits for a resource using RegExp');
}, function() {
test.fail('Casper.waitForResource() waits for a resource using RegExp');
});
casper.run(function() {
test.done();
});
});
casper.test.begin('waitForSelector() tests', 1, function(test) {
casper.start('tests/site/waitFor.html');
casper.waitForSelector('li:nth-child(4)', function() {
test.pass('Casper.waitForSelector() waits for a selector to exist');
}, function() {
test.fail('Casper.waitForSelector() waits for a selector to exist');
});
casper.run(function() {
test.done();
});
});
casper.test.begin('waitForText() tests', 2, function(test) {
casper.start('tests/site/waitFor.html');
casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function() {
casper.waitForText('<li>four</li>', function() {
test.pass('Casper.waitForText() can wait for text');
}, function() {
test.fail('Casper.waitForText() can wait for text');
});
casper.thenOpen('tests/site/waitFor.html').waitForText(/four/i, function() {
casper.reload().waitForText(/four/i, function() {
this.test.pass('Casper.waitForText() can wait for regexp');
}, function() {
this.test.fail('Casper.waitForText() can wait for regexp');
......@@ -40,3 +96,17 @@ casper.test.begin('wait*() tests', 4, function(test) {
test.done();
});
});
casper.test.begin('waitUntilVisible() tests', 1, function(test) {
casper.start('tests/site/waitFor.html');
casper.waitUntilVisible('li:nth-child(4)', function() {
test.pass('Casper.waitUntilVisible() waits for a selector being visible');
}, function() {
test.fail('Casper.waitUntilVisible() waits for a selector being visible');
});
casper.run(function() {
test.done();
});
});
......