Learning center

Turn written reproduction steps into a script with codegen

"Open Orders, filter by open, click Export" reads clearly and still leaves room for a different click, a different account or a different browser. Playwright's codegen records what you do as a script that someone else can run. The script then needs one line you write by hand: the check that fails when the bug is present.

Record the steps#

Run codegen with the starting URL:

npx playwright codegen https://staging.example.test/orders

It opens a browser and the Playwright Inspector. Each click, fill and navigation appears as a line of code. Codegen prefers role, text and test-ID locators where the page offers them. Playwright: Test generator

Codegen also takes options that pin the conditions you reproduced under. These come from npx playwright codegen --help in Playwright 1.63:

OptionPins
--browser cr|ff|wkThe engine: Chromium, Firefox or WebKit
--channel chromeInstalled Google Chrome instead of Playwright's Chromium
--device "iPhone 13"Viewport, user agent, touch and device scale factor
--viewport-size "1280,720"The viewport alone
--lang de-DE and --timezone Europe/BerlinLocale and time zone
--color-scheme darkThe preferred color scheme
--load-storage auth.jsonCookies and storage saved earlier with --save-storage
--save-har repro.harEvery request and response, written at the end
--block-service-workersRuns without a service worker
--test-id-attribute data-qaThe attribute your app uses for test IDs

Put the options you used in the report. A script recorded with --timezone Asia/Tokyo and replayed without it is a different reproduction.

Add the assertion that fails#

A recording ends where you stopped clicking. It does not know what you expected. Add one assertion that states the expected result, so the script fails while the bug exists and passes once it is fixed:

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

test('export includes open orders', async ({ page }) => {
  await page.goto('https://staging.example.test/orders');
  await page.getByRole('button', { name: 'Status' }).click();
  await page.getByRole('option', { name: 'Open' }).click();
  const download = page.waitForEvent('download');
  await page.getByRole('button', { name: 'Export CSV' }).click();
  const file = await (await download).path();
  const rows = (await import('node:fs')).readFileSync(file, 'utf8').trim().split('\n');
  expect(rows.length).toBeGreaterThan(1); // header plus at least one order
});

The first five lines came from codegen. The last three state the bug: the export should contain more than a header row.

Make it run on the maintainer's machine#

A recorded script often depends on things only the reporter had.

  • The account. Replace a real account with a test account whose data the maintainer can create, or load a saved storage state for a test account.
  • Selectors with data in them, such as getByText('Order #48213'), fail on other data. Replace them with roles or test IDs.
  • Fixed waits such as waitForTimeout(3000) make the script slow and flaky. Wait for the response or element instead, or hold the response if the bug depends on order.
  • The environment URL. Put the base URL in one place so the maintainer can point it at their local build.

Then run it yourself once more, from a fresh context, before you send it.

Worked example: a step that was not in the written report#

This example is fictional. A tester writes: Search for "drill", open the first result, the price shows €0. The developer follows it and sees the right price.

The tester records the same steps with codegen and sends the script. One line stands out:

await page.getByRole('combobox', { name: 'Currency' }).selectOption('CHF');

The tester's currency selector had been on CHF since an earlier test. The written steps assumed the default. With the currency line in the script, the €0 price, now shown as CHF 0, replays every time, and the developer finds a missing exchange rate.

Questions#

Does codegen record in the reporter's own browser profile?

By default it starts a fresh context. The --user-data-dir option points it at a profile directory instead, but use a copy, not the profile in daily use, since automation can change it.

Which language should the script be in?

Use the one the maintainer's test suite uses. Codegen's --target option generates Playwright Test, JavaScript, Python, Java or C#.

What if the bug does not reproduce in the script?

Compare what the script does with what you did by hand. Typing versus pasting, a keyboard shortcut versus a click, and a pre-existing tab are common differences. A Playwright trace of the script run shows where it diverged.

Return to reproducing what someone else saw for the other layers a replay needs.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading