Record a Playwright test with codegen
Point npx playwright codegen at your app, click through it, and save the recording as a first test you can run and edit.
Use Playwright codegen to record the browser actions for your first test. Then add an assertion for the result the user should see.
Start your app from a Playwright Test project with browser binaries installed. Run codegen from the test project. The example uses a todo page on port 4110.
npx playwright codegen --output todo.spec.ts http://localhost:4110Two windows open: a browser on your page and the Playwright Inspector. The Inspector already shows a test named test with one line in it, a page.goto for the URL you passed. --output writes that code to todo.spec.ts to the named file. Put that file under the configured testDir if your project sets one.
Click through the page
Click the text field, type a todo, click Add. Each action in the browser appends a line to the test. When you close the browser, the file holds what you did:
import { test, expect } from '@playwright/test';
test('adds a todo', async ({ page }) => {
await page.goto('http://localhost:4110/');
await page.getByRole('textbox', { name: 'New item' }).click();
await page.getByRole('textbox', { name: 'New item' }).fill('Write the post');
await page.getByRole('button', { name: 'Add' }).click();
await expect(page.getByRole('listitem').filter({ hasText: 'Write the post' })).toHaveCount(1);
});Codegen prefers role, text and test id locators, so the recorded lines name the field and the button by role and label. I renamed the generated test and added the last line. Codegen can also record assertions through the Inspector toolbar.
Turn the recording into a test
Rename the test after the behaviour it checks. Add at least one expect, because a test that only clicks passes even when the page is broken.
If the click works but the app never adds the todo, the filtered locator has zero matches and the count assertion fails. Existing todos can stay in the list.
Then read the locators. I keep the ones built from a role and a name, like getByRole('button', { name: 'Add' }). If a recorded line reads like a CSS path, I add a label to the element in the page and record again.
Run it.
$ npx playwright test
✓ 1 todo.spec.ts:3:1 › adds a todo (361ms)
1 passed (2.1s)One test runs and passes in headless Chromium. From here you edit the file like any other test: swap the full URL for a baseURL in the config, then record the next case.