Skip to content

Commit

Permalink
Merge branch 'master' of github.com:beatfactor/nightwatch
Browse files Browse the repository at this point in the history
* 'master' of github.com:beatfactor/nightwatch:
  added command timers to show how long it took a command to run in ms
  fixed a small issue with showing output when disabled
  moved the test into protocol actions
  moved the path resolve into index and a few other small refactoring
  Added exclude tests option based on #110
  The screenshot path in junit must be absolute
  fixed typo in example
  style(jshint): satisfy "W004: 'prop' is already defined."
  style(jshint): satisfy "W033: Missing semicolon"
  style(jshint): satisfy "W109: Strings must use singlequote."
  style(white): fix trailing whitespaces
  Allowing client API to use the keys command from the Selenium protocol.
  • Loading branch information
beatfactor committed Apr 2, 2014
2 parents 4a224f1 + 467dd55 commit 10039f2
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 57 deletions.
20 changes: 11 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,10 @@ Nightwatch.prototype.runProtocolAction = function(requestOptions, callback) {
era : 'short'
}).replace(/:/g,'').replace(/\s/g,'-').replace(/-\(.+?\)/,'');

var fileName = path.join(self.options.screenshots.path, 'ERROR_' +
dateStamp + '.png');
self.saveScreenshotToFile(fileName, screenshotContent);

if (self.results.tests.length) {
var lastTest = self.results.tests[self.results.tests.length-1];
lastTest.screenshots = lastTest.screenshots || [];
lastTest.screenshots.push(fileName);
}
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);
Expand All @@ -300,6 +295,13 @@ Nightwatch.prototype.runProtocolAction = function(requestOptions, callback) {

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;
}
}
});

Expand Down
5 changes: 4 additions & 1 deletion lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ util.inherits(AsyncTree, events.EventEmitter);

AsyncTree.prototype.append = function(nodeName, command, context, args, reset) {
var node = {
startTime : null,
name : nodeName,
command : command,
context : context,
Expand All @@ -39,7 +40,8 @@ AsyncTree.prototype.walkDown = function walkDown(context) {
if (node) {
this.currentNode = node;
this.runCommand(node, function onCommandComplete(command) {
Logger.log(Logger.colors.light_gray(' - Completed command ' + Logger.colors.light_green(node.name)));
var timems = new Date().getTime() - node.startTime;
console.log(Logger.colors.light_gray(' - 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;
Expand Down Expand Up @@ -90,6 +92,7 @@ AsyncTree.prototype.runCommand = function(node, callback) {
throw new Error('Command must be a function');
}

node.startTime = new Date().getTime();
var emitter = commandFn.apply(node.context, node.args);
if (emitter instanceof events.EventEmitter) {
emitter.once('complete', callback);
Expand Down
2 changes: 1 addition & 1 deletion lib/selenium/commands/pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var events = require('events');
* Suspends the test for the given time in milliseconds. If the milliseconds argument is missing it will suspend the test indefinitely
*
* ```
* this.demoTest = function (client) {
* this.demoTest = function (browser) {
* browser.pause(1000);
* // or suspend indefinitely
* browser.pause();
Expand Down
12 changes: 12 additions & 0 deletions lib/selenium/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,18 @@ module.exports = function(Nightwatch) {
return getRequest('/title', callback);
};

/**
* Send a sequence of key strokes to the active element.
*
* @link /session/:sessionId/keys
* @param {Array} keys The keys sequence to be sent. The sequence is defined in the [send keys](https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/value) command.
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @api protocol
*/
Actions.keys = function(keys, callback) {
return postRequest('/keys', { value: keys }, callback);
};

/////////////////////////////////////////////////////////////////////////////
// Cookies
/////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions runner/reporters/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ exports.save = function(results, folder, callback) {
failures += module[x].failed;
}
}

var rendered = ejs.render(tmpl, {
locals: {
module : module,
moduleName : moduleName,
tests : tests,
testsNo : tests,
errors : errors,
failures : failures,
systemerr : results.errmessages.join('\n')
Expand Down
2 changes: 1 addition & 1 deletion runner/reporters/junit.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<testsuites name="<%= moduleName %>"
errors="<%= errors %>"
failures="<%= failures %>"
tests="<%= tests %>">
tests="<%= testsNo %>">
<% for (var testsuite in module) { %>
<testsuite errors="<%= module[testsuite].errors %>" failures="<%= module[testsuite].failed %>" hostname="" id=""
name="<%= testsuite %>" package="<%= moduleName %>" skipped="<%= module[testsuite].skipped %>"
Expand Down
41 changes: 31 additions & 10 deletions runner/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ module.exports = new (function() {
testResults.errors++;
client.terminate();
error = true;
moduleCallback(err, testResults);
}
} else {
moduleCallback(null, testResults);
Expand All @@ -112,7 +111,7 @@ module.exports = new (function() {

if (module.disabled === true) {
if (opts.output) {
console.log('\nSkipping module: ', Logger.colors.cyan(moduleName));
console.log(Logger.colors.cyan(moduleName), 'module is disabled, skipping...');
}
moduleCallback(null, false);
return;
Expand Down Expand Up @@ -184,7 +183,7 @@ module.exports = new (function() {
}

function processExitListener() {
process.on('exit', function(code) {
process.on('exit', function (code) {
if (globalResults.errors > 0 || globalResults.failed > 0) {
process.exit(1);
} else {
Expand Down Expand Up @@ -213,6 +212,16 @@ module.exports = new (function() {
}

paths.forEach(function(p) {
if (opts.exclude) {
opts.exclude = opts.exclude.map(function(item) {
// remove trailing slash
if (item.charAt(item.length-1) === path.sep) {
item = item.substring(0, item.length-1);
}
return path.join(p, item);
});
}

walk(p, function(err, list) {
if (err) {
return cb(err);
Expand All @@ -221,15 +230,24 @@ module.exports = new (function() {

var modules = list.filter(function (filePath) {
var filename = filePath.split(path.sep).slice(-1)[0];
return opts.filter ?
minimatch(filename, opts.filter) :
extensionPattern.exec(filePath);

if (opts.exclude) {
for (var i = 0; i < opts.exclude.length; i++) {
if (minimatch(filePath, opts.exclude[i])) {
return false;
}
}
}

if (opts.filter) {
return minimatch(filename, opts.filter);
}
return extensionPattern.exec(filePath);
});

modules = modules.map(function (filename) {
return filename.replace(extensionPattern, '');
});

cb(null, modules);
}, opts);
});
Expand All @@ -253,7 +271,10 @@ module.exports = new (function() {
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
var dirName = file.split(path.sep).slice(-1)[0];
if (opts.skipgroup && opts.skipgroup.indexOf(dirName) > -1) {
var isExcluded = opts.exclude && opts.exclude.indexOf(file) > -1;
var isSkipped = opts.skipgroup && opts.skipgroup.indexOf(dirName) > -1;

if (isExcluded || isSkipped) {
pending = pending-1;
} else {
walk(file, function(err, res) {
Expand Down Expand Up @@ -359,7 +380,7 @@ module.exports = new (function() {
runTestModule(err, fullpaths);
}, 0);
} else {
if (opts.output && testresults.tests != globalResults.tests || testresults.steps.length > 1) {
if (opts.output && (testresults.tests != globalResults.tests || testresults.steps.length > 1)) {
printResults(globalResults, modulekeys);
}

Expand All @@ -382,8 +403,8 @@ module.exports = new (function() {
console.log(Logger.colors.yellow('Warning: Failed to save report file to folder: ' + output));
console.log(err.stack);
}
finishCallback(null);
});
finishCallback(null);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ process.chdir(__dirname);
try {
var server = require('mockserver').init();
server.on('listening', function() {
reporter.run(['src', 'src/index', 'src/assertions', 'src/protocol', 'src/commands'], options, function() {
reporter.run(['src', 'src/index', 'src/runner', 'src/assertions', 'src/protocol', 'src/commands'], options, function() {
server.close();
});
});
Expand Down
7 changes: 7 additions & 0 deletions tests/sampletests/withexclude/excluded/excluded-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
demoTestExcluded : function (client) {
client.url('http://localhost')
.assert.elementPresent('#weblogin')
.end();
}
};
7 changes: 7 additions & 0 deletions tests/sampletests/withexclude/simple/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
demoTest : function (client) {
client.url('http://localhost')
.assert.elementPresent('#weblogin')
.end();
}
};
46 changes: 34 additions & 12 deletions tests/src/runner/testRunner.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
var Runner = require('../../../runner/run.js');

module.exports = {
setUp: function (callback) {
process.on('exit', function(code) {
process.exit(0);
});
callback();
},

testRunEmptyFolder : function(test) {
Runner.run([process.cwd() + '/sampletests/empty'], {
}, {
Expand Down Expand Up @@ -36,6 +29,40 @@ module.exports = {
});
},

testRunWithExcludeFolder : function(test) {
Runner.run([process.cwd() + '/sampletests/withexclude'], {
seleniumPort : 10195,
silent : true,
output : false,
globals : {
test : test
},
exclude : ['excluded']
}, {
output_folder : false
}, function(err, results) {
test.ok(!('excluded-module' in results.modules));
test.done();
});
},

testRunWithExcludePattern : function(test) {
Runner.run([process.cwd() + '/sampletests/withexclude'], {
seleniumPort : 10195,
silent : true,
output : false,
globals : {
test : test
},
exclude : ['excluded/excluded-*']
}, {
output_folder : false
}, function(err, results) {
test.ok(!('excluded-module' in results.modules));
test.done();
});
},

testRunAsync : function(test) {
test.expect(5);

Expand Down Expand Up @@ -74,10 +101,5 @@ module.exports = {
test.ok('demoTestMixed' in results.modules.sample);
test.done();
});
},

tearDown : function(callback) {
// clean up
callback();
}
};
10 changes: 5 additions & 5 deletions tests/src/testAssertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ module.exports = {
callback();
},

"Testing assertions loaded" : function(test) {
'Testing assertions loaded' : function(test) {
var assertModule = require('assert');
for (var prop in assertModule) {
var prop;
for (prop in assertModule) {
test.ok(prop in this.client.api.assert);
}
for (var prop in assertModule) {
for (prop in assertModule) {
test.ok(prop in this.client.api.verify);
}
test.ok('elementPresent' in this.client.api.assert);
Expand Down Expand Up @@ -56,5 +57,4 @@ module.exports = {
this.client = null;
callback();
}
}

};
18 changes: 16 additions & 2 deletions tests/src/testProtocolActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ module.exports = {
test.done();
});

test.equal(command.request.method, "POST");
test.equal(command.request.method, 'POST');
test.equal(command.request.path, '/wd/hub/session/1352110219202/doubleclick');
});
},
Expand Down Expand Up @@ -840,9 +840,23 @@ module.exports = {
});
},

testKeys : function(test) {
var protocol = this.protocol;

this.client.on('selenium:session_create', function() {
var command = protocol.keys(['A', 'B'], function callback() {
test.done();
});

test.equal(command.request.method, 'POST');
test.equal(command.data, '{"value":["A","B"]}');
test.equal(command.request.path, '/wd/hub/session/1352110219202/keys');
});
},

tearDown : function(callback) {
this.client = null;
// clean up
callback();
}
}
};
3 changes: 1 addition & 2 deletions tests/src/testQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ module.exports = {
CommandQueue.reset();
callback();
}
}

};
Loading

0 comments on commit 10039f2

Please sign in to comment.