From b237ca929d76f05cc335b75aba77d0335c9c783a Mon Sep 17 00:00:00 2001 From: Andrei Rusu Date: Wed, 16 Sep 2015 00:10:05 +0200 Subject: [PATCH] Reverted logging behaviour of errors in some cases and added support for loading stock chai expect assertions onto the client expect api. --- lib/api/expect.js | 5 ----- lib/core/api.js | 10 +++++++++- lib/core/assertion.js | 8 +++++--- lib/core/queue.js | 1 + lib/index.js | 33 +++++++++++++++------------------ lib/runner/testcase.js | 18 +++++------------- lib/runner/testsuite.js | 2 +- lib/util/utils.js | 8 ++++++++ nightwatch.opts | 0 package.json | 17 +++++++++-------- tests/src/testAssertions.js | 24 ++++++++++++++++++++++++ 11 files changed, 77 insertions(+), 49 deletions(-) delete mode 100644 nightwatch.opts diff --git a/lib/api/expect.js b/lib/api/expect.js index 5328cdb03f..f0df3e6c33 100755 --- a/lib/api/expect.js +++ b/lib/api/expect.js @@ -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); diff --git a/lib/core/api.js b/lib/core/api.js index 8ff0ea1657..1bb786a413 100644 --- a/lib/core/api.js +++ b/lib/core/api.js @@ -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); diff --git a/lib/core/assertion.js b/lib/core/assertion.js index 862a39e6ba..41d801ae27 100644 --- a/lib/core/assertion.js +++ b/lib/core/assertion.js @@ -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 { @@ -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'))); } } } diff --git a/lib/core/queue.js b/lib/core/queue.js index 5e9d1fcc9a..c2d5dbdff2 100644 --- a/lib/core/queue.js +++ b/lib/core/queue.js @@ -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; diff --git a/lib/index.js b/lib/index.js index 042d543fef..750fb30956 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 = []; @@ -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); + } + } } @@ -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); }); @@ -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)); } }; diff --git a/lib/runner/testcase.js b/lib/runner/testcase.js index 8f16cc2fc3..feafbcd841 100644 --- a/lib/runner/testcase.js +++ b/lib/runner/testcase.js @@ -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); @@ -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); } }; @@ -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(); diff --git a/lib/runner/testsuite.js b/lib/runner/testsuite.js index 289fc2fc3f..9df4b0e92a 100644 --- a/lib/runner/testsuite.js +++ b/lib/runner/testsuite.js @@ -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) { diff --git a/lib/util/utils.js b/lib/util/utils.js index 85d76e0aa7..ed31453507 100644 --- a/lib/util/utils.js +++ b/lib/util/utils.js @@ -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; @@ -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'))); + } }; \ No newline at end of file diff --git a/nightwatch.opts b/nightwatch.opts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/package.json b/package.json index 0eaf2d8c70..93d63e347f 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/tests/src/testAssertions.js b/tests/src/testAssertions.js index 9b8128dae6..df6c4794ef 100644 --- a/tests/src/testAssertions.js +++ b/tests/src/testAssertions.js @@ -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 = {