A straight-forward option would be to find both spans and compare the 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);
});
Note that getText()
, as many other methods in Protractor
, returns a promise which needs to be resolved. We have two promises here, the resolved values of which we need to compare, we are resolving the first one explicitly via then()
and letting expect()
to implicitly resolve the second. See more at: Comparing values of two promises.
Another possible approach:
var spans = element.all(by.binding("locations.selectedCount"));
spans.first().getText().then(function (firstText) {
var lastText = spans.last().getText();
expect(lastText).toEqual(firstText);
});
This is though not quite scalable and is good probably for 2 elements only.
A more scalable solution would involve using map()
and Array.reduce()
. Let's gather all the span
texts into a array and check if all the 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);
});