skip to Main Content

In my test case there is an element that should be checked if it contains any text and it’s not empty, and if it’s empty fail the test.

I’ve tried to use toHaveText without a string like so:

await expect(myLocator).toHaveText();

but passing string/regex is mandatory.

How can I assert this kind of thing?

3

Answers


  1. Chosen as BEST ANSWER

    I've found a way using Regex "Dot" Metacharacter, which means Any character (except newline character):

    await expect(myLocator).toHaveText(/./);
    

  2. You should try the following test :

    await expect(myLocator).not.toBeEmpty();
    
    Login or Signup to reply.
  3. You could use the following to check if the element has any text in it:

    expect(locator).not.toBeEmpty();
    

    As found in the playwright documentation (
    https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-be-empty):

    toBeEmpty
    Ensures the Locator points to an empty editable element or to a DOM
    node that has no text.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search