0

I was trying to loop, compare based on a text and if matched click on the element.

I stumbled over the below post which was very helpful

Passing Protractor ElementFinder to deferred.fulfill() results in a promise containing a null value

Based on the answer provided in the above post, I implemented this

  element.all(by.css('.classname')).map(function (elm, index) {

        return {
             elm: elm,
            text: elm.getText(),
            index: index

        };
    }).then(function (list) {

        for (var i = 0; i < list.length; i++) {
            if (list[i].text === 'DS_Emulator') {

                return list[i].elm;
            }
        }
        throw new error('Text not found');
    }).then(function (elm) {

          elm.click();           

        });

    });

This made application exit out without any error. This is happening due to elm: elm in map.

if I modify my code as below, the application works fine.

 element.all(by.css('.classname')).map(function (elm, index) {

        return {
           // elm: elm,
            text: elm.getText(),
            index: index

        };
    }).then(function (list) {

        for (var i = 0; i < list.length; i++) {
            if (list[i].text === 'DS_Emulator') {

                return list[i].index;

              //  return list[i].elm;
            }
        }
        throw new error('Text not found');
        }).then(function (elm) {

          //  elm.click();

            element.all(by.css('.classname')).then(function (items) {

                items[elm].click();

            });

    });

Please help me understand where I am going wrong.

Community
  • 1
  • 1
purplized
  • 61
  • 1
  • 7
  • 3
    If you need to find a specific element based on text, you should be using `filter()` not `map()` – Gunderson Apr 06 '17 at 13:32
  • Check the documentation. The example provided is similar to what you are looking for -- http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter – Grasshopper Apr 06 '17 at 14:16
  • @Gunderson Thank u. I used filter() and it worked perfectly. But can you explain if elm: elm i'm doing in map used in my earlier code is wrong? Why is the application exiting? – purplized Apr 06 '17 at 14:30
  • I can take a stab at it, but I'm not positive. I tried in my app and it crashed too, for me it seems to be related to returning too many (large) objects and the JS heap overloads. But I'm not sure why you would want to return this ElementFinder object in your mapped object anyway – Gunderson Apr 06 '17 at 16:33
  • @Gunderson I switched to using filter. Was curious to know why my application is exiting without throwing an error. Large object explanation makes total sense. Thank u once again. – purplized Apr 06 '17 at 17:23

0 Answers0