Skip to main content

Web-first assertions and auto-waiting

Why This Matters

Web pages are never “done”. Products load after the page, the cart updates after a request, an error message appears after a delay. A test that checks too early fails for no reason; a test that sleeps to be safe is slow and still fails on a bad day. Playwright solves this by waiting for the right moment itself, if you use the right kind of assertion.

Actions wait for you

Before Playwright clicks, it waits until the element is attached, visible, stable (not moving), enabled and able to receive the click. You never write “wait until the button is there” before a click; click() does that.

Web-first assertions retry

Assertions on a locator keep retrying until they pass or the timeout runs out (5 seconds by default):

await expect(page.getByTestId("cart-button")).toContainText("2 items");

If the cart still says “1 items” when the assertion starts, Playwright checks again, and again, until it says “2 items”. This is the real test from the repository:

test("adding two products updates the cart button", async ({ page }) => {
  const cart = page.getByTestId("cart-button");
  await expect(cart).toContainText("0 items");

  const headphones = page.getByTestId(/^product-\d+$/).filter({ hasText: "Bluetooth Headphones" });
  const lamp = page.getByTestId(/^product-\d+$/).filter({ hasText: "Desk Lamp" });
  await headphones.getByRole("button", { name: "Add to Cart" }).click();
  await lamp.getByRole("button", { name: "Add to Cart" }).click();

  // Retries until the page shows it: no waitForTimeout.
  await expect(cart).toContainText("2 items");
  await expect(cart).toContainText("$84.98");
});

$84.98 is $59.99 + $24.99: the subtotal() helper from module 1 gives the same number.

The ones you will use most

AssertionPasses when
toBeVisible() / toBeHidden()The element is (not) shown
toHaveText(text)The full text matches
toContainText(text)The text contains this part
toHaveCount(n)The locator matches exactly n elements
toBeEnabled() / toBeDisabled()The control can (not) be used
toHaveAttribute(name, value)An attribute has this value

Wait for loading, without sleeping

The shop loads its products after the page itself. Every shop test therefore starts by waiting for the loading message to disappear:

test.beforeEach(async ({ page }) => {
  await page.goto("/shopping");
  await expect(page.getByTestId("products-loading")).toBeHidden();
});

The login page waits about 0.8 seconds before it answers, on purpose. The test simply asserts on the result; the assertion waits along:

await page.getByRole("button", { name: "Log in" }).click();
await expect(page.getByTestId("welcome-message")).toHaveText("Welcome, Practice User! You are logged in.");

The trap: reading a value yourself

expect(await page.getByTestId("cart-button").textContent()).toContain("2 items");   // trap

This reads the text once, at that exact moment, and then compares a plain string. Nothing retries. If the cart updates 50 milliseconds later, the test fails. The rule of thumb: put the locator inside expect(...), not a value you read from it.

And never waitForTimeout

await page.waitForTimeout(3000);   // don't

A fixed sleep is too long on a fast day and too short on a slow one. Every waitForTimeout in a test is a question: what am I actually waiting for? Assert on that instead.

Exercise

Write a test that searches for “mouse”, checks that exactly one product is shown, and that the results count says 1. Then write the same test with textContent() and a plain expect, and run both with --repeat-each=10.

Show suggested solution
test("searching for mouse finds one product", async ({ page }) => {
  await page.getByRole("textbox", { name: "Search for items" }).fill("mouse");
  await expect(page.getByTestId(/^product-\d+$/)).toHaveCount(1);
  await expect(page.getByTestId("results-count")).toHaveText("1");
});

The search finds only the Wireless Mouse, so both checks expect 1. The web-first version waits for the filtered list and passes every time. The textContent() version reads the count right after typing; if the shop has not filtered the list yet, it reads the old count (10 products) and fails. Whether that happens depends on timing, so it may pass ten times on your machine and fail in CI: a flaky test you wrote yourself, in one line.