Nick Tomlin's answer is what you can do if a module returns serializable data structure as a value. You call require
and call with the module's value the callback that executeAsyncScript
gives you to allow returning asynchronous values. This will work, for instance, if your module returns "foo"
or { foo: 'bar' }
or structures that are generally serializable.
However, it won't always work. Complex modules cannot be retrieved that way. Roughly speaking you should expect what you send through executeScript
and executeAsyncScript
and what they return to have the same limitations as JSON.stringify
does. One major exception is that Selenium will wrap DOM objects returned from these calls into a structure that allows to identify them on the script side, and that allows passing them back to the browser. (Then again, there are limitations there too. This is why you get stale element exceptions, for instance.)
If you try to retrieve modules that export functions, you'll probably get something but it won't be complete. Try this, for instance:
browser.executeAsyncScript(function () {
arguments[0]({ foo: function () {}});
}).then(function (value) {
console.log(value);
});
The output I get is:
Object { foo: Object {} }
The function has been turned into an empty object.