Getting started + codegen

Record your first tests using codegen

You set up Playwright in the prep lesson and you have six green example tests. Now we put Playwright to work — against the store you're already looking at — by recording our first real tests with codegen.

Playwright homepage

Note

The Playwright library and Playwright Test are two different things.

Playwright is the automation library; Playwright Test is the test framework built on top of it. Throughout this workshop, "Playwright" and "PWT" always refer to the test framework.

The same flow looks very different depending on which one you reach for:

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

test("homepage has the right title", async ({ page }) => {
  await page.goto("https://example.com");
  await expect(page).toHaveTitle(/Example/);
});

Run with npx playwright test. Fixtures, parallelism, retries, reporters, traces, and the HTML report all come for free.

Watch the example tests run

You already saw npx playwright test print six green dots in prep. Add --headed and the browsers actually pop open:

$ npx playwright test --headed

Three browsers click around your screen and disappear. Let's record new tests with codegen.


codegen — record a real test against the store

codegen opens a browser, follows you around, and writes the test for you. It listens to clicks, fills, and navigations and emits Playwright code in the right format.

$ npx playwright codegen https://next-example-store-stefan-judis.vercel.app/

Two windows pop open: a browser pointed at the store, and the Playwright Inspector showing generated code as you click.

Playwright codegen recording against the workshop store

Click around. Add a product to the cart. Watch the right-hand panel fill in:

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

test("test", async ({ page }) => {
  await page.goto("https://next-example-store-stefan-judis.vercel.app/");
  await page.getByRole("link", { name: "The Multi-managed Snowboard" }).click();
  await page.getByLabel("Add item to cart").click();
});

When you're happy, copy the test out, paste it into a new file under tests/, and run it.

$ npx playwright test tests/shop.spec.ts

Play around with codegen. Pay attention to the small toolbar — it also lets you add assertions on the fly!

Codegen's toolbar suggesting an assertion for the Workshop heading

Note

If you don't want to copy and paste the code you can also apply the --output param.

npx playwright codegen $YOUR_URL --output tests/new.spec.ts

Recording from VS Code

Most of the CLI lives inside the official Playwright VS Code extension. Click Record new in the Testing sidebar and you get the same recorder without leaving the editor.

Recording a test in VS Code

Use whichever you prefer — they generate the same code.


Hands-on — record three flows

Record each in a fresh codegen session, paste into tests/shop.spec.ts, and confirm npx playwright test shop.spec.ts is green. Don't worry about assertions yet — we'll add those in the assertions lesson.

Exercise 1 of 3

Add-to-cart flow

  1. Run npx playwright codegen https://next-example-store-stefan-judis.vercel.app/.
  2. Open a product detail page.
  3. Click Add to cart.
  4. Open the cart and confirm it's visible
  5. Close the cart
  6. Copy the generated code into tests/shop.spec.ts and run it.
Exercise 2 of 3

Search flow

  1. Start a fresh codegen session.
  2. Use the search input to search for hydrogen.
  3. Open one of the results and add it to the cart.
  4. Save the test and run it.
Exercise 3 of 3

Login flow

  1. Start a fresh codegen session.
  2. Navigate to /login.
  3. Log in with the credentials shown on the form.
  4. Save the test and run it.