Getting started + codegen
Record your first tests using codegen
You set up Playwright in the prep lesson and you have nine green example tests. Now we put Playwright to work against the store you're already looking at by recording our first real tests with the codegen command.
But first things first. I often see people confused about what Playwright actually is. And indeed, things got a bit blurry over the last few months. If you look at the official docs, you'll see this statement:
Playwright enables reliable web automation for testing, scripting and AI agents.

There's the Playwright library, Playwright Test, Playwright MCP and the Playwright CLI. They all have their own use cases.
If we ignore the AI tech for a moment, many people confuse the Playwright library and Playwright Test. The library and Playwright Test are two different things.
"Vanilla 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 use:
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.
import { chromium } from "playwright";
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto("https://example.com");
console.log(await page.title());
await browser.close();
Run with node title.ts. You manage the browser lifecycle, assertions, retries, and reporting yourself. Use the library for scripts and scrapers (not for testing).
Today is all about Playwright Test for end-to-end testing and the new AI agent automation tools.
Let's get used to some common Playwright Test commands and CLI flags.
Watch the example tests run
You already saw npx playwright test print nine green dots in the prep lesson. Add the --headed flag and the browsers actually pop open:
$ npx playwright test --headed
Three browsers click around on your screen and disappear. Now let's get to work and record some new tests.
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 turns them into Playwright code.
$ npx playwright codegen https://www.playwright-workshop.online/
Two windows pop open: a browser pointed at the store, and the Playwright Inspector showing generated code as you click.

Click around. Add a product to the cart. Watch the Inspector window record your actions:
import { test, expect } from "@playwright/test";
test("test", async ({ page }) => {
await page.goto("https://www.playwright-workshop.online/");
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, paste it into a new file (tests/shop.spec.ts), and run it.
$ npx playwright test tests/shop.spec.ts
Play around with codegen. If you run into errors, don't worry. We'll look at debugging Playwright tests in the following lessons.
Pay attention to the small toolbar. It also lets you add assertions on the fly!

If you don't want to copy and paste the code, you can also pass the --output option.
npx playwright codegen $YOUR_URL --output tests/new.spec.ts
VS Code user? If you want to record and control your Playwright tests from within the editor, the official extension is "okay-ish". I prefer the native tools, but maybe you'll like it!
Record at least two user flows
Record each flow 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.
Add-to-cart from home
- Run
npx playwright codegen https://www.playwright-workshop.online/. - Open a product detail page.
- Click Add item to cart.
- Change the quantity of your item in the cart.
- Close the cart.
- Copy the generated code into
tests/shop.spec.tsand run it. - Don't forget to give it a meaningful name (
test('add to cart', /*... */)).
Check the login page
- Start another
codegensession. - Navigate to the login page.
- Assert the heading with the text "Welcome back" is visible.
- Save the test and run it.
At this stage, you should have at least two test cases covering catalog and home add-to-cart behavior.