Search code examples
javaplaywright

Playwright: wait for selector using a regexp, to NOT contain specific text


I am using playwright in java and I have a page that runs javascript. After opening it shows "Loading..." and only when this disappears the content to be tested appears.

So, I want to load the (iframe) with a selector to check for the text "Loading..." to disappear. While I can select for the "headerMessage" etc. I don't get this working with the negated regexp. Is this not possible? What am I doing wrong?

Frame frame = page.frame("tf_content").waitForSelector("#headerMessage :text-matches(\"!Loading...\")").ownerFrame();

Solution

  • Playwright has an assertion system. You can use it to wait until a lot of useful and smart conditions:

    • Element to be visible
    • Element Not to be visible
    • Selector to have X count
    • Element to be enabled
    • Element attribute the have Y value

    Import the class:

    import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
    

    Code example:

    assertThat(page.locator("//*[contains(text(), 'Loading...')]")).hasCount(0);
    // or:
    assertThat(page.locator("//*[contains(text(), 'Loading...')]")).isHidden();
    

    After the assert has passed - you are good to go.