Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #98 from millermedeiros/983797-script-with
Browse files Browse the repository at this point in the history
Bug 983795 - Element#scriptWith should accept extra arguments r=gaye
  • Loading branch information
Gareth Aye committed Mar 14, 2014
2 parents 49bac73 + 7221f61 commit 7d3d724
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
9 changes: 7 additions & 2 deletions lib/marionette/element.js
Expand Up @@ -94,11 +94,16 @@
* a function with this element as first argument.
* @method scriptWith
* @param {Function|String} script remote script.
* @param {Array} [args] optional arguments for script.
* @param {Function} callback callback when script completes.
* @return {String|Number} return value of the script.
*/
scriptWith: function scriptWith(script, callback) {
return this.client.executeScript(script, [this], callback);
scriptWith: function scriptWith(script, args, callback) {
if (!Array.isArray(args)) {
callback = args;
args = [];
}
return this.client.executeScript(script, [this].concat(args), callback);
},

/**
Expand Down
8 changes: 4 additions & 4 deletions test/integration/element-test.js
Expand Up @@ -16,11 +16,11 @@ suite('element methods', function() {

test('#scriptWith', function() {
var element = client.findElement('html');
var evaled = element.scriptWith(function() {
return '111';
});
var evaled = element.scriptWith(function(el, arg) {
return el.tagName + ' ' + arg;
}, ['FTW!']);

assert.equal(evaled, '111');
assert.equal(evaled, 'HTML FTW!');
});

test('#findElement', function() {
Expand Down
28 changes: 25 additions & 3 deletions test/marionette/element-test.js
Expand Up @@ -109,17 +109,39 @@ suite('marionette/element', function() {
subject.client.executeScript = function() {
calledWith = arguments;
};

subject.scriptWith(fn, cb);
});

test('should call client.executeScript with' +
test('should call client.executeScript with ' +
'element as argument', function() {
subject.scriptWith(fn);

assert.strictEqual(calledWith[0], fn);
assert.deepEqual(calledWith[1], [
subject
]);
assert.strictEqual(calledWith[2], undefined);
});


test('should call client.executeScript with element ' +
' + callback', function(){
subject.scriptWith(fn, cb);

assert.strictEqual(calledWith[0], fn);
assert.deepEqual(calledWith[1], [
subject
]);
assert.strictEqual(calledWith[2], cb);
});

test('should call client.executeScript with element ' +
' + args + callback', function(){
subject.scriptWith(fn, ['foo', 'bar'], cb);

assert.strictEqual(calledWith[0], fn);
assert.deepEqual(calledWith[1], [
subject, 'foo', 'bar'
]);
assert.strictEqual(calledWith[2], cb);
});

Expand Down

0 comments on commit 7d3d724

Please sign in to comment.