Commit 5bd2a438 5bd2a4386ea8774cf7d112d25b9cb4e6fb761163 by Nicolas Perriault

added Casper#unwait()

Useful to abort all running waitFor() processes in a single operation.
1 parent 1a50e23d
...@@ -158,6 +158,7 @@ var Casper = function Casper(options) { ...@@ -158,6 +158,7 @@ var Casper = function Casper(options) {
158 this.started = false; 158 this.started = false;
159 this.step = -1; 159 this.step = -1;
160 this.steps = []; 160 this.steps = [];
161 this.waiters = [];
161 this._test = undefined; 162 this._test = undefined;
162 this.__defineGetter__('test', function() { 163 this.__defineGetter__('test', function() {
163 if (!phantom.casperTest) { 164 if (!phantom.casperTest) {
...@@ -1697,6 +1698,22 @@ Casper.prototype.toString = function toString() { ...@@ -1697,6 +1698,22 @@ Casper.prototype.toString = function toString() {
1697 }; 1698 };
1698 1699
1699 /** 1700 /**
1701 * Clear all wait processes.
1702 *
1703 * @return Casper
1704 */
1705 Casper.prototype.unwait = function unwait() {
1706 "use strict";
1707 this.waiters.forEach(function(interval) {
1708 if (interval) {
1709 clearInterval(interval);
1710 }
1711 });
1712 this.waiters = [];
1713 return this;
1714 };
1715
1716 /**
1700 * Sets the user-agent string currently used when requesting urls. 1717 * Sets the user-agent string currently used when requesting urls.
1701 * 1718 *
1702 * @param String userAgent User agent string 1719 * @param String userAgent User agent string
...@@ -1845,7 +1862,13 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -1845,7 +1862,13 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
1845 if (!utils.isFunction(onWaitTimeout)) { 1862 if (!utils.isFunction(onWaitTimeout)) {
1846 throw new CasperError('Invalid timeout function'); 1863 throw new CasperError('Invalid timeout function');
1847 } 1864 }
1848 return onWaitTimeout.call(self, timeout); 1865 try {
1866 return onWaitTimeout.call(self, timeout);
1867 } catch (error) {
1868 self.emit('waitFor.timeout.error', error);
1869 } finally {
1870 return;
1871 }
1849 } 1872 }
1850 self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info"); 1873 self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info");
1851 clearInterval(interval); 1874 clearInterval(interval);
...@@ -1853,6 +1876,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) { ...@@ -1853,6 +1876,7 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
1853 self.then(then); 1876 self.then(then);
1854 } 1877 }
1855 }, 100, this, testFx, timeout, onTimeout); 1878 }, 100, this, testFx, timeout, onTimeout);
1879 this.waiters.push(interval);
1856 }); 1880 });
1857 }; 1881 };
1858 1882
......
...@@ -172,14 +172,13 @@ var Tester = function Tester(casper, options) { ...@@ -172,14 +172,13 @@ var Tester = function Tester(casper, options) {
172 }); 172 });
173 173
174 function errorHandler(error, backtrace) { 174 function errorHandler(error, backtrace) {
175 self.casper.unwait();
175 if (error instanceof Error) { 176 if (error instanceof Error) {
176 self.processError(error); 177 self.processError(error);
177 return self.done(); 178 return self.done();
178 } 179 }
179 if (utils.isString(error)) { 180 if (utils.isString(error) && /^(Assertion|Termination|TimedOut)Error/.test(error)) {
180 if (/^(Assertion|Termination|TimedOut)Error/.test(error)) { 181 return;
181 return;
182 }
183 } 182 }
184 var line = 0; 183 var line = 0;
185 try { 184 try {
...@@ -190,9 +189,15 @@ var Tester = function Tester(casper, options) { ...@@ -190,9 +189,15 @@ var Tester = function Tester(casper, options) {
190 self.uncaughtError(error, self.currentTestFile, line, backtrace); 189 self.uncaughtError(error, self.currentTestFile, line, backtrace);
191 self.done(); 190 self.done();
192 } 191 }
193 this.casper.on('event.error', errorHandler); 192
194 this.casper.on('step.error', errorHandler); 193 [
195 this.casper.on('complete.error', errorHandler); 194 'waitFor.timeout.error',
195 'event.error',
196 'step.error',
197 'complete.error'
198 ].forEach(function(event) {
199 self.casper.on(event, errorHandler);
200 });
196 201
197 this.casper.on('warn', function(warning) { 202 this.casper.on('warn', function(warning) {
198 if (self.currentSuite) { 203 if (self.currentSuite) {
...@@ -979,7 +984,8 @@ Tester.prototype.done = function done() { ...@@ -979,7 +984,8 @@ Tester.prototype.done = function done() {
979 } 984 }
980 } 985 }
981 if (this.currentSuite && this.currentSuite.planned && 986 if (this.currentSuite && this.currentSuite.planned &&
982 this.currentSuite.planned !== this.executed + this.currentSuite.skipped) { 987 this.currentSuite.planned !== this.executed + this.currentSuite.skipped &&
988 !this.currentSuite.failed) {
983 this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name); 989 this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name);
984 } else if (planned && planned !== this.executed) { 990 } else if (planned && planned !== this.executed) {
985 // BC 991 // BC
...@@ -1272,18 +1278,20 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) { ...@@ -1272,18 +1278,20 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
1272 "use strict"; 1278 "use strict";
1273 /*jshint maxstatements:20*/ 1279 /*jshint maxstatements:20*/
1274 save = save || this.options.save; 1280 save = save || this.options.save;
1275 var failed = this.suiteResults.countFailed(), 1281 var exitStatus = 0,
1282 failed = this.suiteResults.countFailed(),
1276 total = this.suiteResults.countExecuted(), 1283 total = this.suiteResults.countExecuted(),
1277 statusText, 1284 statusText,
1278 style, 1285 style,
1279 result, 1286 result;
1280 exitStatus = ~~(status || (failed > 0 ? 1 : 0));
1281 if (total === 0) { 1287 if (total === 0) {
1288 exitStatus = 1;
1282 statusText = this.options.warnText; 1289 statusText = this.options.warnText;
1283 style = 'WARN_BAR'; 1290 style = 'WARN_BAR';
1284 result = f("%s Looks like you didn't run any test.", statusText); 1291 result = f("%s Looks like you didn't run any test.", statusText);
1285 } else { 1292 } else {
1286 if (failed > 0) { 1293 if (this.suiteResults.isFailed()) {
1294 exitStatus = 1;
1287 statusText = this.options.failText; 1295 statusText = this.options.failText;
1288 style = 'RED_BAR'; 1296 style = 'RED_BAR';
1289 } else { 1297 } else {
...@@ -1301,14 +1309,12 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) { ...@@ -1301,14 +1309,12 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
1301 this.suiteResults.countSkipped()); 1309 this.suiteResults.countSkipped());
1302 } 1310 }
1303 this.casper.echo(result, style, this.options.pad); 1311 this.casper.echo(result, style, this.options.pad);
1304 if (failed > 0) { 1312 this.renderFailureDetails();
1305 this.renderFailureDetails();
1306 }
1307 if (save) { 1313 if (save) {
1308 this.saveResults(save); 1314 this.saveResults(save);
1309 } 1315 }
1310 if (exit === true) { 1316 if (exit === true) {
1311 this.casper.exit(exitStatus); 1317 this.casper.exit(status ? ~~status : exitStatus);
1312 } 1318 }
1313 }; 1319 };
1314 1320
...@@ -1459,7 +1465,7 @@ exports.TestSuiteResult = TestSuiteResult; ...@@ -1459,7 +1465,7 @@ exports.TestSuiteResult = TestSuiteResult;
1459 */ 1465 */
1460 TestSuiteResult.prototype.countTotal = function countTotal() { 1466 TestSuiteResult.prototype.countTotal = function countTotal() {
1461 "use strict"; 1467 "use strict";
1462 return this.countPassed() + this.countFailed(); 1468 return this.countPassed() + this.countFailed() + this.countDubious();
1463 }; 1469 };
1464 1470
1465 /** 1471 /**
...@@ -1508,7 +1514,7 @@ TestSuiteResult.prototype.countErrors = function countErrors() { ...@@ -1508,7 +1514,7 @@ TestSuiteResult.prototype.countErrors = function countErrors() {
1508 TestSuiteResult.prototype.countFailed = function countFailed() { 1514 TestSuiteResult.prototype.countFailed = function countFailed() {
1509 "use strict"; 1515 "use strict";
1510 return this.map(function(result) { 1516 return this.map(function(result) {
1511 return result.failed; 1517 return result.failed - result.dubious;
1512 }).reduce(function(a, b) { 1518 }).reduce(function(a, b) {
1513 return a + b; 1519 return a + b;
1514 }, 0); 1520 }, 0);
...@@ -1563,7 +1569,7 @@ TestSuiteResult.prototype.countWarnings = function countWarnings() { ...@@ -1563,7 +1569,7 @@ TestSuiteResult.prototype.countWarnings = function countWarnings() {
1563 */ 1569 */
1564 TestSuiteResult.prototype.isFailed = function isFailed() { 1570 TestSuiteResult.prototype.isFailed = function isFailed() {
1565 "use strict"; 1571 "use strict";
1566 return this.countErrors() + this.countFailed() > 0; 1572 return this.countErrors() + this.countFailed() + this.countDubious() > 0;
1567 }; 1573 };
1568 1574
1569 /** 1575 /**
...@@ -1571,7 +1577,7 @@ TestSuiteResult.prototype.isFailed = function isFailed() { ...@@ -1571,7 +1577,7 @@ TestSuiteResult.prototype.isFailed = function isFailed() {
1571 * 1577 *
1572 * @return Number 1578 * @return Number
1573 */ 1579 */
1574 TestSuiteResult.prototype.isSkipped = function isFailed() { 1580 TestSuiteResult.prototype.isSkipped = function isSkipped() {
1575 "use strict"; 1581 "use strict";
1576 return this.countSkipped() > 0; 1582 return this.countSkipped() > 0;
1577 }; 1583 };
......