2

In protractor I would like to assert that all elements are displayed that have a particular css class. But syntax like this doesn't work.

expect(element.all(by.className('bill')).each(x => x.isDisplayed().toBeTruthy());

What's the protractor way of achieving this goal? Is this it?

let els = element.all(by.className('bill'));
els.then(x => {
  x.forEach(y => {
    expect(y.isDisplayed()).toBeTruthy();
  })
});

This works, but seems overly complex.

IanT8
  • 2,167
  • 2
  • 23
  • 38

2 Answers2

2

You could first convert the items to an array of booleans with map and then assert that the array doesn't contain false:

var elems = element.all(by.className('bill'));
expect(elems.map(e => e.isDisplayed())).not.toContain(false);

You coulds also use reduce to aggregate the state returned by isDisplayed:

var elems = element.all(by.className('bill'));
expect(elems.reduce((acc, e) => acc && e.isDisplayed())).toBeTruthy();
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

In the end, I did this:

let billEls = element.all(by.className('bills'));

billEls.then(elements => {
  elements.forEach(el => {
    expect(el.isDisplayed()).toBeTruthy();
  })
});

but would have preferred the @FlorentB's .reduce method.

IanT8
  • 2,167
  • 2
  • 23
  • 38