Skip to content

Commit

Permalink
implemented sync method for multibrowser #nextLevelShit
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Jun 24, 2015
1 parent 84197a8 commit 6d4221d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ var remote = module.exports.remote = function remote(options, modifier) {
};

module.exports.multiremote = function multiremote(options) {
var clients = {},
multibrowser;
var multibrowser = new Multibrowser();

Object.keys(options).forEach(function(browserName) {
clients[browserName] = remote(options[browserName]);
multibrowser.addInstance(
browserName,
remote(options[browserName], multibrowser.getInstanceModifier())
);
});

multibrowser = remote(options, Multibrowser(clients));
return multibrowser;
}
return remote(options, multibrowser.getModifier());
};
82 changes: 74 additions & 8 deletions lib/multibrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ var q = require('q');
* Constructor
*/
function Multibrowser(instances) {
var browserNames = Object.keys(instances);
this.instances = {};
this.promiseBucket = [];
}

return function modifier(client) {
Multibrowser.prototype.addInstance = function(browserName, client) {
if(this.instances[browserName]) {
throw new Error('webdriver instance "' + browserName + '" is already defined');
}
this.instances[browserName] = client;
}

/**
* modifier for multibrowser instance
*/
Multibrowser.prototype.getModifier = function() {
var browserNames = Object.keys(this.instances),
multibrowser = this;

return function(client) {
client.next = function() {
var self = this,
promises = [],
Expand All @@ -20,9 +35,14 @@ function Multibrowser(instances) {
*/
args.shift();

/**
* flush promise bucket
*/
multibrowser.promiseBucket = [];

return this.lastPromise.done(function() {
browserNames.forEach(function(browserName) {
instance = instances[browserName];
instance = multibrowser.instances[browserName];
instance = instance[fnName].apply(instance, args[0]);
promises.push(instance.promise);
});
Expand All @@ -35,15 +55,61 @@ function Multibrowser(instances) {
};

client.select = function(browserName) {
if(browserNames.indexOf(browserName) === -1) {
throw new Error('browser name ' + browserName + ' was not defined');
var instance = multibrowser.instances[browserName];

if(!instance) {
throw new Error('browser name "' + browserName + '" was not defined');
}
return instances[browserName];

instance.isMultibrowser = false;
return instance;
};

return client;
client.sync = function() {
var bucket = multibrowser.flushPromiseBucket();

return this.call(function() {
return q.all(bucket);
});

};

return client;
};
}
};

/**
* flush bucket and return current pending promises
*/
Multibrowser.prototype.flushPromiseBucket = function() {
var bucket = this.promiseBucket.filter(function(promise) {
return promise.inspect().state === 'pending';
});
this.promiseBucket = [];
return bucket;
};

/**
* modifier for single webdriverio instances
*/
Multibrowser.prototype.getInstanceModifier = function() {
var multibrowser = this;

return function(client) {
var _next = client.next;

/**
* Overwrite next (bind) method to put each command into a bucket.
* This provides us useful information about all current running
* commands.
*/
client.next = function() {
multibrowser.promiseBucket.push(this.promise);
return _next.apply(this, arguments);
}

return client;
}
};

module.exports = Multibrowser;

0 comments on commit 6d4221d

Please sign in to comment.