phantomjs/runner.js in test_squad-0.0.1 vs phantomjs/runner.js in test_squad-0.1.0
- old
+ new
@@ -1,22 +1,26 @@
+/* jshint -W100 */
+/* global phantom */
(function () {
'use strict';
var system = require('system');
var webpage = require('webpage');
- var output = require('./helpers').output;
- var colored = require('./helpers').colored;
+ var helpers = require('./helpers');
+ var output = helpers.output;
+ var colored = helpers.colored;
+ var textForSelector = helpers.textForSelector;
var args = system.args;
var url = args[1];
var timeout = parseInt(args[2], 10);
- var enableLog = args[3] !== 'false';
var page = webpage.create();
var suites = [];
var suite, test;
var errorBuffer = [];
var pendingBuffer = [];
+ var resourceErrors = [];
page.viewportSize = {
width: 800,
height: 800
};
@@ -71,20 +75,24 @@
if (!plural) {
plural = singular + 's';
}
return count + ' ' + (count === 1 ? singular : plural);
- }
+ };
- page.onConsoleMessage = function(message, lineNumber, sourceId) {
+ page.onConsoleMessage = function(message) {
if (test) {
test.logging.push(message);
} else {
console.log(colored('=> ' + message, 'gray'));
}
};
+ page.onResourceError = function(error) {
+ resourceErrors.push(error);
+ };
+
page.onCallback = function(message){
if (message.name === 'suite start') {
suite = {tests: []};
suite.title = message.title;
@@ -125,13 +133,22 @@
console.log('');
if (stats.tests > 0) {
summary.push(pluralize(stats.tests, 'test'));
- stats.assertions && summary.push(pluralize(stats.assertions, 'assertion'));
- stats.pending && summary.push(pluralize(stats.pending, 'test') + ' pending');
- stats.fails && summary.push(pluralize(stats.fails, 'test') + ' failed');
+
+ if (stats.assertions) {
+ summary.push(pluralize(stats.assertions, 'assertion'));
+ }
+
+ if (stats.pending) {
+ summary.push(pluralize(stats.pending, 'test') + ' pending');
+ }
+
+ if (stats.fails) {
+ summary.push(pluralize(stats.fails, 'test') + ' failed');
+ }
} else {
summary.push('No tests were found.');
}
if (stats.fails) {
@@ -150,18 +167,27 @@
exit(stats.fails ? 1 : 0);
}
};
page.open(url, function(status){
- if (status !== 'success') {
+ if (status === 'fail') {
console.error('Unable to access network: ' + status);
exit(1);
} else {
- // Set a timeout on the test running, otherwise tests with async problems will hang forever
- setTimeout(function(){
- console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...');
+ if (resourceErrors.length === 1 && resourceErrors[0].url === url) {
+ var errorHeader = textForSelector(page, 'header > h1');
+ var errorDescription = textForSelector(page, '#container > pre');
+ var matches = errorDescription.match(/^(.*?) \(in (.*?)\)$/)
+
+ console.error(colored('ERROR:', 'red'), errorHeader, '-', matches[1]);
+ console.error(colored(matches[2], 'gray'));
exit(1);
- }, timeout * 1000);
+ } else {
+ setTimeout(function(){
+ console.error(colored('ERROR:', 'red'), 'The specified timeout of ' + timeout + ' seconds has expired. Aborting...');
+ exit(1);
+ }, timeout * 1000);
+ }
}
});
function exit(code) {
if (page) {