IanT8
IanT8

Reputation: 2217

Protractor how to assert multiple elements with same css class are all displayed

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.

Upvotes: 2

Views: 717

Answers (2)

IanT8
IanT8

Reputation: 2217

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.

Upvotes: 0

Florent B.
Florent B.

Reputation: 42528

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();

Upvotes: 2

Related Questions