Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #227 from pranavparikh/master
Browse files Browse the repository at this point in the history
Allowing to pass capabilities as JSON object, merging default capabilities with user defined capabilities
  • Loading branch information
proverma committed Apr 16, 2014
2 parents 852fae4 + d892607 commit 0937cbc
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 87 deletions.
8 changes: 4 additions & 4 deletions lib/session/testexecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var ReportManager = require("../util/reportmanager");
var WdSession = require("../session/wdsession");
var TestSession = require("../session/testsession");
var ProxyManager = require("../proxy/proxymanager");
var clone = require('clone');
var _ = require('underscore');
var path = require("path");
var os = require("os");
var async = require("async");
Expand Down Expand Up @@ -223,7 +223,7 @@ TestExecutor.prototype.setupTestQueues = function(callback) {
for (i = 0; i < wdSessions.length; i += 1) {
testQueues[i] = {curIndex: 0, sessions: []};
for (j = 0; j < tests.length; j += 1) {
test = clone(tests[j]);
test = _.clone(tests[j]);

// If proxy set to true or reuseSession set to false , ignore reuseSession
if (test.startProxyServer === true || test.reuseSession === false) {
Expand All @@ -244,7 +244,7 @@ TestExecutor.prototype.setupTestQueues = function(callback) {
for (i = 0; i < tests.length; i += 1) {
browsers = sf.getBrowsers(tests[i]);
for (j = 0; j < browsers.length; j += 1) {
test = clone(tests[i]);
test = _.clone(tests[i]);
test.browser = browsers[j];
testQueues[0].sessions.push(new TestSession(sf.config, test));
incrementTestSessionCount(test);
Expand Down Expand Up @@ -273,7 +273,7 @@ TestExecutor.prototype.getTests = function(callback) {
}
} else if (sf.tests) {
for (i = 0; i < sf.tests.length; i += 1) {
testParams = clone(sf.args);
testParams = _.clone(sf.args);
testParams.test = sf.tests[i];
// convert the cmd line test to look like a descriptor test
test = {
Expand Down
199 changes: 154 additions & 45 deletions lib/util/capabilitymanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

var log4js = require("log4js");
var fs = require("fs");
var clone = require("clone");
var _ = require("underscore");

function CapabilityManager() {
this.logger = log4js.getLogger("CapabilityManager");
}

CapabilityManager.prototype.getCapability = function (capJsonPath, capName) {
/**
*
* @param caps - JSON blob or path to capabilities file
* @param capName - key to the caps object ,passed as browser from command line
* @returns {*}
*/
CapabilityManager.prototype.getCapability = function (caps, capName) {

var capJson,
cap,
Expand All @@ -24,8 +30,8 @@ CapabilityManager.prototype.getCapability = function (capJsonPath, capName) {

try {

capJson = JSON.parse(fs.readFileSync(capJsonPath, "utf-8"));
cap = clone(capJson.capabilities[capName]);
capJson = self.getCapsJSON(caps);
cap = _.clone(capJson.capabilities[capName]);
commonCap = capJson.common_capabilities;
if (cap) {
for (k in commonCap) {
Expand All @@ -35,47 +41,157 @@ CapabilityManager.prototype.getCapability = function (capJsonPath, capName) {
}

} catch (e) {
self.logger.error('Error in parsing capabilities json -' + capJsonPath);
self.logger.error('Error in parsing capabilities json -' + caps);

}

return null;

};

/**
*
* @param caps - Can be a JSON blob or filePath for capabilities file
* @returns {*} - JSON capabilities object
*/
CapabilityManager.prototype.getCapsJSON = function(caps) {

var capJson,
self = this;

if (caps) {

// If caps is JSON blob
try {
capJson = JSON.parse(caps);
return capJson;
}
catch(e) {
self.logger.debug('Error in parsing caps. It might be JSON file');
}

// If caps is file path containing JSON object
try {
capJson = JSON.parse(fs.readFileSync(caps, "utf-8"));
return capJson;
}
catch(e) {
self.logger.debug('Error in parsing capabilities JSON -' + caps);
}
}

return null;


};

/**
*
* @param args
* @param config
* @returns {{platform: string, javascriptEnabled: boolean, seleniumProtocol: string}}
*/
CapabilityManager.prototype.getCapabilities = function (args, config) {

var self = this,
caps = {
"platform": "ANY",
"javascriptEnabled": true,
"seleniumProtocol": "WebDriver"
},
tmpCaps,
browserInfo,
versionKey,
cm,
isProxyTest = false,
browserName,
browserVersion,
testName = args.testName;
};

caps.browserName = args["browser"] || config["browser"];
if (!caps.browserName) {
caps.error = "Browser is not specified";
return caps;
}

// Set proxy capabilities
caps = self.setProxyCaps(caps, args);

// Set browser capabilities
caps = self.setBrowserCaps(caps, args, config);

// Set mobile browser capabilities
caps = self.setMobileCaps(caps);

// Process user capabilities
caps = self.processUserCapabilities(caps, config);

self.logger.debug('To run the test ' + args.testName + ' with browser:' + caps.browserName + ',version:' + caps.version);

return caps;

};

/**
* Set device and app in capabilities for mobile browsers
*/
CapabilityManager.prototype.processUserCapabilities = function(caps, config) {

var cm,
tmpCaps;
// if the user has passed capabilities as json
if (config.capabilities) {
cm = new CapabilityManager();
tmpCaps = cm.getCapability(config.capabilities, caps.browserName);
if (tmpCaps === null) {
caps.error = "No related capability for " + caps.browserName + " in " + config.capabilities;
return caps;
}

// Copy over tmpCaps to caps. Will over ride any default properties in caps and copy the extra ones
_.extend(caps,tmpCaps);
}

return caps;

};

/**
* Set device and app in capabilities for mobile browsers
*/
CapabilityManager.prototype.setMobileCaps = function(caps) {

var browserName = caps.browserName.toLowerCase();

//extra properties required for appium
//ios case
if (browserName === "iphone" || browserName === "ipad") {
caps.device = browserName;
caps.app = "safari";

// Remove version property if set to latest
if (caps.version === "latest") {
delete caps.version;
}

//android case
} else if (browserName === "android" || browserName === "androidtablet") {
caps.device = browserName;
caps.app = "browser";
}
return caps;


};

/**
* Set capabilities for proxy
* @param args
* @param caps
* @returns {*}
*/
CapabilityManager.prototype.setProxyCaps = function (caps, args) {

var self = this;
// For tests needing proxy server, set proxy flag
if (args.proxyUrl) {

if (args.proxy || args.proxy === undefined) {

isProxyTest = true;

// Add settings for proxy
self.logger.debug("Adding Proxy Setting to the Browser for the test " + testName);
self.logger.debug("Adding Proxy Setting to the Browser for the test " + args.testName);
caps.proxy = {
"httpProxy": args.proxyUrl,
"sslProxy": args.proxyUrl,
Expand All @@ -86,6 +202,23 @@ CapabilityManager.prototype.getCapabilities = function (args, config) {
self.logger.debug("Descriptor overridden proxy param. Not setting proxy in browser");
}
}
return caps;

};

/**
* Set browserName and version
* @param caps
* @param config
* @param args
*/
CapabilityManager.prototype.setBrowserCaps = function (caps, args, config) {

var browserInfo,
browserName,
browserVersion,
versionKey,
self = this;

browserInfo = caps.browserName.split("-", 2);

Expand All @@ -102,43 +235,19 @@ CapabilityManager.prototype.getCapabilities = function (args, config) {
}

// If browser is reuse and its a proxy test, set browser to firefox
if (caps.browserName === "reuse" && isProxyTest) {
if (caps.browserName === "reuse" && caps.proxy) {
caps.browserName = "firefox";
caps.version = "latest";
self.logger.debug(testName + " is a proxy test so can't use reuse. Setting browser to firefox.");
self.logger.debug(args.testName + " is a proxy test so can't use reuse. Setting browser to firefox.");
} else {
caps.browserName = browserName;
caps.version = browserVersion;
}

// if the user has passed capabilities as json
if (config.capabilities) {
cm = new CapabilityManager();
tmpCaps = cm.getCapability(config.capabilities, caps.browserName);
if (tmpCaps === null) {
caps.error = "No related capability for " + caps.browserName + " in " + config.capabilities;
return caps;
}
caps = tmpCaps;
}

self.logger.debug('To run the test ' + testName + ' with browser:' + caps.browserName + ',version:' + caps.version);

browserName = caps.browserName.toLowerCase();

//extra properties required for appium
//ios case
if (browserName === "iphone" || browserName === "ipad") {
caps.device = browserName;
caps.app = "safari";
//android case
} else if (browserName === "android" || browserName === "androidtablet") {
caps.device = browserName;
caps.app = "browser";
}

return caps;

};



module.exports = CapabilityManager;
6 changes: 3 additions & 3 deletions npm-shrinkwrap.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"yui": "3.8.0",
"JSV": "4.0.2",
"log4js": "0.5.1",
"clone": "0.1.0",
"useragent": "1.1.0",
"istanbul": "0.1.25",
"uglify-js": "1.3.4",
Expand All @@ -57,7 +56,8 @@
"async": "~0.2.5",
"xml2js": "0.1.14",
"mkdirp": "0.3.5",
"selenium-webdriver": "2.39.0"
"selenium-webdriver": "2.39.0",
"underscore": "1.6.0"
},
"optionalDependencies": {
"mocha": "1.7.4",
Expand Down
Loading

0 comments on commit 0937cbc

Please sign in to comment.