5

Is there a way how to check whether an element has any text in it? I already found textToBePresentInElement but this function checks for specified value and does not return a proper error if it fails.

I'm population the element via API and it's loaded bit later, so I want the browser to wait till any information appears in the element and then check for correct value.

Alternatively it would be also very helpful to manage to get a specific error message when EC fails:

browser.wait(EC.textToBePresentInElement(element(by.binding('myvar')), "expected"), 5000);
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Karl Adler
  • 15,780
  • 10
  • 70
  • 88

3 Answers3

13

The third argument to browser.wait() is a custom error message:

browser.wait(EC.textToBePresentInElement(element(by.binding('myvar')), "expected"), 5000, "Text is not something I've expected");

See also:


To wait for an element to contain any text, you can write a custom expected condition:

var EC = protractor.ExpectedConditions;

var anyTextToBePresentInElement = function(elementFinder) {
  var hasText = function() {
    return elementFinder.getText().then(function(actualText) {
      return actualText;
    });
  };
  return EC.and(EC.presenceOf(elementFinder), hasText);
};

And here is the usage:

browser.wait(anyTextToBePresentInElement(element(by.binding('myvar'))), 5000);
jdstaerk
  • 882
  • 1
  • 13
  • 30
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks for this useful answer. It's pretty perfect with third argument for `browser.wait()` function. Unfortunately I'm not able to run custom expected condition. It says "undefined function". Is there any documentation for custom expected conditions? – Karl Adler May 15 '15 at 09:05
  • @abimelex oops, my mistake, I've updated the answer, check it out. Thanks. – alecxe May 15 '15 at 10:34
  • It's a while ago, but I guess it is the correct solution. – Karl Adler Mar 04 '16 at 09:44
1

The previous code snippet works form but with a small update: return actualText; should be boolean. So the whole code will be:

var anyTextToBePresentInElement = function(elementFinder) {
  var EC = protractor.ExpectedConditions;
  var hasText = function() {
    return elementFinder.getText().then(function(actualText) {
      return !!actualText;
    });
  };
  return EC.and(EC.presenceOf(elementFinder), hasText);
};

Usage example:

var el = element(by.binding('myvar'));
browser.wait(anyTextToBePresentInElement(el, 5000, 'Element still has no text');
0

You can check this one

        export function titleType(textReference, expectedText)
        {
            textReference.then(function(name)
            {
                if (name == expectedText)
                {
                    expect(textReference).toContain(expectedText);
                    expect(name).toEqual(expectedText);
                }
                else
                {
                    throw new TypeError("Wrong " + expectedText + "  name " + name);
                }
            });
        }
Lelik_
  • 7
  • 3