From 297090b3dcd1936c2fa6c45a1ddad7757757acc7 Mon Sep 17 00:00:00 2001 From: Hugh Troeger Date: Wed, 24 May 2017 21:39:59 -0400 Subject: [PATCH 1/2] feat: allow arguments to be passed to executeJS --- lib/tests-api/actions-builder.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tests-api/actions-builder.js b/lib/tests-api/actions-builder.js index 63c71cce2..d76376d7e 100644 --- a/lib/tests-api/actions-builder.js +++ b/lib/tests-api/actions-builder.js @@ -306,13 +306,13 @@ module.exports = inherit({ return this; }, - executeJS: function(callback) { + executeJS: function(callback, args) { if (typeof callback !== 'function') { throw new TypeError('executeJS argument should be function'); } this._pushAction(this.executeJS, function executeJS(browser) { - return browser.execute(serializeFunc(callback)); + return browser.execute(serializeFunc(callback), args); }); return this; }, @@ -392,7 +392,7 @@ function replaceStack(error, stack) { } function serializeFunc(func) { - return '(' + func.toString() + '(window));'; + return '(' + func.toString() + ').apply(null, [window].concat(Array.prototype.slice.call(arguments)));'; } function findElement(element, browser) { From 951dae226de170e2cfd699da6e7b369a006347aa Mon Sep 17 00:00:00 2001 From: Hugh Troeger Date: Wed, 24 May 2017 21:43:30 -0400 Subject: [PATCH 2/2] docs: executeJS will now accept arguments --- doc/tests.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/tests.md b/doc/tests.md index 4d7ce336b..72bf5f289 100644 --- a/doc/tests.md +++ b/doc/tests.md @@ -243,17 +243,17 @@ call: * `flick(offsets, speed, element)` — flick element with starting point at its center by `offsets.x` and `offset.y` offsets. -* `executeJS(function(window))` — run specified function in a browser. The - argument of a function is the browser's `window` object: +* `executeJS(function(window, [args]), [args])` — run specified function in a browser. The + argument of a function is the browser's `window` object and optional arguments: ```js - actions.executeJS(function(window) { - window.alert('Hello!'); - }); + actions.executeJS(function(window, name) { + window.alert('Hello!', name); + }, name); ``` Note that function is executed in a browser context, so any references to - outer scope of callback won't work. + outer scope of callback won't work unless passed via args. :warning: `window.scrollTo` does not work in Opera@12.16 (see [details](https://github.com/operasoftware/operaprestodriver/issues/108)).