1

I am learning protractor and it has thus far been a wild journey because I am also pretty new to Javascript. I learned so far that protractor queues all promises and they can be executed using then(). However, I am now trying to use a filter() on an ElementArrayFinder but it doesn't seem to execute. Only when I preprend it with the return-keyword, the filter get's executed, but then I leave my function and I don't want that. Can someone help me in understanding this please?

Below my code:

it('Select service', function() {
 servicePage.services.filter(function(elem, index) {
      return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
  });
servicePage.services.get(0).element(by.tagName('input')).click(); 
});

When running above, the console log is not performed so I guess the filter function is not being executed. When I do it like below, the filter is executed but then the click() is not performed.

it('Select service', function() {
 return servicePage.services.filter(function(elem, index) {
      return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
  });
servicePage.services.get(0).element(by.tagName('input')).click(); 
});

Example3:

    it('Select service', function() {
      servicePage.services.filter(function(elem, index) {
        return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
  }).first().element(by.tagName('input')).click();
});

Thanks in advance! Regards

1 Answer 1

2

You should catch the element that filter function returns and then perform action on it. filter() function returns elements that match the criteria you specify in the function. In your case its returning an element that has a class attribute service passive. If there are more than one elements with same attribute, then you probably have to chain get() function to perform operation on the required element. Here's how -

servicePage.services.filter(function(elem, index) {
      return elem.getAttribute('class').then(function(attribute) {
          console.log('*****' + attribute);
          return attribute === 'service passive';
      });
}).element(by.tagName('input')).click(); //if only one element is returning, click on it

OR replace the last line with below line when there are more elements that match the criteria -

}).get(1).element(by.tagName('input')).click(); //click the second element in the array of elements returned

Hope it helps.

Sign up to request clarification or add additional context in comments.

6 Comments

I am not sure if that is correct. I tried both your suggestions and nothing is logged to the console so I suspect the filter function is not executed.
The element doesn't get clicked.
@Homewrecker can you update your question with your html code? or confirm that the locator you are using is proper and returns multiple elements? Thanks
The locator is correct because when I use the example 3 above, the attribute gets printed to the console, however I get an error stating that the index is out of bound.
@Homewrecker it throws index out of bound exception because you are using first() on the returned element and there are no elements returned with class service passive. Can you verify if the class value has exactly that value and no other classes attached to that element? Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.