Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

marionette-client

Package Overview
Dependencies
Maintainers
2
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

marionette-client - npm Package Compare versions

Comparing version 0.7.0 to 0.8.0-alpha

api-design/switching.js

236

lib/marionette/client.js

@@ -6,2 +6,9 @@ (function(module, ns) {

var SCOPE_TO_METHOD = {
scriptTimeout: 'setScriptTimeout',
searchTimeout: 'setSearchTimeout',
context: 'setContext'
};
var key;

@@ -23,2 +30,11 @@ var searchMethods = {

function setState(context, type, value) {
context._scope[type] = value;
context._state[type] = value;
}
function getState(context, type) {
return context._state[type];
}
/**

@@ -88,2 +104,15 @@ * Initializes client.

}
// create the initial state for this client
this._state = {
context: 'content',
scriptTimeout: 5000,
searchTimeout: 250
};
// give the root client a scope.
this._scope = {};
for (var key in this._state) {
this._scope[key] = this._state[key];
}
}

@@ -112,10 +141,35 @@

/**
* The initial context of the driver.
* The current scope of this client instance. Used with _state.
*
* @property context
* @type String
* // Example
* {
* scriptTimeout: 500,
* searchTimeout: 6000,
* context: 'content',
* window: 'window_id',
* frame: 'frameId'
* }
*
* @type {Object}
*/
context: 'content',
_scope: null,
/**
* The current state of the client.
*
* // Example
* {
* scriptTimeout: 500,
* searchTimeout: 6000,
* context: 'content',
* window: 'window_id',
* frame: 'frameId'
* }
*
* @private
* @type {Object}
*/
_state: null,
/**
* Actor id for instance

@@ -136,3 +190,40 @@ *

// _state getters
/**
* @return {String} the current context.
*/
get context() {
return getState(this, 'context');
},
/**
* @return {String|Marionette.Element} frame currently focused.
*/
get frame() {
return getState(this, 'frame');
},
/**
* @return {String|Marionette.Element}
*/
get window() {
return getState(this, 'window');
},
/**
* @return {Number} current scriptTimeout.
*/
get scriptTimeout() {
return getState(this, 'scriptTimeout');
},
/**
* @return {Number} current search timeout.
*/
get searchTimeout() {
return getState(this, 'searchTimeout');
},
/**
* Sends a command to the server.

@@ -145,5 +236,20 @@ * Adds additional information like actor and session

* @chainable
* @param {Object} cmd to be sent over the wire.
* @param {Function} cb executed when response is sent.
*/
send: function send(cmd, cb) {
// first do scoping updates
if (this._scope && this._bypassScopeChecks !== true) {
// really dirty hack
this._bypassScopeChecks = true;
for (var key in this._scope) {
// !important otherwise throws infinite loop
if (this._state[key] !== this._scope[key]) {
this[SCOPE_TO_METHOD[key]](this._scope[key]);
}
}
// undo really dirty hack
this._bypassScopeChecks = false;
}
if (!cmd.to) {

@@ -252,2 +358,118 @@ cmd.to = this.actor || 'root';

/**
* Creates a client which has a fixed window, frame, scriptTimeout and
* searchTimeout.
*
* var child = client.scope({ frame: myiframe });
* var chrome = client.scope({ context: 'chrome' });
*
* // executed in the given iframe in content
* child.setContext('content');
* child.findElement('...')
*
* // executed in the root frame in chrome context.
* chrome.executeScript();
*
*
* @method scope
* @param {Object} options for scopped client.
* @return {Marionette.Client} scoped client instance.
*/
scope: function(options) {
var scopeOptions = {};
for (var key in this._scope) {
scopeOptions[key] = this._scope[key];
}
// copy the given options
for (key in options) {
var value = options[key];
scopeOptions[key] = value;
}
// create child
var scope = Object.create(this);
// assign the new scoping
scope._scope = scopeOptions;
return scope;
},
/**
* Utility for waiting for a success condition to be met.
*
* // sync style
* client.waitFor(function() {
* return element.displayed();
* });
*
* // async style
* client.waitFor(function(done) {
* element.displayed(function(err, result) {
* done(err || result);
* });
* });
*
*
* Options:
* - (Number) interval: time between running test
* - (Number) timeout: maximum wallclock time before failing test.
*
* @method waitFor
* @param {Function} test to execute.
* @param {Object} [options] for timeout see above.
* @param {Function} [callback] optional callback.
*/
waitFor: function(test, options, callback) {
if (typeof(options) === 'function') {
callback = options;
options = null;
}
// setup options
options = options || {};
// must handle default callback case for sync code
callback = callback || this.defaultCallback;
// wallclock timer
var timeout = Date.now() + (options.timeout || 10000);
// interval between test being fired.
var interval = options.interval || 100;
// remember this runs on the host
function sleep(interval) {
setTimeout(marionetteScriptFinished, interval);
}
var runtest = function runtest() {
if (Date.now() >= timeout) {
return callback(new Error('waitFor timed out'));
}
// mocha style "done" argument
test(function(result) {
if (result instanceof Error)
return callback(result);
if (result)
return callback();
// test has failed retry
this.executeAsyncScript(sleep, [interval], runtest);
}.bind(this));
}.bind(this);
// when the test is sync
if (!test.length) {
var originalTest = test;
test = function(done) {
done(originalTest());
};
}
runtest();
},
/**
* Finds actor and creates connection to marionette.

@@ -370,3 +592,2 @@ * This is a combination of calling getMarionetteId and then newSession.

}
return this._sendCommand(cmd, 'ok', callback);

@@ -399,4 +620,3 @@ },

this.context = context;
setState(this, 'context', context);
var cmd = { type: 'setContext', value: context };

@@ -417,2 +637,3 @@ return this._sendCommand(cmd, 'ok', callback);

var cmd = { type: 'setScriptTimeout', value: timeout };
setState(this, 'scriptTimeout', timeout);
return this._sendCommand(cmd, 'ok', callback);

@@ -432,2 +653,3 @@ },

var cmd = { type: 'setSearchTimeout', value: timeout };
setState(this, 'searchTimeout', timeout);
return this._sendCommand(cmd, 'ok', callback);

@@ -434,0 +656,0 @@ },

5

package.json
{
"name": "marionette-client",
"version": "0.7.0",
"version": "0.8.0-alpha",
"main": "lib/marionette/index",

@@ -25,3 +25,4 @@ "description": "Marionette Javascript Client",

"test-agent": "~0.12",
"marionette-host-environment": "~0.3.0"
"marionette-host-environment": "~0.3.0",
"node-static": "~0.6"
},

@@ -28,0 +29,0 @@

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc