How can you record tests in Playwright?
You can record a test by using the site yourself while Playwright watches. The recorder is called codegen. Run one command, click through your flow in the browser window that opens, and Playwright writes the matching test code live in a second window. It is the quickest way for a manual tester to produce a first script, and for anyone to discover good locators on an unfamiliar page.
Step 1: start the recorder
npx playwright codegen https://demo.example.com
Two windows open: a browser, and the Playwright Inspector showing code. Each click, fill or navigation you make appears as a line of code. By default the code is a Playwright Test file in TypeScript/JavaScript; --target switches to Python (python-pytest), Java (java-junit), C# and other flavours.
Step 2: add assertions while you record
A recording that only clicks proves nothing. The Inspector toolbar has assertion tools: choose "assert visibility", "assert text" or "assert value", then click an element. Codegen writes an expect(...) line for it. Add at least one assertion at the end of every flow, on the thing the user actually cares about.
Step 3: save and tidy the code
Copy the code from the Inspector, or write it straight to a file with -o tests/enquiry.spec.ts. A recorded enquiry flow, after a short clean-up, could look like this:
// tests/enquiry.spec.ts
import { test, expect } from '@playwright/test';
test('visitor can send a course enquiry', async ({ page }) => {
await page.goto('/');
await page.getByRole('link', { name: 'Software Testing' }).click();
await page.getByRole('button', { name: 'Enquire now' }).click();
await page.getByLabel('Full name').fill('Neha');
await page.getByLabel('Phone').fill('9999999999');
await page.getByRole('button', { name: 'Submit' }).click();
await expect(page.getByText('Thank you')).toBeVisible();
});
The clean-up usually means: replace the full start URL with '/' and set baseURL in the config, rename the test to say what it proves, delete accidental clicks, and move repeated steps into a fixture or page object (see fixtures in Playwright). The names and labels above are placeholders.
Useful codegen flags
| Flag | Use |
|---|---|
--device="iPhone 13" | Record with mobile emulation |
--viewport-size="1280,720" | Fix the window size |
--save-storage=auth.json | Save cookies and storage at the end, for example after logging in |
--load-storage=auth.json | Start the recording already logged in |
--lang="hi-IN", --timezone="Asia/Kolkata" | Record with a locale and time zone |
--test-id-attribute=data-qa | Prefer your team's test-id attribute in locators |
-b webkit | Record in WebKit or Firefox instead of Chromium |
Run npx playwright codegen --help to see the full list for your installed version.
Other ways to record
- VS Code extension. The official Playwright extension has "Record new" (creates a new test file) and "Record at cursor" (adds steps inside an existing test). It also has "Pick locator" to get a locator for any element.
- From inside a test. Put
await page.pause()in a test and run it with--headed. The Inspector opens at that point and you can record the next steps from the current state.
Limits of recorded tests
- The recorder picks locators by role, label, text or test id, which is good, but it cannot know your intent. Check each locator is specific enough; see selectors in Playwright.
- It records what you did, not the data you need. Replace hard-coded values with test data, and set up state through the API where possible.
- It never adds waits, and you should not add fixed sleeps. Playwright auto-waits; see types of waits in Playwright.
- "Recording" a test is not the same as recording a video of it. For that, see capturing videos during test execution.
Want to master Playwright hands-on?
Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.
Explore the Course →