What is Playwright?
Playwright is an open-source framework from Microsoft for automating real web browsers. You write a script that opens a page, clicks, types and checks what the user would see, and Playwright runs it in Chromium, Firefox or WebKit (the engine behind Safari). It was first released in 2020 and is used mainly for end-to-end testing of web applications, though it also works for scraping, screenshots and general browser automation.
Official bindings exist for JavaScript/TypeScript, Python, Java and .NET (C#). The JavaScript version ships with its own test runner, @playwright/test, which is what most teams mean when they say "we use Playwright".
Library vs test runner
Interviewers often check whether you know that Playwright has two layers:
| Layer | What it gives you |
|---|---|
Browser automation library (playwright) | APIs to launch a browser, open pages, find elements, intercept network calls. You manage setup and assertions yourself. |
Test runner (@playwright/test) | Test files, fixtures such as page and context, web-first assertions with expect, parallel workers, retries, HTML report, traces, screenshots and videos. |
In Python the usual pairing is the library plus the pytest-playwright plugin, which provides similar fixtures.
A first test, line by line
Create a project with npm init playwright@latest. It asks a few questions, installs the browsers and writes a sample config. Then add tests/search.spec.ts:
import { test, expect } from '@playwright/test';
test('Get started link opens the installation page', async ({ page }) => {
await page.goto('https://playwright.dev/');
// Find elements the way a user would: by role and visible name
await page.getByRole('link', { name: 'Get started' }).click();
// Web-first assertion: retries until it passes or times out
await expect(page).toHaveURL(/.*intro/);
await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});
What happens when you run it:
- The runner starts a browser and gives the test a fresh
pageinside a new, isolated browser context (no cookies from other tests). click()waits until the link is visible, stable and enabled before clicking. You do not add sleeps.- Each
expect(...)keeps re-checking for up to 5 seconds by default, so a slow page load does not fail the test straight away. - The result is printed in the terminal, for example
1 passed (2.1s)per browser project.
Everyday CLI commands
| Command | Use |
|---|---|
npx playwright test | Run all tests, headless, in every project from the config |
npx playwright test --headed | Show the browser window |
npx playwright test --project=firefox | Run only one browser project |
npx playwright test --ui | UI mode: watch, time-travel through steps, re-run single tests |
npx playwright test --debug | Open the Playwright Inspector and step through |
npx playwright codegen https://example.com | Record your clicks and generate test code |
npx playwright show-report | Open the HTML report from the last run |
Why teams pick it
- Auto-waiting removes most timing flakiness that comes from hand-written waits.
- Browser contexts make each test start clean, and several users can be simulated in one test.
- Network control with
page.route()lets you mock APIs. See how page.route() works. - Debugging tools: the trace viewer records DOM snapshots, network and console for every step.
- One API across three browser engines, on Windows, macOS and Linux.
If you are coming from Selenium, the Playwright vs Selenium comparison explains the differences in architecture and waiting.
Common beginner mistakes
- Forgetting
await. Every Playwright action returns a promise. A missingawaitmakes the test finish early or fail with confusing errors. - Using
page.waitForTimeout(3000)as a fix. Hard sleeps slow the suite and still flake. Use assertions such astoBeVisible()instead. - Long CSS/XPath chains. Prefer
getByRole,getByLabelandgetByTestId; they survive layout changes better. - Skipping
npx playwright installon a new machine or CI agent. The npm package does not contain the browsers; the error message tells you to run this command.
Next, read about which browsers Playwright supports, or follow the longer beginner guide to Playwright testing.
Want to master Playwright hands-on?
Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.
Explore the Course →