Playwright · Reliable end-to-end tests
How do Playwright assertions differ from plain Jest assertions?
Short answer
Playwright's expect adds web-first assertions that retry against the live page until they pass or time out. Generic assertions run once and are better for plain values such as API payloads.
A generic assertion like expect(value).toBe(3) evaluates immediately. A web-first assertion like expect(locator).toBeVisible() keeps re-checking the page for a short period, which handles rendering, animation and network latency without a sleep.
await expect(page.getByRole("heading")).toHaveText("Orders");
await expect(page.getByRole("row")).toHaveCount(3);
const order = { total: 120 };
expect(order.total).toBe(120);Useful web-first assertions
toBeVisible,toBeHidden,toBeEnabled,toBeCheckedtoHaveText,toContainText,toHaveValue,toHaveCounttoHaveAttribute,toHaveClass,toHaveCSS,toHaveURLtoHaveScreenshotandtoMatchAriaSnapshotfor visual and structural checksexpect.softto collect several failures before ending the test
Use expect.soft only when the test should continue after a failure and report multiple problems, such as a form with several fields. For a normal flow, a hard assertion is clearer because the test stops at the first broken step.
expect(locator).not.toBeVisible() waits for the element to become hidden; it does not pass just because the element is briefly absent before rendering.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you use a generic assertion instead of a web-first one?
- 2What does expect.soft change?
- 3Does a negated assertion retry?
- 4Why is toHaveText better than reading textContent manually?
Say it in the interview
Playwright's expect adds web-first assertions that retry against the page until they pass or time out, so I do not need to wait manually for text or visibility. For plain values, like an API response, I use ordinary assertions that run once. I use soft assertions when I want to collect several failures in one test. Negated locator assertions also retry, which avoids the classic race where an element has not rendered yet.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Playwright mock interview and get scored feedback on depth, clarity and confidence.