---
source: https://iinventedlight.com/library/notes/playwright-codegen.md
title: "Playwright codegen records browser actions and writes a test"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Playwright docs); Apache-2.0 (microsoft/playwright (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Playwright docs, Test generator, Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation; microsoft/playwright (GitHub), Test generator (Playwright source docs), Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: Playwright codegen"
tags: [playwright, codegen, test-generator, locators]
client: Librarian
sources:
  - id: S1
    title: "Test generator"
    publisher: Playwright docs
    url: https://playwright.dev/docs/codegen
    accessed: 2026-11-16
    license: "Apache-2.0"
  - id: S2
    title: "Test generator (Playwright source docs)"
    publisher: microsoft/playwright (GitHub)
    url: https://github.com/microsoft/playwright/blob/main/docs/src/codegen.md
    accessed: 2026-11-16
    license: "Apache-2.0"
---
# Playwright codegen records browser actions and writes a test
**Answer:** Playwright can generate tests for you as you perform actions in the browser. The `codegen` command opens two windows: a browser window and the Playwright Inspector window.

## Running codegen

Use the `codegen` command to run the test generator. You can pass the URL of the website you want to generate tests for. Example: `npx playwright codegen demo.playwright.dev/todomvc`.

## Locators

Playwright will look at your page and figure out the best locator. It prioritizes role, text and test id locators. If multiple elements match, the generator will improve the locator. You can also generate locators with the test generator in VS Code.

## See also

- [playwright-locators](https://iinventedlight.com/library/notes/playwright-locators/)
- [playwright-headless-tests](https://iinventedlight.com/library/notes/playwright-headless-tests/)
- [playwright-test-projects](https://iinventedlight.com/library/notes/playwright-test-projects/)
- [playwright-fixtures](https://iinventedlight.com/library/notes/playwright-fixtures/)

## Claims

- id: C1
  claim: The codegen command opens a browser and an Inspector window.
  source: S1
  evidence: "When running the"

- id: C2
  claim: codegen can take a demo URL such as demo.playwright.dev/todomvc.
  source: S1
  evidence: "npx playwright codegen demo.playwright.dev/todomvc"

- id: C3
  claim: The generator prefers role, text, and test id locators.
  source: S2
  evidence: "prioritizing [role, text and test id locators]"

- id: C4
  claim: The source docs title the page Test generator.
  source: S2
  evidence: "title: \"Test generator\""

## Sources

- [S1] Test generator. Playwright docs. https://playwright.dev/docs/codegen . License: Apache-2.0. Accessed 2026-11-16.

- [S2] Test generator (Playwright source docs). microsoft/playwright (GitHub). https://github.com/microsoft/playwright/blob/main/docs/src/codegen.md . License: Apache-2.0. Accessed 2026-11-16.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Playwright docs, Test generator, Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation; microsoft/playwright (GitHub), Test generator (Playwright source docs), Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
