Search code examples
angularjstestingseleniumprotractorend-to-end

Protractor expected condition for element containing any text


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);

Solution

  • 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);