← Back to library
Playwrightwaitintermediatev1.20+

Poll until a JS condition is true

Use waitForFunction to keep polling the DOM until a dynamic condition resolves.

1test('waits until feed has at least 5 items', async ({ page }) => {
2  await page.goto('/live-feed');
3
4  // Evaluate runs inside the browser; Playwright polls until it returns truthy
5  await page.waitForFunction(
6    () => document.querySelectorAll('[data-testid="feed-item"]').length >= 5,
7    { timeout: 15_000, polling: 500 }
8  );
9
10  await expect(page.locator('[data-testid="feed-item"]')).toHaveCount(5);
11});
#waitForFunction#poll#condition#live feed