Playwright · Reliable end-to-end tests
What is Playwright codegen and when should you use it?
Short answer
Codegen records browser actions and generates Playwright code. Use it to explore an unfamiliar flow or discover locators, then refactor the output into maintainable tests.
Codegen opens a browser and a recorder. As you click and type, it writes Playwright calls and shows candidate locators. It is a discovery tool: useful for a quick start on an unfamiliar UI, not a substitute for test design.
npx playwright codegen https://example.com
npx playwright codegen --device="iPhone 15" https://example.com
npx playwright codegen --load-storage=auth.json https://example.comawait page.getByRole("button", { name: "Sign in" }).click();
await page.getByLabel("Email").fill("asha@example.com");
await page.getByRole("button", { name: "Continue" }).click();Where codegen helps
- Discovering a robust role or label locator for a tricky element
- Walking through a flow you do not know yet
- Checking which action triggers a popup, dialog or download
- Generating a first draft that you then refactor
- Inspecting a mobile layout with a device profile
Recorded code tends to be procedural, has no assertions, and may include incidental clicks. Treat it as a sketch. Add meaningful assertions, remove steps that are not part of the requirement, and extract fixtures or page objects only where they earn their keep.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you record a flow that requires login?
- 2Can codegen emulate a mobile device?
- 3Why is recorded code not production-ready?
- 4How do you improve a weak generated locator?
Say it in the interview
Codegen records my clicks and typing into Playwright code, which is handy for exploring an unfamiliar flow or finding a locator. I run it with a URL, optionally a device or loaded auth state. The output is only a draft because it lacks assertions and may include incidental steps. I refactor it into a clear test with user-facing locators before committing.
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.