Playwright · Reliable end-to-end tests
What does a first Playwright test look like?
Short answer
Import test and expect, call test with a title and an async function, and use the injected page fixture. Navigate, act through locators, then assert on a user-visible outcome.
A Playwright test is a function that receives fixtures. The most important fixture is page, a fresh tab in an isolated context. The test should read like a short user story: go somewhere, do something, and verify a result the user can see.
import { test, expect } from "@playwright/test";
test("searches for a product", async ({ page }) => {
await page.goto("/");
await page.getByRole("searchbox", { name: "Search" }).fill("lamp");
await page.getByRole("button", { name: "Search" }).click();
await expect(page.getByRole("heading", { name: "Results for lamp" })).toBeVisible();
await expect(page.getByRole("listitem")).toHaveCount(3);
});The anatomy
testdeclares a case with a title and an async callbackexpectprovides both generic and web-first assertionspageis created and torn down by the runner for every test- Locators describe elements lazily and re-resolve them on each action
- The test file is discovered by the configured test directory pattern
Run the file with npx playwright test tests/search.spec.ts, or add --headed to watch it. Failures produce a report with the failing step, and enabling a trace gives a step-by-step DOM and network timeline.
await page.waitForTimeout(1000) after an action. The next locator action or web-first assertion already waits for the condition it needs, and fixed sleeps only make the suite slower and less reliable.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Where does the `page` fixture come from?
- 2How do you run a single test file?
- 3Why does the test not need a manual wait after the click?
- 4What happens to the page after the test finishes?
Say it in the interview
A first test imports test and expect, declares a case, and uses the page fixture. I navigate, interact through locators, and assert on something visible, like a heading or a result count. The runner creates a fresh context and page for that test and tears them down afterwards. I do not add sleeps because locators and assertions already wait for the right condition.
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.