Skip to content

Commit

Permalink
Merge branch 'features/saucelabs-support'
Browse files Browse the repository at this point in the history
* features/saucelabs-support:
  updated sample setting.json file with an example for saucelabs configuration
  added support for connecting to selenium servers which require credentials and support for connecting to servers using https
  Hacking in the buttonDown/buttonUp protocols, thus enabling Dragand-Drop.
  • Loading branch information
beatfactor committed Feb 1, 2014
2 parents 3f7747f + fc8a4dd commit 6706867
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 69 deletions.
4 changes: 3 additions & 1 deletion bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ try {
output_folder : output_folder,
selenium : (settings.selenium || null)
}, function() {
runner.stopSelenium();
if (settings.selenium && settings.selenium.start_process) {
runner.stopSelenium();
}
});
});
}
Expand Down
22 changes: 20 additions & 2 deletions bin/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"src_folders" : ["node_modules/nightwatch/examples/tests"],
"output_folder" : "node_modules/nightwatch/examples/reports",
"src_folders" : ["./examples/tests"],
"output_folder" : "./examples/reports",
"custom_commands_path" : "",

"selenium" : {
Expand Down Expand Up @@ -28,6 +28,24 @@
"javascriptEnabled": true,
"acceptSslCerts": true
}
},

"saucelabs" : {
"selenium_host" : "ondemand.saucelabs.com",
"selenium_port" : 80,
"username" : "...",
"access_key" : "...",
"use_ssl" : false,
"silent": false,
"output": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
}
}
}
}
37 changes: 22 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function Nightwatch(options) {
"path" : ""
});

this.options.output =
typeof options == 'undefined' ||
typeof options.output == 'undefined' || options.output;
this.options.output = this.options.output || typeof this.options.output == 'undefined';
this.screenshotPath = this.options.screenshotPath || "";
this.isRunning = false;
this.launch_url = this.options.launch_url || null;
Expand All @@ -34,16 +32,25 @@ function Nightwatch(options) {
Logger.enable();
}

if (this.options.port) {
HttpRequest.setSeleniumPort(this.options.port);
if (this.options.selenium_port) {
HttpRequest.setSeleniumPort(this.options.selenium_port);
}
if (this.options.host) {
HttpRequest.setSeleniumHost(this.options.host);
if (this.options.selenium_host) {
HttpRequest.setSeleniumHost(this.options.selenium_host);
}
if (this.options.use_ssl) {
HttpRequest.useSSL(true);
}
if (this.options.username && this.options.access_key) {
HttpRequest.setCredentials({
username : this.options.username,
key : this.options.access_key
});
}
this.desiredCapabilities = {
browserName: "firefox",
version: "",
javascriptEnabled: true,
acceptSslCerts: true,
platform: "ANY"
};

Expand Down Expand Up @@ -432,13 +439,13 @@ 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);
self.emit('selenium:session_create', self.sessionId);
} else {
Logger.warn("Couldn't retrieve a new session from selenium server.");
}
if (data.sessionId) {
self.sessionId = data.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.");
}
})
.on('error', function(err) {
console.error(Logger.colors.red("Connection refused!"), "Is selenium server started?", err);
Expand Down
27 changes: 21 additions & 6 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ var util = require("util"),
events = require("events"),
qs = require('querystring'),
http = require('http'),
https = require('https'),
Logger = require('./logger');

var Settings = {
selenium_host : "localhost",
selenium_port : 4444,
default_path : "/wd/hub"
default_path : "/wd/hub",
credentials : null,
use_ssl : false
}
var DO_NOT_LOG_ERRORS = [
'Unable to locate element'
Expand All @@ -25,26 +28,32 @@ util.inherits(HttpRequest, events.EventEmitter);
HttpRequest.prototype.createOptions = function(options) {
var reqOptions = {
host : options.host || Settings.selenium_host,
port : options.port || Settings.selenium_port,
port : options.selenium_port || Settings.selenium_port,
method: options.method || "POST",
path : Settings.default_path + (options.path || "")
};

if (options.sessionId) {
reqOptions.path = reqOptions.path.replace(':sessionId', options.sessionId);
}

reqOptions.headers = {
'content-type': 'application/json',
'content-length': this.data.length
'Content-Type': 'application/json',
'Content-Length': this.data.length
}

if (Settings.credentials &&
Settings.credentials.username && Settings.credentials.key
) {
var authHeader = new Buffer(Settings.credentials.username + ":" + Settings.credentials.key).toString('base64');
reqOptions.headers['Authorization'] = "Basic " + authHeader;
}

return reqOptions;
}

HttpRequest.prototype.send = function() {
var self = this;
this.request = http.request(this.requestOptions, function (response) {
this.request = (Settings.use_ssl ? https: http).request(this.requestOptions, function (response) {
response.setEncoding('utf8');

if (response.statusCode === 302 || response.statusCode === 304) {
Expand Down Expand Up @@ -122,9 +131,15 @@ HttpRequest.prototype.send = function() {
HttpRequest.setSeleniumPort = function(port) {
Settings.selenium_port = port;
};
HttpRequest.useSSL = function(value) {
Settings.use_ssl = value;
};
HttpRequest.setSeleniumHost = function(host) {
Settings.selenium_host = host;
};
HttpRequest.setCredentials = function(credentials) {
Settings.credentials = credentials;
};

function parseResult(data) {
var result;
Expand Down
23 changes: 23 additions & 0 deletions lib/selenium/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ Actions.elementIdClick = function(id, callback) {
return postRequest.call(this, "/element/" + id + "/click", '', callback);
};

function buttonHandler(that, direction, id, button, callback) {
var bi;
if (typeof button == 'function') {
callback = button;
button = 0;
}
if (typeof button == 'string') {
bi = ['left', 'middle', 'right'].indexOf(button.toLowerCase());
if (bi !== -1) {
button = bi;
}
}
return postRequest.call(that, '/button' + direction, {button: button}, callback);
};

Actions.buttonDown = function(id, button, callback) {
return buttonHandler(this, 'down', id, button, callback);
};

Actions.buttonUp = function(id, button, callback) {
return buttonHandler(this, 'up', id, button, callback);
};

/**
* http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/css/:propertyName
*
Expand Down
4 changes: 2 additions & 2 deletions tests/mocks.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"port" : 10199,
"port" : 10195,
"mocks" : [
{
"url" : "/wd/hub/session",
"postdata" : "{\"desiredCapabilities\":{\"browserName\":\"firefox\",\"version\":\"\",\"javascriptEnabled\":true,\"platform\":\"ANY\"},\"sessionId\":null}",
"postdata" : "{\"desiredCapabilities\":{\"browserName\":\"firefox\",\"javascriptEnabled\":true,\"acceptSslCerts\":true,\"platform\":\"ANY\"},\"sessionId\":null}",
"response" : "{\"status\": 0, \"sessionId\": \"1352110219202\", \"value\": { \"javascriptEnabled\": true, \"browserName\": \"firefox\"}, \"state\": null}",
"responseHeaders" : {
},
Expand Down
7 changes: 5 additions & 2 deletions tests/nightwatch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
var nightwatch = require('../index.js');

module.exports = {
init : function() {
init : function(callback) {
return nightwatch.client({
port : 10199,
selenium_port : 10195,
silent : true,
output : false
}).start().once('error', function() {
if (callback) {
callback();
}
process.exit();
})
}
Expand Down
55 changes: 27 additions & 28 deletions tests/node_modules/mockserver.js

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

9 changes: 2 additions & 7 deletions tests/src/testAssertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ var CommandQueue = require('../../lib/queue.js');

module.exports = {
setUp: function (callback) {
try {
this.server = require('mockserver').init();
} catch(ex) {
console.log(ex.stack)
}

this.client = require('../nightwatch.js').init();
this.server = require('mockserver').init();
this.client = require('../nightwatch.js').init(callback);

callback();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var CommandQueue = require('../../lib/queue');

module.exports = {
setUp: function (callback) {
this.server = require('mockserver').init();
Expand All @@ -8,7 +6,7 @@ module.exports = {
callback();
},

"Test initialization of WebdriverJS" : function(test) {
"Test initialization" : function(test) {
var self = this;

this.client.on('selenium:session_create', function(sessionId) {
Expand Down Expand Up @@ -44,7 +42,7 @@ module.exports = {
var request = client.runProtocolCommand({
host : "127.0.0.1",
path : "/test",
port : 10199
port : 10195
});

test.ok("send" in request);
Expand All @@ -68,7 +66,7 @@ module.exports = {
var request = client.runProtocolCommand({
host : "127.0.0.1",
path : "/test_error",
port : 10199
port : 10195
});

request.on('result', function(result) {
Expand Down
1 change: 0 additions & 1 deletion tests/src/testProtocolActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = {
setUp: function (callback) {
this.server = require('mockserver').init();
this.client = require('../nightwatch.js').init();

callback();
},

Expand Down
Loading

0 comments on commit 6706867

Please sign in to comment.