Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Codegen Test Generator

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:


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:

  1. getByRole() - the element has a clear ARIA role
  2. getByText() - the element has clear text content
  3. getByLabel() - the form element has an associated label
  4. getByPlaceholder() - the input box has a placeholder
  5. getByAltText() - the image has an alt attribute
  6. getByTestId() - the element has a data-testid
  7. getByTitle() - the element has a title attribute
  8. locator() - 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:


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