Skip to content

Commit

Permalink
added support for calling .end from after method - nightwatchjs#257
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Sep 7, 2014
1 parent ee8443e commit 9bf4e74
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 38 deletions.
9 changes: 7 additions & 2 deletions examples/tests/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module.exports = {
.waitForElementVisible('body', 1000)
.assert.title('beatfactor/nightwatch · GitHub')
.assert.visible('.container .breadcrumb a span')
.assert.containsText('.container .breadcrumb a span', 'nightwatch', 'Checking project title is set to nightwatch')
.end();
.assert.containsText('.container .breadcrumb a span', 'nightwatch', 'Checking project title is set to nightwatch');


},

after : function(client) {
client.end();
}
};
6 changes: 5 additions & 1 deletion lib/core/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ AsyncTree.prototype.walkUp = function(context) {
AsyncTree.prototype.getNextChild = function(context) {
for (var i = 0; i < context.children.length; i++) {
if (!context.children[i].started) {
return context.children[i];
var child = context.children[i];
context.children.splice(i, 1);
return child;
}
}
return false;
Expand Down Expand Up @@ -162,6 +164,8 @@ AsyncTree.prototype.runCommand = function(node, callback) {
};

AsyncTree.prototype.done = function() {

this.rootNode.started = false;
this.emit('queue:finished');
return this;
};
Expand Down
65 changes: 42 additions & 23 deletions lib/runner/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,39 @@ module.exports = new (function() {
steps : keys.slice(0)
};

if (afterIndex > -1) {
keys.splice(afterIndex, 1);
testResults.steps.splice(testResults.steps.indexOf('after'), 1);
if (after.length >= 2) {
afterCallback = function(error, results) {
after.call(module, client && client.api || null, function() {
moduleCallback(error, results);
var callAfterFn = function(context, afterFn, moduleCallback) {
if (afterFn.length >= 2) {
return function(error, results) {
afterFn.call(context, client && client.api, function() {
checkQueue(error, results, moduleCallback);
});
};
} else {
afterCallback = function(error, results) {
after.call(module, client && client.api || null);
}

return function(error, results) {
afterFn.call(context, client && client.api);
checkQueue(error, results, moduleCallback);
};
};

var checkQueue = function(error, results, moduleCallback) {
if (shouldRestartQueue()) {
restartQueue(function() {
moduleCallback(error, results);
};
});
} else {
moduleCallback(error, results);
}
} else {
afterCallback = moduleCallback;
}
};

var shouldRestartQueue = function() {
return client && client.queue.list().length;
};

var restartQueue = function(onComplete) {
client.queue.reset();
client.queue.run(onComplete);
};

var onTestFinished = function (results, errors, startTime) {
globalResults.modules[moduleKey][currentTest] = {
Expand Down Expand Up @@ -153,6 +168,14 @@ module.exports = new (function() {
}
};

if (afterIndex > -1) {
keys.splice(afterIndex, 1);
testResults.steps.splice(testResults.steps.indexOf('after'), 1);
afterCallback = callAfterFn(module, after, moduleCallback);
} else {
afterCallback = moduleCallback;
}

if (module.disabled === true) {
if (opts.output) {
console.log(Logger.colors.cyan(moduleName), 'module is disabled, skipping...');
Expand Down Expand Up @@ -217,15 +240,10 @@ module.exports = new (function() {
if (typeof module.afterEach == 'function' || typeof module.tearDown == 'function') {
var afterEachFn = module.afterEach || module.tearDown;
afterEach = function(context, clientFn, client, onTestComplete) {
if (afterEachFn.length === 0) {
startClient(context, clientFn, client, function(results, errors) {
callTearDown(module, results, errors, onTestComplete, false);
});
} else if (afterEachFn.length >= 0) {
startClient(context, clientFn, client, function(results, errors) {
callTearDown(module, results, errors, onTestComplete, true);
});
}
var hasCallback = afterEachFn.length >= 0;
startClient(context, clientFn, client, function(results, errors) {
callTearDown(module, results, errors, onTestComplete, hasCallback);
});
};
var indexAfter = keys.indexOf('afterEach');
if (indexAfter == -1) {
Expand All @@ -244,6 +262,7 @@ module.exports = new (function() {
} else {
afterEach = startClient;
}

var beforeIndex = keys.indexOf('before');
if (beforeIndex > -1) {
keys.splice(beforeIndex, 1);
Expand Down
9 changes: 7 additions & 2 deletions tests/sampletests/simple/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module.exports = {
client.globals.test.equals(client.options.desiredCapabilities.name, 'test-Name');

client.url('http://localhost')
.assert.elementPresent('#weblogin')
.end();
.assert.elementPresent('#weblogin');

},
after : function(client) {
client.end(function() {
client.globals.test.ok('END called');
});
}
};
8 changes: 8 additions & 0 deletions tests/sampletests/usexpath/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@ module.exports = {
})
.useCss()
.end();
},

after : function(client, callback) {
process.nextTick(function() {
client.end();
client.globals.test.ok('after callback called');
callback();
});
}
};
1 change: 1 addition & 0 deletions tests/src/commands/testLocateStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
},

'test run sample test with xpath' : function(test) {
test.expect(3);
Runner.run([process.cwd() + '/sampletests/usexpath'], {
seleniumPort : 10195,
silent : true,
Expand Down
1 change: 1 addition & 0 deletions tests/src/runner/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
},

testRunSimple : function(test) {
test.expect(5);
var testsPath = path.join(process.cwd(), '/sampletests/simple');
this.Runner.run([testsPath], {
seleniumPort : 10195,
Expand Down
21 changes: 11 additions & 10 deletions tests/src/testQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ module.exports = {
'Test commands queue' : function(test) {
var client = this.client, urlCommand, endCommand;
this.client.on('nightwatch:finished', function() {
test.equal(true, urlCommand.done);
test.equal(true, endCommand.children[0].done);
test.equal(true, endCommand.done);
test.equal(urlCommand.done, true);
test.equal(endCommand.children.length, 0);
test.equal(endCommand.done, true);
test.equal(CommandQueue.list().length, 0);
test.done();
});

client.api.url('http://localhost').end();

test.deepEqual(CommandQueue.list(), [ 'url', 'end']);
test.equal(CommandQueue.list().length, 2);
urlCommand = CommandQueue.instance().rootNode.children[0];
endCommand = CommandQueue.instance().rootNode.children[1];

this.client.on('selenium:session_create', function(sessionId) {
urlCommand = CommandQueue.instance().rootNode.children[0];
endCommand = CommandQueue.instance().rootNode.children[1];
test.equal(false, endCommand.done);
test.equal(false, urlCommand.done);
test.equal(false, endCommand.started);
test.equal(true, urlCommand.started);
test.equal(endCommand.done, false);
test.equal(urlCommand.done, false);
test.equal(endCommand.started, false);
test.equal(urlCommand.started, true);
});

},
Expand Down

0 comments on commit 9bf4e74

Please sign in to comment.