#What Playwright is
AddedPlaywright is a tool that drives real browsers from code: it opens Chromium, Firefox or WebKit (Safari's engine), loads your app, clicks, types and reads the page exactly as a user would, and lets you check what happened. Microsoft released it in January 2020; it was started by the engineers who had built Puppeteer at Google, with the goal of one API for every modern browser engine instead of Chrome only.
It comes in two parts, and it helps to keep them apart in your head:
- The library (
playwright): launch a browser, open pages, click and read. You can use it in any Node script: scraping, generating PDFs, automating a form. It knows nothing about tests. - The test runner (
@playwright/test): the part you use for testing. It finds your test files, gives each test a fresh browser page, runs tests in parallel, retries, takes screenshots and traces on failure, and reports the results. It includes the library.
The mental model: the library is a remote control for a browser; the test runner is a test lab built around that remote control, with clean rooms (a fresh page per test), a scheduler (workers running tests in parallel) and a lab notebook (the reports and traces). In interviews, "Playwright" almost always means the test runner.
Here's the library on its own: a plain Node script that opens the Practice Shop, signs in and prints what it sees. There's no test and no expect, just a browser being driven.
import { chromium } from "playwright";
const browser = await chromium.launch(); // starts a real (headless) Chromium
const page = await browser.newPage(); // a new tab in a fresh, empty profile
await page.goto("https://rohit-playwright.pages.dev/practice/");
console.log("Title:", await page.title());
await page.getByLabel("Username").fill("rohit");
await page.getByLabel("Password").fill("playwright");
await page.getByRole("button", { name: "Sign in" }).click();
await page.waitForURL("**/practice/shop"); // signing in takes half a second
console.log("Now at:", new URL(page.url()).pathname);
console.log("Heading:", await page.getByRole("heading", { level: 1 }).textContent());
await browser.close();Title: Sign in · Practice Shop
Now at: /practice/shop
Heading: Productschromium.launch()starts a real Chromium process (headless: no window) and returns aBrowser. Nothing is shared with the Chrome you browse with: no cookies, no history, no extensions.browser.newPage()creates a fresh browser context (an empty profile, explained below) with one tab in it, so the shop sees a first-time visitor with nothing in localStorage.page.goto()loads the sign-in page and waits for itsloadevent;page.title()reads the<title>, which is why the first line is "Sign in · Practice Shop".getByLabel("Username")finds the input by its visible label, the way a person would, andfill()types into it.getByRole("button", { name: "Sign in" })finds the button by its role and text, andclick()presses it.- The shop takes 500 ms to sign in (it pretends to call a server), so the script waits for the URL to become
/practice/shopinstead of guessing a delay. Then it reads the page's main heading: "Products". browser.close()ends the browser process. Forget it and the script never exits, because the browser is still running.
The test runner wraps all of this. You don't launch browsers or close them; each test asks for a page and gets a fresh one, and you check things with expect:
import { test, expect } from "@playwright/test";
test("rohit can sign in", async ({ page }) => {
await page.goto("/practice/");
await page.getByLabel("Username").fill("rohit");
await page.getByLabel("Password").fill("playwright");
await page.getByRole("button", { name: "Sign in" }).click();
await expect(page).toHaveURL("/practice/shop");
await expect(page.getByRole("heading", { level: 1 })).toHaveText("Products");
});Running 1 test using 1 worker
✓ 1 [chromium] › tests/sign-in.spec.ts:3:1 › rohit can sign in (962ms)
1 passed (1.4s)test("…", async ({ page }) => …)declares a test. The{ page }is a fixture: the runner launches the browser, makes a new context and page for this test, hands it over, and closes it afterwards. You never writelaunch()orclose().page.goto("/practice/")is relative: the config'sbaseURL(https://rohit-playwright.pages.dev) is put in front. Chapter 2 shows the config.- The three lines that sign in are the same as in the script.
await expect(page).toHaveURL("/practice/shop")doesn't check once: it keeps checking until the URL matches or 5 seconds pass. That's why there's nowaitForURLhere: the assertion is the wait.- The output is the runner's
listreporter: one line per test with the browser project ([chromium]), the file and line, the test's title and how long it took, then a summary. A failing test would print✘and the reason.