9

I am trying to wait for an element that indicates a page is still loading, and exists on the page multiple time, to not be visible (think table with loading data placeholders).

Playwright documentation suggests using Locators is best practice, and therefore I initially tried to achieve this by doing:

locator.waitFor({state: "hidden")

However that errors due to Locators being strict and only being allow to match one element.

I'm now doing it with the following code:

page.waitForSelector(".foo .bar", {state: "hidden"})

This is non-ideal for a couple of reasons:

  • I'm storing page elements as locators in the Page Object Model, and you seemingly cannot access the selector of a locator, meaning the selector is duplicated in the code
  • I believe page.waitForSelector will use an ElementHandle which is discouraged

Is there any way to turn off the strictness constraint on a Locator? Or a way to achieve this using the Locator. I'm aware you can do .count on a Locator which matches multiple elements, but I've not found a nice way to combine that with waiting for the count to be 0.

bee-anchor
  • 119
  • 1
  • 1
  • 6
  • So using https://playwright.dev/docs/locators#lists and awaiting the visible state on each locator does not work for you? – madflow Jan 28 '22 at 16:30
  • @madflow ah no I'd glanced past that in the docs and not appreciated what that was for! I've got a working solution now (looping round getting the hidden state of all elements and waiting till they are all true, or a timeout is exceeded). – bee-anchor Jan 31 '22 at 15:18

3 Answers3

2

hope it will work

this code will check the next element each time the previous one disappears

while (await page.locator('.foo .bar').first().isVisible()) { //do nothing }
2

I got this working in the end using the evaluateAll method. Example code:

async waitForAllHidden(locator: Locator, timeout: number = 10000) {
    const start = Date.now()
    const elementsVisible = async () => (
        await locator.evaluateAll(elements =>
            elements.map(element => element.hidden))
    ).includes(false)

    while (await elementsVisible()) {
        if (start + timeout < Date.now()) {
            throw (`Timeout waiting for all elements to be hidden.
                    Locator: ${locator}. Timeout: ${timeout}ms`);
        }
    }
    console.log(`All elements hidden: ${locator}`)
}
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
bee-anchor
  • 119
  • 1
  • 1
  • 6
1

Use toHaveCount() (Added in: v1.20)

await expect(locator).toHaveCount(0,{timeout:2000});

Timeout: To avoid infinite loops timeout can be configured globally or passed as an parameter in the function call.

// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  expect: {
    timeout: 5000
  },
});

Tip: Its better to keep local timeouts lower than global timeout to avoid any race conditions if both exist.

Reference: https://github.com/microsoft/playwright/issues/11988

How can I assert that an element is NOT on the page in playwright?

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23