Skip to content
This repository has been archived by the owner on Jan 26, 2018. It is now read-only.

Commit

Permalink
Fixed nightwatchjs#745 - properly clearing the command queue when an …
Browse files Browse the repository at this point in the history
…assertion fails
  • Loading branch information
beatfactor committed Jan 1, 2016
1 parent 91800ee commit 5e81501
Show file tree
Hide file tree
Showing 21 changed files with 1,016 additions and 842 deletions.
2 changes: 1 addition & 1 deletion examples/tests/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
'Demo test GitHub' : function (client) {
client
.url('https://github.com/nightwatchjs/nightwatch')
.waitForElementVisible('xbody', 1000)
.waitForElementVisible('body', 1000)
.assert.title('nightwatchjs/nightwatch · GitHub')
.assert.visible('.container h1 strong a')
.assert.containsText('.container h1 strong a', 'nightwatch', 'Checking project title is set to nightwatch');
Expand Down
2 changes: 1 addition & 1 deletion examples/unittests/testUtilsWithChai.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {

testFormatElapsedTimeMore : function(client) {
var resultMs = Utils.formatElapsedTime(999);
expect(resultMs).to.equal('999msx');
expect(resultMs).to.equal('999ms');
},

testMakeFnAsync : function(client) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ module.exports = new (function() {
});

if (!passed && abortOnFailure) {
client.terminate();
client.terminate(true);
}
};

Expand Down
3 changes: 3 additions & 0 deletions lib/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ AsyncTree.prototype.scheduleTraverse = function(node) {
};

AsyncTree.prototype.traverse = function() {
this.emit('queue:started');

try {
this.walkDown(this.currentNode);
} catch (err) {
Expand Down Expand Up @@ -215,6 +217,7 @@ module.exports = new (function() {
callback(err);
});
}

return queue.traverse();
};

Expand Down
33 changes: 22 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,38 @@ Nightwatch.prototype.start = function() {
return this;
};

Nightwatch.prototype.terminate = function() {
var self = this;
Nightwatch.prototype.terminate = function(deferred) {
this.terminated = true;

// in case this was a synchronous command (e.g. assert.ok()) we need to wait for other possible
// commands which might have been added afterwards while client is terminated
if (deferred) {
this.queue.instance().once('queue:started', this.terminateSession.bind(this));
} else {
this.terminateSession();
}

return this;
};

Nightwatch.prototype.resetTerminated = function() {
this.terminated = false;
return this;
};

Nightwatch.prototype.terminateSession = function() {
this.queue.reset();
this.queue.empty();

if (this.options.end_session_on_fail && this.options.start_session) {
this.api.end(function() {
self.finished();
});
this.finished();
}.bind(this));

this.queue.run();
} else {
this.finished();
}

return this;
};

Nightwatch.prototype.resetTerminated = function() {
this.terminated = false;
return this;
};

Expand Down Expand Up @@ -326,12 +337,12 @@ Nightwatch.prototype.runProtocolAction = function(requestOptions, callback) {
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
28 changes: 19 additions & 9 deletions lib/runner/testsuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ TestSuite.prototype.printResult = function(startTime) {
return this.client.print(startTime);
};

TestSuite.prototype.shouldRetrySuite = function() {
return this.suiteMaxRetries > this.suiteRetries && (this.testResults.failed > 0 || this.testResults.errors > 0);
};

TestSuite.prototype.setTestResult = function() {
this.testResults = {};
this.testResults.steps = this.module.keys.slice(0);
Expand Down Expand Up @@ -259,11 +263,15 @@ TestSuite.prototype.printRetry = function() {
};

TestSuite.prototype.retryTestSuiteModule = function() {
this.client.resetTerminated();
this.clearResult();
this.suiteRetries +=1;
this.resetTestCases();
this.printRetry();
this.globalBeforeEach();
return this.runTestSuiteModule();

return this.globalBeforeEach().then(function() {
return this.runTestSuiteModule();
}.bind(this));
};

TestSuite.prototype.runTestSuiteModule = function() {
Expand All @@ -279,17 +287,19 @@ TestSuite.prototype.runTestSuiteModule = function() {
return self.client.checkQueue();
})
.then(function() {
if (self.suiteMaxRetries > self.suiteRetries && (self.testResults.failed > 0 || self.testResults.errors > 0)) {
self.globalAfterEach();
self.client.resetTerminated();
self.clearResult();
return self.retryTestSuiteModule();
return self.shouldRetrySuite();
})
.then(function(shouldRetrySuite) {
if (shouldRetrySuite) {
return self.globalAfterEach().then(function() {
return self.retryTestSuiteModule();
});
}
})
.catch(function(e) {
this.testResults.errors++;
self.testResults.errors++;
throw e;
}.bind(this));
});
};

TestSuite.prototype.onTestCaseFinished = function(results, errors, time) {
Expand Down
37 changes: 37 additions & 0 deletions tests/sampletests/suiteretries/suiteRetriesSample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var failFirstTime = true;

module.exports = {
before: function (client) {
client.url('http://localhost')
},

after: function (client) {
client.end();
},

demoStep1 : function (client) {
client
.assert.elementPresent('#weblogin')
.perform(function() {
client.globals.test.ok('demoStep1 called.');
});

},

demoStep2 : function (client) {
client
.elements('css selector', '#weblogin', function() {
if (failFirstTime) {
failFirstTime = false;
client.assert.ok(false);
}

client
.assert.elementPresent('#weblogin')
.perform(function() {
client.globals.test.ok('demoStep2 called.');
});
});
}

};
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/src/runner/testParallelExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module.exports = {
});

runner.setup({}, function() {
test.equals(self.allArgs.length, 20);
test.equals(self.allArgs.length, 21);
test.ok(path.join('sampletests', 'async', 'sample_1') in runner.runningProcesses);
test.ok(path.join('sampletests', 'before-after', 'sampleSingleTest_2') in runner.runningProcesses);

Expand Down Expand Up @@ -144,7 +144,7 @@ module.exports = {

runner.setup({}, function() {
test.ok(!runner.isParallelMode());
test.equals(self.allArgs.length, 20);
test.equals(self.allArgs.length, 21);
test.done();
});

Expand All @@ -169,7 +169,7 @@ module.exports = {

runner.setup({}, function() {
test.ok(!runner.isParallelMode());
test.equals(self.allArgs.length, 20);
test.equals(self.allArgs.length, 21);
test.done();
});

Expand Down
Loading

0 comments on commit 5e81501

Please sign in to comment.