diff --git a/bin/marionette-mocha b/bin/marionette-mocha index 8f02103..4488d79 100755 --- a/bin/marionette-mocha +++ b/bin/marionette-mocha @@ -31,6 +31,18 @@ function requireReporter(path) { } } +/** + * Helper to import the marionette desired capabilities + */ +function importDesiredCapabilities(path) { + try { + return require(path); + } catch (e) { + // Can not parse marionette desired capabilities file. + return; + } +} + /** Convert this host log argument into a stream (not done as a type to avoid destructive operations when calling --help) @@ -108,6 +120,12 @@ parser.addArgument(['--mocha-flags'], { action: 'storeTrue' }); +parser.addArgument(['--marionette-capabilities'], { + type: importDesiredCapabilities, + dest: 'desiredCapabilities', + help: 'Path tp marionette desired capabilities file (*.json)' +}); + var argv = ['test/', 'tests/', '.'].reduce(function(argv, path) { var optsFile = fsPath.join(path, OPTS_FILE); if (!fs.existsSync(optsFile)) { @@ -192,6 +210,8 @@ function main(argv) { profileBase: args[0].profileBase, + desiredCapabilities: args[0].desiredCapabilities, + hostLog: hostLogStream(args[0].hostLog), verbose: args[0].verbose diff --git a/lib/childrunner.js b/lib/childrunner.js index 91dc326..33fdf26 100644 --- a/lib/childrunner.js +++ b/lib/childrunner.js @@ -56,6 +56,9 @@ function ChildRunner(options) { // base details used in every profile this.profileBase = options.profileBase; + // optional marionette desired capabilities + this.desiredCapabilities = options.desiredCapabilities; + this._nextRemoteId = 1; this._verbose = !!options.verbose; this.remotes = {}; @@ -414,6 +417,15 @@ ChildRunner.prototype = { process.nextTick(next); }, + /** + * Encode the data in base64 + json + * @param {JSON} data Data to be encoded. + * @return {String} base64 encoded string + */ + _encode: function(data) { + return new Buffer(JSON.stringify(data)).toString('base64'); + }, + /** * Spawn the process for the mocha child runner. */ @@ -423,10 +435,8 @@ ChildRunner.prototype = { ['--reporter', PROXY_BINARY] ); - // encode the metadata in base64 + json - var metadata = new Buffer( - JSON.stringify(this.host.constructor.metadata) - ).toString('base64'); + // encode the metadata + var metadata = this._encode(this.host.constructor.metadata); // pass all environment variables to the child... var env = {}; @@ -444,6 +454,11 @@ ChildRunner.prototype = { // turn on the fork options so we get ipc messages. options.env[reporter.FORK_ENV] = 1; + if (this.desiredCapabilities) { + // Encode and pass desired capabilities to the child + options.env.DESIRED_CAPABILITIES = this._encode(this.desiredCapabilities); + } + this.process = fork(MOCHA_BINARY, argv, options); this.runner = new Consumer(this.process); diff --git a/lib/parentrunner.js b/lib/parentrunner.js index f46f875..953ae6a 100644 --- a/lib/parentrunner.js +++ b/lib/parentrunner.js @@ -65,7 +65,8 @@ ParentRunner.prototype = { profileBuilder: options.profileBuilder, profileBase: options.profileBase, verbose: options.verbose, - hostLog: options.hostLog + hostLog: options.hostLog, + desiredCapabilities: options.desiredCapabilities }); // keep track of all children- mostly for future use. diff --git a/lib/runtime/hostmanager.js b/lib/runtime/hostmanager.js index dd90f7b..f1fa8f2 100644 --- a/lib/runtime/hostmanager.js +++ b/lib/runtime/hostmanager.js @@ -42,6 +42,7 @@ HostManager.prototype = { createHost: function(profile, driver, desiredCapabilities) { profile = profile || {}; driver = driver || Marionette.Drivers.TcpSync; + desiredCapabilities = desiredCapabilities || this.desiredCapabilities; debug('create host', profile); diff --git a/lib/ui.js b/lib/ui.js index 29d4ee0..9fa043b 100644 --- a/lib/ui.js +++ b/lib/ui.js @@ -14,30 +14,28 @@ var Suite = require('mocha/lib/suite'), utils = require('mocha/lib/utils'); /** - * Internal method designed to attempt to find the metadata for this child - * process based on the environment variable CHILD_METADATA. + * Internal method designed to attempt to find the env data for this child + * process based on the environment variable. * * The environment variable is expected to be a base64 encoded string which can * be parsed as JSON. * + * @param {String} name environment variable name. * * @private * @return {Object} */ -function findMetadata() { - if (!process.env.CHILD_METADATA) +function decode(name) { + var data = process.env[name]; + if (!data) { return {}; - + } try { - return JSON.parse( - new Buffer(process.env.CHILD_METADATA, 'base64').toString() - ); + return JSON.parse(new Buffer(data, 'base64').toString()); } catch (e) { - console.error('could not parse CHILD_METADATA'); + console.error('could not parse ' + name); return {}; } - - return result; } /** @@ -101,9 +99,10 @@ module.exports = function(suite) { * * @type {Object} */ - context.marionette.metadata = findMetadata(); + context.marionette.metadata = decode('CHILD_METADATA'); // Setup global state manager for the marionette runtime. + manager.desiredCapabilities = decode('DESIRED_CAPABILITIES'); context.marionette._manager = manager; context.marionette.client = manager.createHost.bind(manager); context.marionette.plugin = manager.addPlugin.bind(manager); diff --git a/test/bin/fixtures/capabilities.json b/test/bin/fixtures/capabilities.json new file mode 100644 index 0000000..3862a62 --- /dev/null +++ b/test/bin/fixtures/capabilities.json @@ -0,0 +1,3 @@ +{ + "desiredCapability": true +} diff --git a/test/bin/marionette-mocha.js b/test/bin/marionette-mocha.js index 3f21fe7..8327bcd 100644 --- a/test/bin/marionette-mocha.js +++ b/test/bin/marionette-mocha.js @@ -143,6 +143,26 @@ suite('mocha integration', function() { }); }); + suite('--marionette-capabilities', function() { + var result, + capabilitiesPath = __dirname + '/fixtures/capabilities.json', + expected = JSON.stringify({ capabilities: require(capabilitiesPath) }), + argv = [ + '--marionette-capabilities', capabilitiesPath, + '--host-log', 'stdout', + __dirname + '/fixtures/marionettetest' + ]; + + setup(function(done) { + var proc = spawnMarionette(argv); + result = waitForProcess(proc, done); + }); + + test('desired capabilities are set', function() { + assert.ok(result.stdoutRaw.indexOf(expected) !== -1); + }); + }); + suite('--host-log=stdout', function() { var result, argv = [