Skip to main content

Overview

Playwright’s Code Generator (codegen) is an interactive tool that records your browser interactions and generates test code automatically. It’s the fastest way to create end-to-end tests by simply using your application as you normally would.

Starting Codegen

Basic Usage

This opens a browser window and the Playwright Inspector. All your actions in the browser will be recorded and converted to test code.

With a URL

Start recording with a specific URL already loaded.

Command Options

Browser Selection

Supported browsers:
  • chromium (default)
  • firefox or ff
  • webkit or wk
  • cr (alias for chromium)

Target Language

Generate code in different languages and test frameworks:
  • javascript - Plain JavaScript with Playwright Library
  • playwright-test - Playwright Test framework (default)
  • python - Synchronous Python API
  • python-async - Async Python API
  • python-pytest - Pytest with Playwright fixtures
  • java - Java with Playwright
  • java-junit - JUnit with Playwright
  • csharp - C# with Playwright
  • csharp-mstest - MSTest with Playwright
  • csharp-nunit - NUnit with Playwright

Output to File

Automatically save the generated code to a file instead of displaying it in the inspector.

Test ID Attribute

When set, codegen will prioritize selectors using the specified attribute. This generates more maintainable tests that are resilient to DOM structure changes.

Browser Context Options

Codegen supports all standard browser context options to emulate different environments:

Device Emulation

Custom Viewport

Color Scheme

Geolocation

Timezone

User Agent

Authentication and Storage

Saving Storage State

Perform authentication steps during recording, then save cookies and localStorage to a file. This state can be reused in tests.

Loading Storage State

Start with previously saved authentication state to record authenticated flows without repeating login.

Network and Proxy

Proxy Server

Ignore HTTPS Errors

Save HAR

Record all network activity during the session. Use glob patterns to filter specific requests.

How Codegen Works

Based on the implementation in /home/daytona/workspace/source/packages/playwright-core/src/cli/program.ts:563, codegen:
  1. Launches a browser with specified options (headless or headed mode)
  2. Enables the recorder through context._enableRecorder() which activates the recording mode
  3. Creates a trace directory for temporary trace storage
  4. Opens the Playwright Inspector to display generated code
  5. Monitors DOM interactions and converts them into test code
  6. Generates selectors using a priority system:
    • Test ID attributes (if configured)
    • Role-based selectors
    • Text content
    • CSS selectors
Codegen uses the same recorder engine that powers the Playwright Inspector, ensuring generated code matches best practices.

Tips for Better Code Generation

Use Test IDs

Add data-testid attributes to your application and use --test-id-attribute for stable selectors.

Slow Down

Don’t rush through actions. Give the page time to load and codegen time to record.

Clean Up

Review and refactor generated code. Extract reusable functions and add assertions.

Save State

Use --save-storage to capture authentication and reuse it across tests.

Example Workflow

See Also