Skip to content

Commit

Permalink
Merge branch 'latest'
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Aug 18, 2014
2 parents 55610b1 + 3d88775 commit a7896cb
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 128 deletions.
39 changes: 23 additions & 16 deletions bin/_clirunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,30 @@ CliRunner.prototype = {
*/
startSelenium : function(callback) {
callback = callback || function() {};
var beforeGlobal = this.test_settings.globals && this.test_settings.globals.before || function(done) {done();};

if (!this.manageSelenium) {
callback();
beforeGlobal(function() {
callback();
});
return this;
}
this.settings.parallelMode = this.parallelMode;
var self = this;

Selenium.startServer(this.settings, function(error, child, error_out, exitcode) {
if (error) {
if (self.test_settings.output) {
Logger.error('There was an error while starting the Selenium server:');
beforeGlobal(function() {
Selenium.startServer(self.settings, function(error, child, error_out, exitcode) {
if (error) {
if (self.test_settings.output) {
Logger.error('There was an error while starting the Selenium server:');
}
self.globalErrorHandler({
message : error_out
});
return;
}
self.globalErrorHandler({
message : error_out
});
return;
}

callback();

callback();
});
});
return this;
},
Expand Down Expand Up @@ -260,9 +263,13 @@ CliRunner.prototype = {
src_folders : self.settings.src_folders,
live_output : self.settings.live_output
}, function(err) {
self
.stopSelenium()
.globalErrorHandler(err);
self.stopSelenium();

var afterGlobal = self.test_settings.globals && self.test_settings.globals.after || function(done) {done();};
afterGlobal(function() {
self.globalErrorHandler(err);
});

});
});

Expand Down
6 changes: 3 additions & 3 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ try {
} else {
process.chdir(process.cwd());

var runner = new CliRunner(argv);
var runner = module.exports = new CliRunner(argv);
runner.init().runTests();
}
} catch (ex) {
Logger.error('There was an error while starting the test runner:\n');
console.log(ex.stack);
Logger.error('There was an error while starting the test runner:\n\n');
process.stderr.write(ex.stack);
process.exit(2);
}
1 change: 1 addition & 0 deletions lib/core/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = new (function() {

var expected = typeof self.expected == 'function' ? self.expected() : self.expected;
self.client.assertion(passed, value, expected, self.message, self.abortOnFailure);
self.emit('complete');
});
};

Expand Down
123 changes: 72 additions & 51 deletions lib/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,65 @@ AsyncTree.prototype.append = function(nodeName, command, context, args) {
parent : this.currentNode
};
this.currentNode.children.push(node);
//this.print(this.rootNode);

if (this.currentNode.started && !this.currentNode.done) {
this.scheduleTraverse();
this.scheduleTraverse(node);
}
};

AsyncTree.prototype.runChildNode = function runChildNode(node) {
var self = this;

this.runCommand(node, function onCommandComplete(command) {
var timems = new Date().getTime() - node.startTime;
Logger.log(' ' + Logger.colors.green('→') +
' Completed command ' + Logger.colors.light_green(node.name), '(' + timems, 'ms)');
// checking if new children have been added while running this command which haven't finished yet
var childrenInProgress = false;
var currentChildNode = null;
for (var i = 0; i < node.children.length; i++) {
currentChildNode = node.children[i];
if (!currentChildNode.done) {
childrenInProgress = true;
break;
}
AsyncTree.prototype.print = function(node, level) {
process.stdout.write(node.name + '\n');
level = level || 1;
for (var i = 0; i < node.children.length; i++) {
var childNode = node.children[i];
for (var k = 0; k < level; k++) {
process.stdout.write(' |- ');
}

if (!childrenInProgress) {
self.traverse();
if (childNode.children.length) {
var levelUp = level + 1;
arguments.callee(childNode, levelUp);
} else {
process.stdout.write(childNode.name + '\n');
}
}
process.stdout.write('');
};

AsyncTree.prototype.scheduleTraverse = function(node) {
if (this.scheduled) {
return this;
}
this.scheduled = true;
var self = this;
process.nextTick(function() {
self.scheduled = false;
self.traverse();
});
};

AsyncTree.prototype.traverse = function() {
try {
this.walkDown(this.currentNode);
} catch (err) {
console.log(err.stack);
}

this.currentNode.started = true;
return this;
};

AsyncTree.prototype.walkDown = function walkDown(context) {
var self = this;
var node = this.getNextChild(context);

if (node) {
this.runChildNode(node);
} else {
context.done = true;
if (context.instance && !context.done) {
return;
}

if (this.currentNode.name === '__root__') {
this.currentNode.done = true;
this.done();
} else {
this.walkUp(context);
Expand All @@ -76,7 +96,6 @@ AsyncTree.prototype.walkDown = function walkDown(context) {

AsyncTree.prototype.walkUp = function(context) {
this.currentNode = context.parent;
context.done = true;
this.walkDown(context.parent);
};

Expand All @@ -89,6 +108,31 @@ AsyncTree.prototype.getNextChild = function(context) {
return false;
};

AsyncTree.prototype.runChildNode = function runChildNode(node) {
var self = this;

this.runCommand(node, function onCommandComplete() {
var timems = new Date().getTime() - node.startTime;
// checking if new children have been added while running this command which haven't finished yet
var childrenInProgress = false;
var currentChildNode = null;
for (var i = 0; i < node.children.length; i++) {
currentChildNode = node.children[i];
if (!currentChildNode.done) {
childrenInProgress = true;
break;
}
}

Logger.log(' ' + Logger.colors.green('→') +
' Completed command ' + Logger.colors.light_green(node.name), '(' + timems, 'ms)');
node.done = true;
if (!childrenInProgress) {
self.traverse();
}
});
};

AsyncTree.prototype.runCommand = function(node, callback) {
this.currentNode = node;
node.started = true;
Expand All @@ -105,10 +149,10 @@ AsyncTree.prototype.runCommand = function(node, callback) {
}

node.startTime = new Date().getTime();
var emitter = commandFn.apply(node.context, node.args);

if (emitter instanceof events.EventEmitter) {
emitter.once('complete', callback);
var instance = commandFn.apply(node.context, node.args);
if (instance instanceof events.EventEmitter) {
node.instance = instance;
instance.once('complete', callback);
}
return node.context;
} catch (err) {
Expand Down Expand Up @@ -136,29 +180,6 @@ AsyncTree.prototype.reset = function() {
return this;
};

AsyncTree.prototype.scheduleTraverse = function() {
if (this.scheduled) {
return this;
}
this.scheduled = true;
var self = this;
process.nextTick(function() {
self.scheduled = false;
self.traverse();
});
};

AsyncTree.prototype.traverse = function() {
try {
this.walkDown(this.currentNode);
} catch (err) {
console.log(err.stack);
}

this.currentNode.started = true;
return this;
};

module.exports = new (function() {
var queue = new AsyncTree();

Expand Down
3 changes: 1 addition & 2 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ module.exports = (function() {
} else {
self.emit('error', result, response, screenshotContent);
}

self.emit('complete', response);
//self.emit('complete', response);
});
});

Expand Down
106 changes: 53 additions & 53 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,63 +257,63 @@ Nightwatch.prototype.printResult = function(startTime) {
Nightwatch.prototype.runProtocolAction = function(requestOptions, callback) {
var self = this;

var request = new HttpRequest(requestOptions);
request.on('success', function(result, response) {
if (result.status && result.status !== 0) {
result = self.handleTestError(result);
}
request.emit('result', result, response);
})
.on('error', function(result, response, screenshotContent) {
result = self.handleTestError(result);
if (screenshotContent && self.options.screenshots.enabled) {
var d = new Date();
var dateStamp = d.toLocaleString('en-GB', {
weekday : 'narrow',
year : 'numeric',
month : '2-digit',
day : '2-digit',
timeZoneName : 'short',
hour : '2-digit',
minute : '2-digit',
second : '2-digit',
era : 'short'
}).replace(/:/g,'').replace(/\s/g,'-').replace(/-\(.+?\)/,'');

var fileNamePath = path.resolve(path.join(self.options.screenshots.path, 'ERROR_' +
dateStamp + '.png'));
self.saveScreenshotToFile(fileNamePath, screenshotContent);
result.lastScreenshotFile = fileNamePath;
}

request.emit('result', result, response);
})
.on('result', function(result) {
if (typeof callback != 'function') {
var error = new Error('Callback parameter is not a function - ' + typeof(callback) + ' passed: "' + callback + '"');
self.errors.push(error.stack);
self.results.errors++;
} else {
try {
callback.call(self.api, result);
} catch (ex) {
self.errors.push('Error while running tests: ' + ex.message);
var stack = ex.stack.split('\n');
var firstLine = stack.shift();
console.log(' ' + Logger.colors.red(firstLine));
console.log(stack.join('\n'));
var request = new HttpRequest(requestOptions)
.on('result', function(result) {
if (typeof callback != 'function') {
var error = new Error('Callback parameter is not a function - ' + typeof(callback) + ' passed: "' + callback + '"');
self.errors.push(error.stack);
self.results.errors++;
} else {
try {
callback.call(self.api, result);
} catch (ex) {
self.errors.push('Error while running tests: ' + ex.message);
var stack = ex.stack.split('\n');
var firstLine = stack.shift();
console.log(' ' + Logger.colors.red(firstLine));
console.log(stack.join('\n'));
self.results.errors++;
}
}
}

if (result.lastScreenshotFile && self.results.tests.length > 0) {
var lastTest = self.results.tests[self.results.tests.length-1];
lastTest.screenshots = lastTest.screenshots || [];
lastTest.screenshots.push(result.lastScreenshotFile);
delete result.lastScreenshotFile;
}
if (result.lastScreenshotFile && self.results.tests.length > 0) {
var lastTest = self.results.tests[self.results.tests.length-1];
lastTest.screenshots = lastTest.screenshots || [];
lastTest.screenshots.push(result.lastScreenshotFile);
delete result.lastScreenshotFile;
}
request.emit('complete');
})
.on('success', function(result, response) {
if (result.status && result.status !== 0) {
result = self.handleTestError(result);
}
request.emit('result', result, response);
})
.on('error', function(result, response, screenshotContent) {
result = self.handleTestError(result);
if (screenshotContent && self.options.screenshots.enabled) {
var d = new Date();
var dateStamp = d.toLocaleString('en-GB', {
weekday : 'narrow',
year : 'numeric',
month : '2-digit',
day : '2-digit',
timeZoneName : 'short',
hour : '2-digit',
minute : '2-digit',
second : '2-digit',
era : 'short'
}).replace(/:/g,'').replace(/\s/g,'-').replace(/-\(.+?\)/,'');

var fileNamePath = path.resolve(path.join(self.options.screenshots.path, 'ERROR_' +
dateStamp + '.png'));
self.saveScreenshotToFile(fileNamePath, screenshotContent);
result.lastScreenshotFile = fileNamePath;
}

});
request.emit('result', result, response);
});

return request;
};
Expand Down
Loading

0 comments on commit a7896cb

Please sign in to comment.