133 / 139 · 13 Browser Automation with Playwright · AI-Assisted Testing← prev⊞ allnext →☰ Read as one page
16.2Playwright Codegen
Playwright's built-in code generator is the simplest form of AI-assisted test creation: record browser interactions and generate test code automatically.
npx playwright codegen https://example.com
This opens a browser and a code inspector. As you click, type, and navigate, Playwright generates test code in real time.
What Codegen Produces
// Generated by codegen — a solid starting point
test('test', async ({ page }) => {
await page.goto('https://example.com/login');
await page.getByLabel('Email').fill('user@example.com');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
});
Where Codegen Falls Short
- Generated tests are linear scripts, not structured with page objects
- No test data management or parameterization
- No error handling or edge case coverage
- Locators may not follow your team's strategy (e.g., using testId vs role)
Best practice: Use codegen to bootstrap, then refactor into your framework's patterns.