1
<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>

How would I verify through protractor that the value of these two spans are the same when one span is under an tag while the other is under a label tag in different places?

is it using the 'equal' element?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Kisuna97
  • 37
  • 7

1 Answers1

0

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);
});
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • @Kisuna97 ok, I've provided you with 3 options, try them out and let me know. – alecxe Jul 28 '15 at 20:26
  • your 2nd approach, i get an error expected 1023 to equal to ' ' and for the last approach it worked after i changed a: false; to a: true; – Kisuna97 Jul 28 '15 at 20:33
  • @Kisuna97 don't change `false` to `true` in the third approach. Can you check how many `span` elements with this binding are there on the page? And also, what about the first option? (I'll be AFK for a while, but it sounds like you have more than one `span` element with the same `locations.selectedCount` binding, probably others are invisible..check it out). Thanks. – alecxe Jul 28 '15 at 20:35
  • the first one I understand the code, however it says no element found using by.binding(locations.selectedCount) – Kisuna97 Jul 29 '15 at 14:35
  • @Kisuna97 well, can you provide a link to the website or an HTML code of the appropriate block? Also, do you see any iframes on the page? Thanks. – alecxe Jul 29 '15 at 14:40
  • @Kisuna97 now I'm puzzled what your real HTML is and what are you trying to achieve. Anyway, I think you should be able to adapt any of the 3 options I've provided. – alecxe Jul 29 '15 at 19:53