Ad
Selenium-webdriver Pass An Array Of Functions As Argument To ExecuteScript
import webdriver from 'selenium-webdriver';
const driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
driver.get('https://www.google.com');
let foo = function(rules) {
rules.forEach(rule => {
rule();
});
}
let bar = function() { return 'bar' };
let baz = function() { return 'baz' };
driver.executeScript(foo, [bar, baz]).then(function(result) {
console.log(result);
});
driver.quit();
it errors out with
WebDriverError: unknown error: rule is not a function
let foo = function(rules) {
return rules;
// rules.forEach(rule => {
// rule();
// });
}
let bar = function() { return 'bar' };
let baz = function() { return 'baz' };
driver.executeScript(foo, [bar, baz]).then(function(result) {
console.log(result); // refer the log pasted below
});
Looks like the function are being serialized a string
[ 'function bar() {\n return \'bar\';\n}',
'function baz() {\n return \'baz\';\n}' ]
Any pointer on how to pass array of functions as arguments
would be helpful.
Ad
Answer
I have found a different work around, w/o using eval
explicitly, but in similar fashion.
- the functions to be injected must be named function
inject the
function.toString()
as the content of a<script>
function foo(rules) { var result = []; rules.forEach(rule => { result.push(rule()); }); return result; } function bar() { return 'bar' }; function baz() { return 'baz' }; function inject(content) { var script = document.createElement('script'); script.innerHTML = content; document.head.appendChild(script); } let script = `${bar.toString()} ${baz.toString()} ${foo.toString()}`; driver.executeScript(inject, script);
then execute the desired function as
driver.executeScript('return foo([bar, baz])').then(function(result) { // use the result });
complete example
// example.js
import webdriver from 'selenium-webdriver';
const driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
driver.get('https://www.google.com');
function foo(rules) {
var result = [];
rules.forEach(rule => {
result.push(rule());
});
return result;
}
function bar() { return 'bar' };
function baz() { return 'baz' };
function inject(content) {
var script = document.createElement('script');
script.innerHTML = content;
document.head.appendChild(script);
}
let script = `${bar.toString()} ${baz.toString()} ${foo.toString()}`;
driver.executeScript(inject, script);
driver.executeScript('return foo([bar, baz])').then(functn
driver.executeScript('return foo([bar, baz])').then(function(result) {
console.log(result);
});
driver.quit();
> babel-node example.js
[ 'bar', 'baz' ]
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad