How to wait for gettext not to be empty for an element

  • I want to make sure the getText returns a non-empty string before continuing the program flow.

    I have tried :

    var EC = protractor.ExpectedConditions;
    await browser.wait(EC.not(EC.textToBePresentInElement($('#result'), '')), 5000);

    my div looks similar to:

    <div id="result">5</div>

    But the browser.wait always passes as the empty character is considered to be included in every string:

    console.log('teststring'.includes('')) //always returns true

    Could someone let me know the correct way to wait till the text is present in an element? I cannot wait for a specific text as the text changes according to inputs.

  • I went through the source code of the expected condition,

    Thee browser.wait expects a promise that resolves to a condition, so i created a promise that resolves to true of getText()==='', else false:

    So the solution is :

    let textNotToBePresentInElement= function (elem,text)  {
        return async()=> {
        let textreceived=await elem.getText();
        let bool=textreceived === text;
        return !(bool);
     await browser.wait(textNotToBePresentInElement(resultElement,''), 5000, "Result is empty even afer 3 sec");

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2