Skip to content

Commit

Permalink
Resolved both issues reported in nightwatchjs#21
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Feb 7, 2014
1 parent cb42664 commit 4b63ac5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
74 changes: 43 additions & 31 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var util = require("util"),
var util = require('util'),
fs = require('fs'),
path = require('path'),
events = require("events"),
events = require('events'),
HttpRequest = require('./request.js'),
CommandQueue = require('./queue.js'),
Logger = require("./logger.js");
Logger = require('./logger.js');

function Nightwatch(options) {
events.EventEmitter.call(this);
Expand All @@ -17,12 +17,12 @@ function Nightwatch(options) {
this.terminated = false;

this.options.screenshots || (this.options.screenshots = {
"enabled" : false,
"path" : ""
'enabled' : false,
'path' : ''
});

this.options.output = this.options.output || typeof this.options.output == 'undefined';
this.screenshotPath = this.options.screenshotPath || "";
this.screenshotPath = this.options.screenshotPath || '';
this.isRunning = false;
this.launch_url = this.options.launch_url || null;

Expand All @@ -48,10 +48,10 @@ function Nightwatch(options) {
});
}
this.desiredCapabilities = {
browserName: "firefox",
browserName: 'firefox',
javascriptEnabled: true,
acceptSslCerts: true,
platform: "ANY"
platform: 'ANY'
};

if (this.options.desiredCapabilities) {
Expand Down Expand Up @@ -191,7 +191,7 @@ Nightwatch.prototype.loadCustomCommands = function() {
var self = this;

commandFiles.forEach(function(file) {
if (path.extname(file) == ".js") {
if (path.extname(file) == '.js') {
var command = require(path.join(absPath, file));
var name = path.basename(file, '.js');
self.addCommand(name, command.command, self, self);
Expand All @@ -213,18 +213,18 @@ Nightwatch.prototype.loadAssertions = function() {
var actual = arguments[0];

var message = typeof arguments[arguments.length-1] == 'string' &&
(arguments.length > 2 || typeof arguments[0] === "boolean") &&
(arguments.length > 2 || typeof arguments[0] === 'boolean') &&
arguments[arguments.length-1]
|| (typeof arguments[0] === "function" && '[Function]')
|| (typeof arguments[0] === 'function' && '[Function]')
|| '' + actual;

try {
assertModule[prop].apply(null, arguments);
passed = true;
message = "Assertion passed: " + message;
message = 'Assertion passed: ' + message;
} catch (ex) {
passed = false;
message = "Assertion failed: " + (ex.message || message);
message = 'Assertion failed: ' + (ex.message || message);
actual = ex.actual;
expected = ex.expected
}
Expand Down Expand Up @@ -252,7 +252,7 @@ Nightwatch.prototype.loadAssetionFiles = function(parent, abortOnFailure) {
}

for (var i = 0, len = commandFiles.length; i < len; i++) {
if (path.extname(commandFiles[i]) == ".js") {
if (path.extname(commandFiles[i]) == '.js') {
var commandName = path.basename(commandFiles[i], '.js');
var Module = require(path.join(__dirname, relativePath) + commandFiles[i]);
var m = new Module();
Expand All @@ -266,7 +266,7 @@ Nightwatch.prototype.addCommand = function(name, command, context, parent) {
parent = parent || this;
if (parent[name]) {
this.results.errors++;
var error = new Error("The command '" + name + "' is already defined!");
var error = new Error('The command "' + name + '" is already defined!');
this.errors.push(error.stack)
throw error;
}
Expand Down Expand Up @@ -300,18 +300,28 @@ Nightwatch.prototype.runProtocolCommand = function(requestOptions, callback) {
result = self.handleTestError(result);
if (screenshotContent && self.options.screenshots.enabled) {
var d = new Date();
var datestamp = d.toLocaleDateString().toLowerCase().replace(/\//g, '-') + '--' + d.toLocaleTimeString()
var datestamp = d.toLocaleString('en-GB', {
weekday: 'narrow',
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZoneName : 'short',
hour : '2-digit',
minute : '2-digit',
second : '2-digit',
era : 'short'
}).replace(/\:/g,'').replace(/\s/g,'-').replace(/-\(.+?\)/,'');
var fileName = path.join(self.options.screenshots.path, 'ERROR_' + datestamp + '.png');

self.saveScreenshotToFile(fileName, screenshotContent)
self.saveScreenshotToFile(fileName, screenshotContent);
}

request.emit('result', result, response);
})
.on('result', function(result) {
if (callback) {
try {
if (typeof callback != "function") {
if (typeof callback != 'function') {
var error = new Error('Callback parameter is not a function - ' + typeof(callback) + ' passed: "' + callback + '"');
this.errors.push(error.stack)
this.results.errors++;
Expand Down Expand Up @@ -352,7 +362,7 @@ Nightwatch.prototype.executeCommand = function(command) {

Nightwatch.prototype.getElement = function(using, value, callback) {
var self = this;
return this.runCommand("element", [using, value, function(result) {
return this.runCommand('element', [using, value, function(result) {
if (result.status == 0) {
callback.call(self, result.value.ELEMENT, result);
} else {
Expand All @@ -366,7 +376,7 @@ Nightwatch.prototype.assertion = function(passed, receivedValue, expectedValue,

if (passed) {
if (this.options.output) {
console.log(Logger.colors.green("✔") + " " + message);
console.log(Logger.colors.green('✔') + ' ' + message);
}
this.results.passed++;
} else {
Expand All @@ -378,9 +388,9 @@ Nightwatch.prototype.assertion = function(passed, receivedValue, expectedValue,
Error.captureStackTrace(err, arguments.callee);
throw err;
} catch (ex) {
var logged = Logger.colors.red("✖") + " " + message;
if (typeof expectedValue != "undefined" && typeof receivedValue != "undefined") {
logged += " " + Logger.colors.white(' - expected "' + Logger.colors.green(expectedValue)) + '" but got: ' + Logger.colors.red(receivedValue);
var logged = Logger.colors.red('✖') + ' ' + message;
if (typeof expectedValue != 'undefined' && typeof receivedValue != 'undefined') {
logged += ' ' + Logger.colors.white(' - expected "' + Logger.colors.green(expectedValue)) + '" but got: ' + Logger.colors.red(receivedValue);
}

if (this.options.output) {
Expand All @@ -404,9 +414,11 @@ Nightwatch.prototype.assertion = function(passed, receivedValue, expectedValue,
};

Nightwatch.prototype.saveScreenshotToFile = function(fileName, content) {
fs.writeFile(fileName, content, "base64", function(err) {
if (err) {
Logger.error(err);
fs.writeFile(fileName, content, 'base64', function(err) {

if (err) {
console.log(Logger.colors.yellow('Couldn\'t save screenshot to '), fileName);
Logger.warn(err);
}
});
};
Expand All @@ -415,7 +427,7 @@ Nightwatch.prototype.handleTestError = function(result) {
var errorMessage = '';
if (result && result.status) {
var errorCodes = require('./selenium/errors.json');
errorMessage = errorCodes[result.status].message || "";
errorMessage = errorCodes[result.status].message || '';
}

var rv = {
Expand All @@ -431,7 +443,7 @@ Nightwatch.prototype.handleTestError = function(result) {
Nightwatch.prototype.startSession = function() {
var self = this;
var request = new HttpRequest({
path : "/session",
path : '/session',
data : {
desiredCapabilities : this.desiredCapabilities,
sessionId : null
Expand All @@ -441,14 +453,14 @@ Nightwatch.prototype.startSession = function() {
request.on('success', function(data, response) {
if (data.sessionId) {
self.sessionId = data.sessionId;
Logger.info("Got sessionId from selenium", self.sessionId);
Logger.info('Got sessionId from selenium', self.sessionId);
self.emit('selenium:session_create', self.sessionId, request, response);
} else {
Logger.warn("Couldn't retrieve a new session from selenium server.");
Logger.warn('Couldn\'t retrieve a new session from selenium server.');
}
})
.on('error', function(data, err) {
console.error(Logger.colors.red("Connection refused!"), "Is selenium server started?");
console.error(Logger.colors.red('Connection refused!'), 'Is selenium server started?');
self.emit('error', data, err);
})
.send();
Expand Down
2 changes: 1 addition & 1 deletion runner/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ module.exports = new (function() {

var modulePath = fullpaths.shift();
var module = require(modulePath);
var moduleName = modulePath.split('/').pop();
var moduleName = modulePath.split(path.sep).pop();
globalresults.modules[moduleName] = [];
console.log('\n' + Logger.colors.cyan('[ ' + moduleName + ' module ]'));

Expand Down

0 comments on commit 4b63ac5

Please sign in to comment.