An easy solution would be to locate both spans and compare their texts:
var firstSpan = element(by.css("a.showld")).element(by.binding("locations.selectedCount")),
secondSpan = element(by.css('label[key="search.selectedLocations"]')).element(by.binding("locations.selectedCount"));
firstSpan.getText().then(function (firstText) {
var secondText = secondSpan.getText();
expect(secondText).toEqual(firstText);
});
It's important to note that getText()
, like many other methods in Protractor
, returns a promise that must be resolved. In this case, we are resolving the first promise explicitly with then()
and allowing expect()
to implicitly resolve the second promise. For more information, visit: Comparing values of two promises.
Another method could be:
var spans = element.all(by.binding("locations.selectedCount"));
spans.first().getText().then(function (firstText) {
var lastText = spans.last().getText();
expect(lastText).toEqual(firstText);
});
However, this approach may not be as scalable and is best suited for only 2 elements.
A more scalable solution involves using map()
and Array.reduce()
. By gathering all span
texts into an array and checking if all items in the array are equal:
var spans = element.all(by.binding("locations.selectedCount"));
spans.map(function (span) {
return span.getText();
}).then(function (texts) {
var allTextsAreTheSame = texts.reduce(function(a, b) {
return (a === b) ? a : false;
});
expect(allTextsAreTheSame).toBe(true);
});