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) { ...@@ -1249,6 +1249,7 @@ Casper.prototype.reload = function reload(then) {
1249 if (utils.isFunction(then)) { 1249 if (utils.isFunction(then)) {
1250 this.then(this.createStep(then)); 1250 this.then(this.createStep(then));
1251 } 1251 }
1252 return this;
1252 }; 1253 };
1253 1254
1254 /** 1255 /**
...@@ -1758,6 +1759,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -1758,6 +1759,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
1758 var start = new Date().getTime(); 1759 var start = new Date().getTime();
1759 var condition = false; 1760 var condition = false;
1760 var interval = setInterval(function _check(self, testFx, timeout, onTimeout) { 1761 var interval = setInterval(function _check(self, testFx, timeout, onTimeout) {
1762 /*jshint maxstatements:20*/
1761 if ((new Date().getTime() - start < timeout) && !condition) { 1763 if ((new Date().getTime() - start < timeout) && !condition) {
1762 condition = testFx.call(self, self); 1764 condition = testFx.call(self, self);
1763 return; 1765 return;
...@@ -1767,17 +1769,17 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -1767,17 +1769,17 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
1767 self.log("Casper.waitFor() timeout", "warning"); 1769 self.log("Casper.waitFor() timeout", "warning");
1768 var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout; 1770 var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout;
1769 self.emit('waitFor.timeout', timeout, onWaitTimeout); 1771 self.emit('waitFor.timeout', timeout, onWaitTimeout);
1772 clearInterval(interval); // refs #383
1770 if (!utils.isFunction(onWaitTimeout)) { 1773 if (!utils.isFunction(onWaitTimeout)) {
1771 throw new CasperError('Invalid timeout function, exiting.'); 1774 throw new CasperError('Invalid timeout function');
1772 }
1773 onWaitTimeout.call(self, timeout);
1774 } else {
1775 self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info");
1776 if (then) {
1777 self.then(then);
1778 } 1775 }
1776 return onWaitTimeout.call(self, timeout);
1779 } 1777 }
1778 self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info");
1780 clearInterval(interval); 1779 clearInterval(interval);
1780 if (then) {
1781 self.then(then);
1782 }
1781 }, 100, this, testFx, timeout, onTimeout); 1783 }, 100, this, testFx, timeout, onTimeout);
1782 }); 1784 });
1783 }; 1785 };
......
...@@ -143,8 +143,16 @@ var Tester = function Tester(casper, options) { ...@@ -143,8 +143,16 @@ var Tester = function Tester(casper, options) {
143 143
144 // casper events 144 // casper events
145 this.casper.on('error', function onCasperError(msg, backtrace) { 145 this.casper.on('error', function onCasperError(msg, backtrace) {
146 this.test.fail(msg, { 146 var type = 'error', message = msg, match = /^(\w+)Error: (.*)/.exec(msg);
147 type: 'error', 147 if (match) {
148 type = match[1].toLowerCase();
149 message = match[2];
150 }
151 if (type !== 'assertion') {
152 return this.test.uncaughtError(msg, this.currentTestFile, null, backtrace);
153 }
154 this.test.fail(message, {
155 type: type,
148 doThrow: false, 156 doThrow: false,
149 values: { 157 values: {
150 stack: backtrace 158 stack: backtrace
...@@ -1173,8 +1181,11 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) { ...@@ -1173,8 +1181,11 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
1173 style = 'GREEN_BAR'; 1181 style = 'GREEN_BAR';
1174 } 1182 }
1175 result = f('%s %s tests executed in %ss, %d passed, %d failed.', 1183 result = f('%s %s tests executed in %ss, %d passed, %d failed.',
1176 statusText, total, utils.ms2seconds(this.suiteResults.calculateDuration()), 1184 statusText,
1177 passed, failed); 1185 total,
1186 utils.ms2seconds(this.suiteResults.calculateDuration()),
1187 passed,
1188 failed);
1178 } 1189 }
1179 this.casper.echo(result, style, this.options.pad); 1190 this.casper.echo(result, style, this.options.pad);
1180 if (failed > 0) { 1191 if (failed > 0) {
...@@ -1437,7 +1448,7 @@ TestSuiteResult.prototype.getAllResults = function getAllResults() { ...@@ -1437,7 +1448,7 @@ TestSuiteResult.prototype.getAllResults = function getAllResults() {
1437 TestSuiteResult.prototype.calculateDuration = function calculateDuration() { 1448 TestSuiteResult.prototype.calculateDuration = function calculateDuration() {
1438 "use strict"; 1449 "use strict";
1439 return this.getAllResults().map(function(result) { 1450 return this.getAllResults().map(function(result) {
1440 return result.time; 1451 return ~~result.time;
1441 }).reduce(function add(a, b) { 1452 }).reduce(function add(a, b) {
1442 return a + b; 1453 return a + b;
1443 }, 0); 1454 }, 0);
......
1 /*global casper*/ 1 /*global casper*/
2 /*jshint strict:false*/ 2 /*jshint strict:false*/
3 casper.test.begin('wait*() tests', 4, function(test) { 3 casper.test.begin('wait() tests', 1, function(test) {
4 var waitStart; 4 var waitStart;
5 5
6 casper.start('tests/site/index.html', function() { 6 casper.start('tests/site/index.html', function() {
...@@ -12,25 +12,81 @@ casper.test.begin('wait*() tests', 4, function(test) { ...@@ -12,25 +12,81 @@ casper.test.begin('wait*() tests', 4, function(test) {
12 'Casper.wait() can wait for a given amount of time'); 12 'Casper.wait() can wait for a given amount of time');
13 }); 13 });
14 14
15 casper.thenOpen('tests/site/waitFor.html', function() { 15 casper.run(function() {
16 this.waitFor(function() { 16 test.done();
17 return this.evaluate(function() { 17 });
18 return document.querySelectorAll('li').length === 4; 18 });
19 }); 19
20 }, function() { 20 casper.test.begin('waitFor() tests', 2, function(test) {
21 test.pass('Casper.waitFor() can wait for something to happen'); 21 casper.start('tests/site/waitFor.html');
22 }, function() { 22
23 test.fail('Casper.waitFor() can wait for something to happen'); 23 casper.waitFor(function() {
24 return this.evaluate(function() {
25 return document.querySelectorAll('li').length === 4;
24 }); 26 });
27 }, function() {
28 test.pass('Casper.waitFor() can wait for something to happen');
29 }, function() {
30 test.fail('Casper.waitFor() can wait for something to happen');
31 });
32
33 casper.reload().waitFor(function(){
34 return false;
35 }, function() {
36 test.fail('waitFor() processes onTimeout callback');
37 }, function() {
38 test.pass('waitFor() processes onTimeout callback');
39 }, 1000);
40
41 casper.run(function() {
42 test.done();
43 });
44 });
45
46 casper.test.begin('waitForResource() tests', 2, function(test) {
47 casper.start('tests/site/waitFor.html');
48
49 casper.waitForResource('phantom.png', function() {
50 test.pass('Casper.waitForResource() waits for a resource');
51 }, function() {
52 test.fail('Casper.waitForResource() waits for a resource');
53 });
54
55 casper.reload().waitForResource(/phantom\.png$/, function() {
56 test.pass('Casper.waitForResource() waits for a resource using RegExp');
57 }, function() {
58 test.fail('Casper.waitForResource() waits for a resource using RegExp');
25 }); 59 });
26 60
27 casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function() { 61 casper.run(function() {
62 test.done();
63 });
64 });
65
66 casper.test.begin('waitForSelector() tests', 1, function(test) {
67 casper.start('tests/site/waitFor.html');
68
69 casper.waitForSelector('li:nth-child(4)', function() {
70 test.pass('Casper.waitForSelector() waits for a selector to exist');
71 }, function() {
72 test.fail('Casper.waitForSelector() waits for a selector to exist');
73 });
74
75 casper.run(function() {
76 test.done();
77 });
78 });
79
80 casper.test.begin('waitForText() tests', 2, function(test) {
81 casper.start('tests/site/waitFor.html');
82
83 casper.waitForText('<li>four</li>', function() {
28 test.pass('Casper.waitForText() can wait for text'); 84 test.pass('Casper.waitForText() can wait for text');
29 }, function() { 85 }, function() {
30 test.fail('Casper.waitForText() can wait for text'); 86 test.fail('Casper.waitForText() can wait for text');
31 }); 87 });
32 88
33 casper.thenOpen('tests/site/waitFor.html').waitForText(/four/i, function() { 89 casper.reload().waitForText(/four/i, function() {
34 this.test.pass('Casper.waitForText() can wait for regexp'); 90 this.test.pass('Casper.waitForText() can wait for regexp');
35 }, function() { 91 }, function() {
36 this.test.fail('Casper.waitForText() can wait for regexp'); 92 this.test.fail('Casper.waitForText() can wait for regexp');
...@@ -40,3 +96,17 @@ casper.test.begin('wait*() tests', 4, function(test) { ...@@ -40,3 +96,17 @@ casper.test.begin('wait*() tests', 4, function(test) {
40 test.done(); 96 test.done();
41 }); 97 });
42 }); 98 });
99
100 casper.test.begin('waitUntilVisible() tests', 1, function(test) {
101 casper.start('tests/site/waitFor.html');
102
103 casper.waitUntilVisible('li:nth-child(4)', function() {
104 test.pass('Casper.waitUntilVisible() waits for a selector being visible');
105 }, function() {
106 test.fail('Casper.waitUntilVisible() waits for a selector being visible');
107 });
108
109 casper.run(function() {
110 test.done();
111 });
112 });
......