findIndex() method issue with internet explorer

Chaoui05 picture Chaoui05 · Jun 8, 2016 · Viewed 36.5k times · Source

I am doing some tests with differents browsers using the Selenium::Remote::Driver module.

I would like to check if I find some item in my web site list, list from a framework JavaScript (which creates grids). For this case I have to use JavaScript snippet allowed by Selenium::Remote::Driver.

I wrote the following code

$script = q{

      var paramProgramName = arguments[0];

      var list  = $('#c-list').dxList('instance');
      var items = list.option('items');
      var index = items.findIndex(function(el){ return el.name == paramProgramName; });

      list.selectItem(index);

      return ;
};

$driver->execute_script($script, $programName);

It works fine with Chrome and Firefox but not with Internet Explorer because the findIndex method is only supported by version 12 and following. For some reason I have to use version 11.

What can I do differently to get an index from every browser?

Answer

T.J. Crowder picture T.J. Crowder · Jun 8, 2016

So my question is how can i do differently to get my index for every browser ?

You have at least three options:

  1. Shim Array#findIndex; MDN has a shim/polyfill you can use.

  2. Use something else that IE11 has, such as Array#some (which even IE9 has):

    var index = -1;
    items.some(function(el, i) {
        if (el.name == paramProgramName) {
            index = i;
            return true;
        }
    });
    
  3. Use something else that even IE8 has, such as for:

    var index = -1;
    for (var i = 0; i < items.length; ++i) {
        if (items[i].name == paramProgramName) {
            index = i;
            break;
        }
    }