Skip to content

Commit

Permalink
updated execute so it makes the args paramter optional and added foll…
Browse files Browse the repository at this point in the history
…ow redirects in an attempt to fix #106
  • Loading branch information
beatfactor committed Mar 28, 2014
1 parent 2652448 commit 1b559a6
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 60 deletions.
52 changes: 40 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ util.inherits(Nightwatch, events.EventEmitter);
Nightwatch.prototype.assertion = Assertion.assert;

Nightwatch.prototype.setOptions = function(options) {
this.options = options || {};
this.options = {};
if (options && typeof options == 'object') {
for (var propName in options) {
this.options[propName] = options[propName];
}
}

this.api.launchUrl = this.options.launchUrl || this.options.launch_url || null;
// backwords compatibility
this.api.launch_url = this.api.launchUrl;

if (this.options.globals) {
if (typeof this.options.globals == 'object') {
for (var globalKey in this.options.globals) {
this.api.globals[globalKey] = this.options.globals[globalKey];
}
if (this.options.globals && typeof this.options.globals == 'object') {
for (var globalKey in this.options.globals) {
this.api.globals[globalKey] = this.options.globals[globalKey];
}
}

Expand Down Expand Up @@ -130,7 +133,11 @@ Nightwatch.prototype.setOptions = function(options) {
};

Nightwatch.prototype.setCapabilities = function() {
this.desiredCapabilities = Nightwatch.DEFAULT_CAPABILITIES;
this.desiredCapabilities = {};
for (var capability in Nightwatch.DEFAULT_CAPABILITIES) {
this.desiredCapabilities[capability] = Nightwatch.DEFAULT_CAPABILITIES[capability];
}

if (this.options.desiredCapabilities) {
for (var prop in this.options.desiredCapabilities) {
if (this.options.desiredCapabilities.hasOwnProperty(prop)) {
Expand Down Expand Up @@ -333,24 +340,27 @@ Nightwatch.prototype.handleTestError = function(result) {
};
};

Nightwatch.prototype.startSession = function() {
Nightwatch.prototype.startSession = function () {
var self = this;
var request = new HttpRequest({
var options = {
path : '/session',
data : {
desiredCapabilities : this.desiredCapabilities,
sessionId : null
}
});
};

request.on('success', function(data, response) {
if (data.sessionId) {
var request = new HttpRequest(options);
request.on('success', function(data, response, isRedirect) {
if (data && data.sessionId) {
self.sessionId = self.api.sessionId = data.sessionId;
if (data.value) {
self.api.capabilities = data.value;
}
Logger.info('Got sessionId from selenium', self.sessionId);
self.emit('selenium:session_create', self.sessionId, request, response);
} else if (isRedirect) {
self.followRedirect(request, response);
} else {
Logger.warn('Couldn\'t retrieve a new session from selenium server.');
}
Expand All @@ -365,6 +375,24 @@ Nightwatch.prototype.startSession = function() {
return this;
};

Nightwatch.prototype.followRedirect = function (request, response) {
if (!response.headers || !response.headers.location) {
this.emit('error', null, null);
return this;
}
var url = require('url');
var urlParts = url.parse(response.headers.location);

request.setOptions({
path : urlParts.pathname,
host : urlParts.hostname,
port : urlParts.port,
method : 'GET'
}).send();

return this;
};

exports.client = function(options) {
return new Nightwatch(options);
};
Expand Down
33 changes: 19 additions & 14 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,32 @@ module.exports = (function() {

function HttpRequest(options) {
events.EventEmitter.call(this);
this.setOptions(options);
}

util.inherits(HttpRequest, events.EventEmitter);

HttpRequest.prototype.setOptions = function(options) {
this.data = options.data && JSON.stringify(options.data) || '';
this.contentLength = this.data.length;
this.reqOptions = this.createOptions(options);
this.request = null;
}

util.inherits(HttpRequest, events.EventEmitter);
return this;
};

HttpRequest.prototype.createOptions = function(options) {
var defaultPathPrefix = options.path && options.path.indexOf(Settings.default_path) === -1 ?
Settings.default_path : '';

var reqOptions = {
path : Settings.default_path + (options.path || ''),
path : defaultPathPrefix + (options.path || ''),
host : options.host || Settings.selenium_host,
port : options.selenium_port || Settings.selenium_port,
method : options.method || 'POST',
headers : {}
};
var requestMethod = reqOptions.method.toUpperCase();

if (options.sessionId) {
reqOptions.path = reqOptions.path.replace(':sessionId', options.sessionId);
}
Expand Down Expand Up @@ -69,17 +77,10 @@ module.exports = (function() {
this.request = (Settings.use_ssl ? https: http).request(this.reqOptions, function (response) {
response.setEncoding('utf8');

if (response.statusCode === 302 || response.statusCode === 304) {
if (isRedirect(response.statusCode)) {
Logger.info('Response ' + response.statusCode + ' ' + self.reqOptions.method + ' ' + self.reqOptions.path);
try {
self.emit('success', {}, response);
} catch (ex) {
console.log(ex.message);
console.log(ex.stack);
self.emit('error', {error:ex.message}, response);
}

self.emit('complete', response);
self.emit('success', {}, response, true);
//self.emit('complete', response);
return self;
}

Expand Down Expand Up @@ -164,6 +165,10 @@ module.exports = (function() {
///////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////
function isRedirect(statusCode) {
return [302, 303, 304].indexOf(statusCode) > -1;
}

function needsContentLengthHeader(requestMethod) {
return ['POST', 'DELETE'].indexOf(requestMethod) > -1;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/selenium/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ module.exports = function(Nightwatch) {
fn = script;
}

if ((arguments.length === 3) && (typeof arguments[2] === 'function')) {
if (arguments.length === 2) {
args = [];
} else if ((arguments.length === 3) && (typeof arguments[2] === 'function')) {
callback = arguments[2];
args = [];
}
Expand Down Expand Up @@ -530,10 +532,10 @@ module.exports = function(Nightwatch) {
* @returns {*} The script result.
*/
Actions.execute = function(body, args, callback) {
args = Array.prototype.slice.call(arguments, 0);
args.unshift('/execute');
var executeArgs = Array.prototype.slice.call(arguments, 0);
executeArgs.unshift('/execute');

return executeHandler.apply(null, args);
return executeHandler.apply(null, executeArgs);
};

/**
Expand Down
21 changes: 19 additions & 2 deletions tests/mocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,33 @@
"response" : "{\"status\": 0, \"sessionId\": \"1352110219202\", \"value\": { \"javascriptEnabled\": true, \"browserName\": \"firefox\"}, \"state\": null}",
"responseHeaders" : {
},
"statusCode" : 200,
"statusCode" : 201,
"method": "POST"
},
{
"url" : "/wd/hub/session",
"postdata" : "{\"desiredCapabilities\":{\"browserName\":\"chrome\",\"javascriptEnabled\":true,\"acceptSslCerts\":true,\"platform\":\"ANY\"},\"sessionId\":null}",
"responseHeaders" : {
"location" : "http://localhost:10195/wd/hub/session/1352110219202"
},
"statusCode" : 302,
"method": "POST"
},
{
"url" : "/wd/hub/session/1352110219202",
"response" : "{\"status\": 0, \"sessionId\": \"1352110219202\", \"value\": { \"javascriptEnabled\": true, \"browserName\": \"chrome\"}, \"state\": null}",
"responseHeaders" : {
},
"statusCode" : 201,
"method": "GET"
},
{
"url" : "/wd/hub/session/1352110219202",
"method": "DELETE"
},
{
"url" : "/wd/hub/session/1352110219202/url",
"postdata" : "{\"url\":\"http://localhost\"}"
"postdata" : "{\"url\":\"http://localhost\"}"
},
{
"url" : "/wd/hub/test",
Expand Down
14 changes: 11 additions & 3 deletions tests/nightwatch.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
var nightwatch = require('../index.js');

module.exports = {
init : function(callback) {
return nightwatch.client({
init : function(options, callback) {
var opts = {
seleniumPort : 10195,
silent : true,
output : false,
globals : {
myGlobal : 'test'
}
}).start().once('error', function() {
};

if (options) {
for (var prop in options) {
opts[prop] = options[prop];
}
}

return nightwatch.client(opts).start().once('error', function() {
if (callback) {
callback();
}
Expand Down
16 changes: 8 additions & 8 deletions tests/node_modules/mockserver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions tests/run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ process.chdir(__dirname);
try {
var server = require('mockserver').init();
server.on('listening', function() {
reporter.run(['src', 'src/assertions', 'src/protocol', 'src/commands'], options, function() {
reporter.run(['src', 'src/index', 'src/assertions', 'src/protocol', 'src/commands'], options, function() {
//reporter.run(['src/index'], options, function() {
server.close();
process.exit();
});
});


//reporter.run(['src/commands'], options);
} catch (err) {
console.log(e);
process.exit();
Expand Down
Loading

0 comments on commit 1b559a6

Please sign in to comment.