Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Codegen


Share

Playwright Codegen

Codegen is a Playwright tool that records what you do in a browser and writes it as test code.

You click and type like a normal user, and Playwright writes the matching page.goto(), fill() and click() lines for you.

It is a fast way to start a new test, and a good way to learn which locators Playwright prefers.


Start Recording

Run codegen from your project folder, with the page you want to test:

Example

npx playwright codegen https://www.w3schools.com/playwright/practice/login.html

Two windows open:

  • A browser window with the page: this is where you click and type
  • The Playwright Inspector: this is where the code appears, line by line, while you work

Recording starts at once.

Every action you take in the browser window becomes a line of code in the Inspector.

Note: Codegen opens real browser windows, so it needs a desktop.

It does not work on a computer without a screen, like most CI servers (computers that run your tests automatically).



The Inspector Window

The Inspector shows the recorded code and has a toolbar at the top:

ButtonWhat it does
RecordStarts or stops recording
Pick locatorLets you click an element to see its locator, without recording a click
CopyCopies the generated code to the clipboard
ClearClears the recorded code so you can start over
TargetChooses the language of the code: Playwright Test (JavaScript), Python, Java, C# and more

At the bottom of the Inspector there are tabs.

The Locator tab shows the locator of the element you picked.

You can edit it there to try your own locator.

The Log tab shows what Playwright did for each action.


Record a Login

On the practice login page, do this in the browser window:

  1. Click the Username field and type student
  2. Click the Password field and type w3schools
  3. Click the Log in button

Codegen writes a test like this one.

Copy it into a file in your tests folder:

Example

tests/login.spec.js

import { test, expect } from '@playwright/test';

test('test', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/login.html');
  await page.getByRole('textbox', { name: 'Username' }).click();
  await page.getByRole('textbox', { name: 'Username' }).fill('student');
  await page.getByRole('textbox', { name: 'Password' }).click();
  await page.getByRole('textbox', { name: 'Password' }).fill('w3schools');
  await page.getByRole('button', { name: 'Log in' }).click();
  await expect(page.getByTestId('welcome')).toContainText('Welcome, student!');
});

The recorded test runs like any other test:

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/login.spec.js:3:1 › test
  1 passed (4.0s)

A recording is a first draft.

Give the test a real name, and remove steps you do not need, like the click() before each fill().


Record Assertions

A test also needs checks, not only actions.

The toolbar at the top of the browser window has buttons for that:

ButtonGenerated code
Assert visibilityawait expect(locator).toBeVisible();
Assert textawait expect(locator).toContainText('...');
Assert valueawait expect(locator).toHaveValue('...');
Assert snapshotawait expect(locator).toMatchAriaSnapshot(`...`);

Click an assert button, then click the element on the page.

The last line of the login test above came from Assert text on the welcome message.


How Codegen Picks Locators

Codegen looks at the element you clicked and chooses the most stable locator it can find.

It prefers, in this order:

  1. A test id, like getByTestId('welcome')
  2. A role and accessible name, like getByRole('button', { name: 'Log in' })
  3. A label, placeholder or text
  4. A CSS selector, only when nothing better exists

This is the same advice as in the Locators chapter.

Use Pick locator when you write tests by hand: hover an element, click it, and copy the locator from the Inspector.


Codegen Options

You can change how codegen records with options:

OptionDescription
--target <language>The language to generate, for example python or java (the default is playwright-test)
-o <file>Saves the generated code to a file
--device <name>Emulates a device, for example "iPhone 15"
--viewport-size <size>Sets the browser size in pixels, for example "800,600"
-b <browser>Records in chromium, firefox or webkit
--save-storage <file>Saves cookies and local storage to a file when you close codegen
--load-storage <file>Starts with cookies and local storage from a saved file

This command records on a phone screen and saves the code to a test file:

Example

npx playwright codegen --device="iPhone 15" -o tests/mobile.spec.js https://www.w3schools.com/playwright/practice/todo.html

This command records a login and saves the logged-in state (cookies and local storage) to auth.json, so you can reuse it later:

Example

npx playwright codegen --save-storage=auth.json https://www.w3schools.com/playwright/practice/login.html

You will learn how to use saved state in the Authentication chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.