Skip to content

Commit

Permalink
exposed desiredCapabilities on the client api to satisfy #202
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Jul 18, 2014
1 parent 8c189de commit fb0d894
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
19 changes: 14 additions & 5 deletions lib/index.js
Expand Up @@ -59,6 +59,7 @@ Nightwatch.prototype.assertion = Assertion.assert;
Nightwatch.prototype.setOptions = function(options) {
var fs = require('fs');
this.options = {};
this.api.options = {};
if (options && typeof options == 'object') {
for (var propName in options) {
this.options[propName] = options[propName];
Expand All @@ -76,12 +77,14 @@ Nightwatch.prototype.setOptions = function(options) {
}

var screenshots = this.options.screenshots;
if (screenshots && screenshots.enabled) {
if (!screenshots.path || !fs.existsSync(screenshots.path)) {
throw new Error('A valid path for screenshots.path must be specified.');
}
var screenshotsEnabled = screenshots && screenshots.enabled || false;

this.api.screenshotsPath = screenshots.path;
this.api.options.screenshots = screenshotsEnabled;
if (screenshotsEnabled) {
if (!screenshots.path) {
throw new Error('Please specify the screenshots.path in nightwatch.json.');
}
this.api.screenshotsPath = this.api.options.screenshotsPath = screenshots.path;
} else {
this.options.screenshots = {
'enabled' : false,
Expand Down Expand Up @@ -117,12 +120,17 @@ Nightwatch.prototype.setOptions = function(options) {
var key = this.options.accesKey || this.options.access_key || this.options.password;

if (username && key) {
this.api.options.username = username;
this.api.options.accessKey = key;

HttpRequest.setCredentials({
username : username,
key : key
});
}



return this;
};

Expand All @@ -139,6 +147,7 @@ Nightwatch.prototype.setCapabilities = function() {
}
}
}
this.api.options.desiredCapabilities = this.desiredCapabilities;
return this;
};

Expand Down
23 changes: 16 additions & 7 deletions lib/runner/run.js
Expand Up @@ -23,13 +23,6 @@ module.exports = new (function() {

function runModule(module, opts, moduleName, moduleKey, moduleCallback, finishCallback) {
var client;
try {
client = Nightwatch.client(opts);
} catch (err) {
console.log(err.stack);
finishCallback(err, false);
return;
}
var keys = Object.keys(module);
var currentTest;
var setUp;
Expand Down Expand Up @@ -130,6 +123,22 @@ module.exports = new (function() {
}
};

try {
if (typeof module.desiredCapabilities == 'object') {
opts.desiredCapabilities = opts.desiredCapabilities || {};
for (var capability in module.desiredCapabilities) {
if (module.desiredCapabilities.hasOwnProperty(capability)) {
opts.desiredCapabilities[capability] = module.desiredCapabilities[capability];
}
}
}

client = Nightwatch.client(opts);
} catch (err) {
console.log(err.stack);
finishCallback(err, false);
return;
}
module.client = client.api;

if (module.disabled === true) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/logger.js
Expand Up @@ -34,7 +34,7 @@ function ConsoleColor() {
var self = this;
var mappings = {
blue : '0;34',
light_blue : '1;34',
light_blue : '1;34'
};
this.background = new Background();

Expand Down
9 changes: 9 additions & 0 deletions tests/mocks.json
Expand Up @@ -10,6 +10,15 @@
"statusCode" : 201,
"method": "POST"
},
{
"url" : "/wd/hub/session",
"postdata" : "{\"desiredCapabilities\":{\"browserName\":\"firefox\",\"javascriptEnabled\":true,\"acceptSslCerts\":true,\"platform\":\"ANY\",\"name\":\"test-Name\"}}",
"response" : "{\"status\": 0, \"sessionId\": \"1352110219202\", \"value\": { \"javascriptEnabled\": true, \"browserName\": \"firefox\", \"version\": \"TEST\", \"platform\": \"TEST\"}, \"state\": null}",
"responseHeaders" : {
},
"statusCode" : 201,
"method": "POST"
},
{
"url" : "/wd/hub/session",
"postdata" : "{\"desiredCapabilities\":{\"browserName\":\"chrome\",\"javascriptEnabled\":true,\"acceptSslCerts\":true,\"platform\":\"ANY\"}}",
Expand Down
5 changes: 5 additions & 0 deletions tests/sampletests/simple/sample.js
@@ -1,5 +1,10 @@
module.exports = {
desiredCapabilities : {
name : 'test-Name'
},
demoTest : function (client) {
client.globals.test.equals(client.options.desiredCapabilities.name, 'test-Name');

client.url('http://localhost')
.assert.elementPresent('#weblogin')
.end();
Expand Down
18 changes: 15 additions & 3 deletions tests/src/index/testNightwatchIndex.js
Expand Up @@ -172,6 +172,20 @@ module.exports = {
eq(client.api.launch_url, '/home');

eq(client.options.screenshots.enabled, false);
eq(client.api.options.screenshots, false);

test.done();
},

testSetOptionsCredentials : function(test) {
var client = this.client = Client.init({
username : 'test-user',
accesKey : 'test-access-key'
});
var eq = test.equals;

eq(client.api.options.username, 'test-user');
eq(client.api.options.accessKey, 'test-access-key');

test.done();
},
Expand All @@ -180,11 +194,9 @@ module.exports = {
test.throws(function() {
var client = this.client = Client.init({
screenshots : {
enabled : true,
path : '/screens'
enabled : true
}
});
test.equals(client.api.screenshotsPath, '/home');
});
test.done();
},
Expand Down

0 comments on commit fb0d894

Please sign in to comment.