What Is Playwright Codegen? Recording Tests by Clicking Through Your App
Playwright Codegen watches you click through your app and writes the test code for you. Here's how it works, what it's good at, and where you still need a human in the loop.
Alex Johnson
TL;DR
Playwright Codegen is a built-in tool that records your browser interactions and generates ready-to-run Playwright test code, picking resilient selectors automatically. It's the fastest way to scaffold a test, but the generated code still needs assertions, cleanup, and review before it's production-ready.
Playwright Codegen is a built-in tool that writes test code for you by watching what you do. You run a command, a browser opens, and as you click links, fill forms, and navigate pages, Codegen generates the matching Playwright test code in real time. It is the fastest way to go from "I know the journey I want to test" to a working script.
How to use it
Codegen ships with Playwright — there is nothing extra to install. You start it from the terminal:
- Run
npx playwright codegen https://yourapp.com - Playwright opens a browser alongside an inspector window
- You interact with your app exactly as a user would
- Codegen writes the corresponding test code live in the inspector
- You copy the generated code into a test file and refine it
You can also start recording at a specific URL, emulate a device or viewport, or save authentication state so you do not have to log in every time you record.
What Codegen is genuinely good at
Picking resilient selectors
The hardest part of writing tests by hand is choosing how to locate elements. Codegen does this well: it prefers user-facing attributes — roles, visible text, labels, and test IDs — over brittle CSS classes. That means the recorded test is more likely to survive a design change, which is one of the biggest causes of flaky tests.
Scaffolding speed
For a long journey — say a ten-step checkout — typing every locator and action by hand is tedious and error-prone. Codegen captures the whole sequence in the time it takes to click through it once, giving you a solid skeleton in seconds.
Learning the API
If you are new to Playwright, watching Codegen translate your clicks into code is one of the fastest ways to learn how its locators and actions work.
Where you still need a human
Codegen records actions, but a good test is mostly about assertions — the checks that prove the app did the right thing. A recording will click "Place order," but it will not automatically know that you care whether an order-confirmation number appeared. You typically need to:
- Add meaningful assertions about the expected outcome
- Remove redundant or accidental steps captured during recording
- Parameterize data (emails, names) instead of hardcoding what you typed
- Restructure repeated journeys into reusable helpers
- Review the result for stability before trusting it in CI
In other words, Codegen gets you to a great first draft fast — but the draft is not the finished test.
Why this matters for self-service testing
Codegen is what makes it realistic for teams to create and maintain their own tests without deep automation expertise. Lowering the barrier to authoring a test means product managers, QA analysts, and developers can all contribute coverage. The trade-off is discipline: without review, recorded tests can pile up as shallow click-throughs that inflate your count without proving journeys work — the fragmentation problem we cover in why teams replace tests with flows.
The bottom line
Playwright Codegen is an excellent accelerator: it removes the tedious part of writing tests and chooses good selectors automatically. Treat its output as a strong starting point that a human shapes into a reliable, assertion-rich test. QA Guardian pairs tooling like Codegen with senior engineers who add the assertions, structure, and review that turn recordings into durable coverage — and we hand you standard Playwright code you own. Book a demo to see the workflow on your app.
Frequently asked questions
How do you run Playwright Codegen?
Run the command `npx playwright codegen <url>` in your terminal. Playwright opens a browser and an inspector window; as you click and type, it writes the matching test code live, which you can copy into a test file.
Is Codegen-generated code production-ready?
Not by itself. Codegen produces an excellent starting point with resilient selectors, but you typically need to add meaningful assertions, remove redundant steps, and review it for stability before relying on it in CI.
Tags
More on Playwright
What Is Playwright? A Plain-English Guide to Modern Browser Testing
Playwright is an open-source framework for automating web browsers to test that your app works the way real users expect. Here's what it is, who it's for, and why teams are adopting it.
How Playwright Works: Architecture, Auto-Waiting, and the Test Lifecycle
Under the hood, Playwright communicates with browsers over a single WebSocket connection and waits for elements to be actionable automatically. Here's how that architecture produces fast, reliable tests.
Playwright vs. Selenium: Which Browser Automation Framework Should You Use?
Selenium defined browser automation for a decade. Playwright is the modern alternative. Here is a neutral comparison of their architectures, speed, browser support, and when to migrate.
See modern QA in action
Everything we write about is what we build and run every day. Book a demo and we'll show you flow-based Playwright coverage on your own codebase.