Playwright Codegen
Playwright Codegen
Codegen is a Playwright tool that records what you do in a browser and writes it as test code.
You click and type like a normal user, and Playwright writes the matching page.goto(), fill() and click() lines for you.
It is a fast way to start a new test, and a good way to learn which locators Playwright prefers.
Start Recording
Run codegen from your project folder, with the page you want to test:
Example
npx playwright codegen https://www.w3schools.com/playwright/practice/login.html
Two windows open:
- A browser window with the page: this is where you click and type
- The Playwright Inspector: this is where the code appears, line by line, while you work
Recording starts at once.
Every action you take in the browser window becomes a line of code in the Inspector.
Note: Codegen opens real browser windows, so it needs a desktop.
It does not work on a computer without a screen, like most CI servers (computers that run your tests automatically).
The Inspector Window
The Inspector shows the recorded code and has a toolbar at the top:
| Button | What it does |
|---|---|
| Record | Starts or stops recording |
| Pick locator | Lets you click an element to see its locator, without recording a click |
| Copy | Copies the generated code to the clipboard |
| Clear | Clears the recorded code so you can start over |
| Target | Chooses the language of the code: Playwright Test (JavaScript), Python, Java, C# and more |
At the bottom of the Inspector there are tabs.
The Locator tab shows the locator of the element you picked.
You can edit it there to try your own locator.
The Log tab shows what Playwright did for each action.
Record a Login
On the practice login page, do this in the browser window:
- Click the Username field and type
student - Click the Password field and type
w3schools - Click the Log in button
Codegen writes a test like this one.
Copy it into a file in your tests folder:
Example
tests/login.spec.js
import { test, expect } from '@playwright/test';
test('test', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/login.html');
await page.getByRole('textbox', { name: 'Username' }).click();
await page.getByRole('textbox', { name: 'Username' }).fill('student');
await page.getByRole('textbox', { name: 'Password' }).click();
await page.getByRole('textbox', { name: 'Password' }).fill('w3schools');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page.getByTestId('welcome')).toContainText('Welcome, student!');
});
The recorded test runs like any other test:
Output:
Running 1 test using 1 worker
[1/1] [chromium] › tests/login.spec.js:3:1 › test
1 passed (4.0s)
A recording is a first draft.
Give the test a real name, and remove steps you do not need, like the click() before each fill().
Record Assertions
A test also needs checks, not only actions.
The toolbar at the top of the browser window has buttons for that:
| Button | Generated code |
|---|---|
| Assert visibility | await expect(locator).toBeVisible(); |
| Assert text | await expect(locator).toContainText('...'); |
| Assert value | await expect(locator).toHaveValue('...'); |
| Assert snapshot | await expect(locator).toMatchAriaSnapshot(`...`); |
Click an assert button, then click the element on the page.
The last line of the login test above came from Assert text on the welcome message.
How Codegen Picks Locators
Codegen looks at the element you clicked and chooses the most stable locator it can find.
It prefers, in this order:
- A test id, like
getByTestId('welcome') - A role and accessible name, like
getByRole('button', { name: 'Log in' }) - A label, placeholder or text
- A CSS selector, only when nothing better exists
This is the same advice as in the Locators chapter.
Use Pick locator when you write tests by hand: hover an element, click it, and copy the locator from the Inspector.
Codegen Options
You can change how codegen records with options:
| Option | Description |
|---|---|
--target <language> | The language to generate, for example python or java (the default is playwright-test) |
-o <file> | Saves the generated code to a file |
--device <name> | Emulates a device, for example "iPhone 15" |
--viewport-size <size> | Sets the browser size in pixels, for example "800,600" |
-b <browser> | Records in chromium, firefox or webkit |
--save-storage <file> | Saves cookies and local storage to a file when you close codegen |
--load-storage <file> | Starts with cookies and local storage from a saved file |
This command records on a phone screen and saves the code to a test file:
Example
npx playwright codegen --device="iPhone 15" -o tests/mobile.spec.js https://www.w3schools.com/playwright/practice/todo.html
This command records a login and saves the logged-in state (cookies and local storage) to auth.json, so you can reuse it later:
Example
npx playwright codegen --save-storage=auth.json https://www.w3schools.com/playwright/practice/login.html
You will learn how to use saved state in the Authentication chapter.