3

I'm looking to set some data dynamically during testing using browser.executescript. Something like:

var x;
browser.executeScript(function () {
  var something = x;
});

But x seems to be out of the scope of the function being run. Is there a way for me to pass arguments that will be in the inner scope?

Any help greatly appreciated C

2 Answers 2

11

Pass the arguments inside arguments:

Any arguments provided in addition to the script will be included as script arguments and may be referenced using the arguments object. Arguments may be a boolean, number, string, or webdriver.WebElement. Arrays and objects may also be used as script arguments as long as each item adheres to the types previously mentioned.

var x;
browser.executeScript(function (arguments) {
  var something = arguments[0];
}, x);
Sign up to request clarification or add additional context in comments.

Comments

2

In addition to @alecxe's answer, if you want more readable code, and you can support es6 this seems like a nice option. More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

var my, params, go, here;
browser.executeScript(function ([my, params, go, here]) {
  var something = here;
}, [my, params, go, here]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.