Consider comparing the quantity of elem1
elements to those that contain elem2
elements:
var elem1count = element.all(by.xpath("//*[contains(@class, 'elem1')]")).count();
var elem1WithElem2count = element.all(by.xpath("//*[contains(@class, 'elem1') and .//*[contains(@class, 'elem2')]]")).count();
elem1WithElem2count.then(function (elem1WithElem2count) {
expect(elem1count).toEqual(elem1WithElem2count);
});
This approach may not be the most elegant solution.
A similar concept can be achieved using filter()
:
var elem1count = element.all(by.css(".elem1")).count();
var elem1WithElem2count = element.all(by.css(".elem1")).filter(function (elem1) {
return elem1.element(by.css('.elem2')).isPresent();
}).count();
elem1WithElem2count.then(function (elem1WithElem2count) {
expect(elem1count).toEqual(elem1WithElem2count);
});
Alternatively, you can utilize reduce()
to solve this problem:
var result = element.all(by.css('.elem1')).reduce(function(acc, elem1) {
return elem1.element(by.css('.elem2')).isPresent().then(function (isPresent) {
return acc && isPresent;
});
}, false);
expect(result).toBe(true);
In this scenario, each elem1
element is assessed for the presence of an elem2
element, with the results being consolidated into a single boolean value.
You could also employ each()
alongside expect
statements to validate every elem1
:
element.all(by.css('.elem1')).each(function(elem1) {
expect(elem1.element(by.css('.elem2')).isPresent()).toBe(true);
});