3

I've got a fairly simple Protractor test which should check the text value in a row of a ng-repeat.

Here's my HTML:

<div ng-repeat="destination in destinations">
    <span>{{destination.city}}, {{destination.country}}</span>
</div>

And here's my JS:

lastDestination = element.all(by.repeater('destination in destinations').row(1));
expect(lastDestination.getText()).toEqual("Madrid, Spain");

The documentation for getText() states:

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

So I'd expect the text from the row's span tag to be returned, but when running the Protractor test, I get the following error for the assertion:

Expected [ 'Madrid, Spain' ] to equal 'Madrid, Spain'.

GetText() seems to be returning an array instead of a string.

I tried resolving getText()'s promised, but still got the same error:

lastDestination = element.all(by.repeater('destination in destinations').row(1));

lastDestination.getText().then(function (text) {
   expect(text).toEqual("Madrid, Spain"); 
});

I can get around the issue by targeting the first value in the array:

expect(text[0]).toEqual("Madrid, Spain");

But I'd still like to know why this isn't working in the first place.

Any ideas?

Update: A similar bug has been reported on Protractor's Github page, so it could be that the getText() function is just not working as it should.

0

1 Answer 1

4

By the documentation:

// Returns a promise that resolves to an array of WebElements containing
// the DIVs for the second book.
bookInfo = element.all(by.repeater('book in library').row(1));

You are attempting to use getText on a promise, you need to resolve it first.

var lastDestination;
element.all(by.repeater('destination in destinations').row(1)).then(
     function(elements){
          lastDestination = elements[0];
});
expect(lastDestination.getText()).toEqual("Madrid, Spain");

SOURCE:http://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.repeater

This is what happens behind the scenes. Presuming you call getText() on an WebElement class. The element will be the value passed to the core.text.getElementText

Selenium(protractor) handles what parameters to send.

This is the code that gets the content, if a WebElement is used. I don't know what happens if a promise that resolves to an array is the explicit thisArg.

explicitThisArg.getText()//the explicit thisArg is the object that the function is called from.

  core.text.getElementText = function(element) {
  var text = '';
  var isRecentFirefox =
      (goog.userAgent.GECKO && goog.userAgent.VERSION >= '1.8');

  if (isRecentFirefox || goog.userAgent.WEBKIT || goog.userAgent.IE) {
    text = core.text.getTextContent_(element, false);
  } else {
    if (element.textContent) {
      text = element.textContent;
    } else {
      if (element.innerText) {
        text = element.innerText;
      }
    }
  }

  text = core.text.normalizeNewlines_(text);
  text = core.text.normalizeSpaces_(text);

  return goog.string.trim(text);
};
Sign up to request clarification or add additional context in comments.

3 Comments

Your code works, however, the documentation for getText() states that "Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace". So I believe that getText() on the row should also work. I've found a similar issue here github.com/angular/protractor/issues/1794, so I'm thinking this might be a bug.
I should note that you calling getText on a promise, which is not the documentations intended use anyway.
Makes sense, thank you for delving into this and the detailed answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.