Skip to content

Commit

Permalink
Reverted logging behaviour of errors in some cases and added support …
Browse files Browse the repository at this point in the history
…for loading stock chai expect assertions onto the client expect api.
  • Loading branch information
beatfactor committed Sep 15, 2015
1 parent 1ec8094 commit b237ca9
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 49 deletions.
5 changes: 0 additions & 5 deletions lib/api/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ module.exports = function(client) {
flag(this, 'before', true);
});

ChaiAssertion.addMethod('before', function(ms) {
flag(this, 'waitFor', ms);
flag(this, 'before', true);
});

ChaiAssertion.addMethod('after', function(ms) {
flag(this, 'after', true);
flag(this, 'waitFor', ms);
Expand Down
10 changes: 9 additions & 1 deletion lib/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,15 @@ module.exports = new (function() {
var Expect = require('../api/expect.js')(client);
var assertions = Object.keys(Expect);

parent.expect = {};
try {
var chaiExpect = module.require('chai').expect;
parent.expect = function() {
return chaiExpect.apply(chaiExpect, arguments);
};
} catch (err) {
parent.expect = {};
}

assertions.forEach(function(assertion) {
parent.expect[assertion] = function() {
var args = Array.prototype.slice.call(arguments);
Expand Down
8 changes: 5 additions & 3 deletions lib/core/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ module.exports = new (function() {
if (originalStackTrace) {
var stackParts = originalStackTrace.split('\n');
stackParts.shift();
stackParts.unshift('AssertionError: ' + message);
if (!client.options.start_session) {
stackParts.unshift('AssertionError: ' + message);
}
originalStackTrace = getUsefulStacktrace(stackParts);
err.stack = originalStackTrace;
} else {
Expand All @@ -232,11 +234,11 @@ module.exports = new (function() {

if (client.options.output) {
if (client.options.start_session) {
console.log(stacktrace);
console.log(Logger.colors.light_gray(stacktrace));
} else {
var parts = stacktrace.split('\n');
parts.shift();
console.log(parts.join('\n'));
console.log(Logger.colors.light_gray(parts.join('\n')));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ AsyncTree.prototype.runCommand = function(node, callback) {
}
return node.context;
} catch (err) {
err.stack = node.stackTrace;
err.name = 'Error while running ' + node.name + ' command';
this.emit('error', err);
return this;
Expand Down
33 changes: 15 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,13 @@ Nightwatch.prototype.finished = function() {

Nightwatch.prototype.getFailureMessage = function() {
var errors = '';
var error;
if (this.results.errors) {
errors = this.results.errors + ' errors during the test. ';
for (var i = 0; i < this.errors.length; i++) {
console.log(Logger.colors.red(this.errors[i]));
error = this.errors[i].split('\n');
var headline = error.shift();
Utils.showStackTrace(headline, error);
}
}
var failure_msg = [];
Expand Down Expand Up @@ -314,10 +317,17 @@ Nightwatch.prototype.runProtocolAction = function(requestOptions, callback) {
try {
callback.call(self.api, result);
} catch (ex) {

var stack = ex.stack.split('\n');
var firstLine = stack.shift();
self.addError(ex.message, firstLine);
console.log(stack.join('\n'));
var firstLine = ' ' + String.fromCharCode(10006) + ' ' +stack.shift();

Utils.showStackTrace(firstLine, stack);
if (ex.name == 'AssertionError') {
self.results.failed++;
} else {
self.addError(ex.message, firstLine);
}

}
}

Expand Down Expand Up @@ -345,19 +355,6 @@ Nightwatch.prototype.runProtocolAction = function(requestOptions, callback) {
result.lastScreenshotFile = fileNamePath;
}

var errorMessage = result.error;
var msg = result.value && result.value.message || errorMessage;

if (result.lastScreenshotFile) {
msg += '\n\nScreenshot saved at: ' + result.lastScreenshotFile;
}

if (self.options.silent) {
msg += '\n\nFor more information about the error run with --verbose option.';
}

self.addError(msg);

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

Expand All @@ -375,7 +372,7 @@ Nightwatch.prototype.addError = function(message, logMessage) {
this.errors.push('Error while running '+ currentTest + ':\n' + message);
this.results.errors++;
if (this.options.output) {
console.log(' ' + Logger.colors.red(logMessage || message));
Logger.warn(' ' + (logMessage || message));
}

};
Expand Down
18 changes: 5 additions & 13 deletions lib/runner/testcase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path');
var Q = require('q');
var Logger = require('../util/logger.js');
var Utils = require('../util/utils.js');
var failSymbol = String.fromCharCode(10006);
var doneSymbol = String.fromCharCode(10004);

Expand Down Expand Up @@ -71,16 +72,9 @@ TestCase.prototype.run = function () {

if (err) {
var parts = err.stack.split('\n');
var logged = '';
if (typeof err.expected != 'undefined' && typeof err.actual != 'undefined') {
logged += 'AssertionError - ' + 'expected ' + Logger.colors.green('"' +
err.expected + '"') + ' but got: ' + Logger.colors.red(err.actual);
parts.shift();
console.log(logged);
}

var stackTrace = parts.join('\n');
console.log(stackTrace);
var headline = parts.shift();

Utils.showStackTrace(failSymbol + ' ' + headline, parts);
}
};

Expand All @@ -107,9 +101,7 @@ TestCase.prototype.run = function () {
var failed = self.suite.client.results('failed');
self.suite.client.results('failed', failed+1);

if (!self.suite.client.options.start_session) {
doneCb(err);
}
doneCb(err);
}

self.suite.client.start();
Expand Down
2 changes: 1 addition & 1 deletion lib/runner/testsuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TestSuite.prototype.initAfterEachHook = function() {
if (expectedArgs <= 1) {
asyncArgsCount = 1;
} else {
asyncArgsCount = 2;
asyncArgsCount = 2;
}
asyncFn = Utils.makeFnAsync(asyncArgsCount, hookFn, module);
return self.makePromise(function(done, deferred) {
Expand Down
8 changes: 8 additions & 0 deletions lib/util/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path');
var Util = module.exports = {};
var Logger = require('./logger.js');

Util.formatElapsedTime = function(timeMs, includeMs) {
var seconds = timeMs/1000;
Expand Down Expand Up @@ -157,4 +158,11 @@ Util.getModuleKey = function(filePath, srcFolders, fullPaths) {
}

return path.join(parentFolder, diffInFolder, moduleName);
};

Util.showStackTrace = function(headline, stack) {
console.log(Logger.colors.red(headline));
if (stack.length > 0) {
console.log(Logger.colors.light_gray(stack.join('\n')));
}
};
Empty file removed nightwatch.opts
Empty file.
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@
"mocha-nightwatch": "^2.2.5"
},
"devDependencies": {
"chai": "^3.2.0",
"coveralls": "latest",
"grunt": "~0.4.4",
"nodeunit": "latest",
"grunt-complexity": "^0.1.7",
"grunt-contrib-jshint": "~0.10.0",
"grunt-jsonlint": "~1.0.4",
"grunt-npm-release": "latest",
"jscoverage": "latest",
"coveralls": "latest",
"jshint": "~2.4.4",
"jsonlint": "~1.6.0",
"mock-spawn": "^0.2.1",
"mockery": "~1.4.0",
"jsonlint": "~1.6.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-jsonlint": "~1.0.4",
"grunt-complexity": "^0.1.7",
"grunt-npm-release": "latest",
"nock": "~0.45.0"
"nock": "~0.45.0",
"nodeunit": "latest"
},
"bin": {
"nightwatch": "./bin/nightwatch"
Expand Down
24 changes: 24 additions & 0 deletions tests/src/testAssertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ module.exports = {
test.done();
},

'Testing chai expect is loaded' : function(test) {
var client = require('../nightwatch.js').init({});
test.equal(typeof client.api.expect, 'function');
test.equal(typeof client.api.expect.element, 'function');

var something = 'test';
var expect = client.api.expect(something);
test.deepEqual(expect, require('chai').expect(something));

var element = client.api.expect.element('body');
test.ok('attribute' in element);
test.ok('css' in element);
test.ok('enabled' in element);
test.ok('present' in element);
test.ok('selected' in element);
test.ok('text' in element);
test.ok('a' in element);
test.ok('an' in element);
test.ok('value' in element);
test.ok('visible' in element);

test.done();
},

'Testing passed assertion retry' : function(test) {
var assertionFn = require('../../' + BASE_PATH + '/api/assertions/containsText.js');
var client = {
Expand Down

0 comments on commit b237ca9

Please sign in to comment.