Playwright Codegen Test Generator
This chapter introduces Playwright's command-line test generator, Codegen, which automatically generates test code through browser operations.
What is Codegen
Codegen (Code Generator) is Playwright's built-in interactive test generation tool.
You operate the browser like a normal user, and Codegen automatically converts your operations into Playwright test code.
Launch Codegen
# Basic usage: open a URL and start recording
npx playwright codegen https://www.runoob.com/
# Specify the language for code generation (default TypeScript)
npx playwright codegen --target=javascript https://www.runoob.com/
npx playwright codegen --target=python https://www.runoob.com/
After launching, two windows open:
- Browser window: you operate the page in this window
- Playwright Inspector window: displays the generated code in real time
Record operations
Any operation performed in the browser window causes Codegen to automatically generate the corresponding Playwright code:
navigation
Entering a new URL in the address bar and pressing Enter generates page.goto() if statement.
Click
Clicking any element (button, link, menu) generates page.getByRole(...).click() or similar locate-and-click code.
Input
Typing text into an input box generates page.getByLabel(...).fill(...) or page.getByPlaceholder(...).fill(...)。
Choice
Selecting an option in a dropdown generates page.getByLabel(...).selectOption(...)。
Checkbox
Checking or unchecking a checkbox generates page.getByLabel(...).check() or .uncheck()。
Automatically select the best Locator
Codegen analyzes page element attributes and generates Locators in the following priority order:
getByRole()- the element has a clear ARIA rolegetByText()- the element has clear text contentgetByLabel()- the form element has an associated labelgetByPlaceholder()- the input box has a placeholdergetByAltText()- the image has an alt attributegetByTestId()- the element has a data-testidgetByTitle()- the element has a title attributelocator()- CSS selector (chosen last)
If Codegen finds multiple matching elements, it automatically refines the Locator until the target element is uniquely identified.
Generate assertions
In the Inspector window's toolbar, you can click assertion buttons to generate assertion code:
- Assert Visibility - click then select an element to generate
await expect(locator).toBeVisible() - Assert Text - generates
await expect(locator).toHaveText(...) - Assert Value - generates
await expect(locator).toHaveValue(...) - Assert Snapshot - generates
await expect(locator).toHaveScreenshot()
Common Codegen options
| Option | Description |
|---|---|
--target=javascript |
Generate JavaScript code (TypeScript by default) |
--target=python |
Generate Python code |
--target=java |
Generate Java code |
--target=csharp |
Generate C# code |
--save-storage=auth.json |
Save authentication state after recording ends |
--load-storage=auth.json |
Load existing authentication state before recording |
-o tests/test.spec.ts |
Specify output file (outputs to terminal by default) |
--viewport-size=375,812 |
Custom viewport size (simulate mobile) |
--device="iPhone 15" |
Use device presets |
--color-scheme=dark |
Specify color scheme |
Choosing between Codegen and VS Code recording
| Method | Applicable scenarios |
|---|---|
| VS Code Record New | For daily test development, code goes directly into the editor and can be modified immediately |
| Codegen command line | Rapid prototyping, cross-language code generation, no IDE dependency |