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 evalexplicitly, but in similar fashion.

  1. the functions to be injected must be named function
  2. 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);
    
  3. 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
Ad