Chaoui05 Chaoui05 - 6 months ago 132
Perl Question

findIndex() method issue with internet explorer

I am doing some tests with differents browsers. I am using for that Selenium::Remote::Driver.
I would like to check if i find some item in my web site list , list from a framework js( which creates grids). Fo this case, i have to use Javascript snippet allowed by Selenium::Remote::Driver.
i did 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. My issue is that my code doesn't work with internet.explorer because findIndex() method is only supported by i.e 12 and following. And for some reasons i have to use internet.explorer 11.

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

Thanks

Answer

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:

    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;
        }
    }