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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Playwright codegen records browser actions and writes a test." iinventedlight Library, iil:note:wew2vxgc, version of 2026-11-16. https://iinventedlight.com/library/notes/playwright-codegen/. License: CC-BY-4.0.
@misc{iil_note_wew2vxgc,
author = {{iinventedlight}},
title = {{Playwright codegen records browser actions and writes a test}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/playwright-codegen/},
note = {Permanent ID iil:note:wew2vxgc. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/playwright-codegen.md.