How to use the Playwright Cheat Sheet
- 1. Search or browseType what you want to do, or pick one section of the Playwright cheat sheet.
- 2. CopyEvery entry has its own Copy button, so it goes straight into your editor or notes.
- 3. DownloadSave the whole Playwright cheat sheet as plain text or Markdown for offline revision.
What's in it
Snippets use the @playwright/test runner for Node.js; Python, Java and .NET versions use the same ideas with different syntax. The 40 snippets are organised into 7 sections: Setup & running, Writing tests, Locators, Actions, Assertions, Network & auth, Config.
Every code snippet was checked to be valid JavaScript syntax, and the device names exist in Playwright's device list.
Example
A flaky test often looks like await page.click(".btn-3") followed by a fixed wait. The Playwright way is await page.getByRole("button", { name: "Submit" }).click(); then await expect(page.getByRole("alert")).toBeVisible();. Both lines wait automatically until the element is ready or the check passes, so there's no sleep to tune and the locator survives CSS class changes.
Tips for using it
- Reach for getByRole, getByLabel and getByText before CSS selectors; they mirror how users find things.
- Always await expect(...) with locator assertions; a missing await makes a check silently do nothing.
- Turn on trace: "on-first-retry" in CI. When a test fails, the trace shows every step with screenshots.
A quick look, one entry per section
- Setup & running:
npm init playwright@latest(create a project with config, an example test and browsers) - Writing tests:
import { test, expect } from "@playwright/test";(a complete test) - Locators:
page.getByRole("button", { name: "Sign in" })(best default: find by role and accessible name) - Actions:
await page.getByLabel("Email").fill("asha@example.com");(type into a field (clears it first)) - Assertions:
await expect(page.getByRole("alert")).toBeVisible();(auto-retrying check until it passes or times out) - Network & auth:
await page.route("**/api/courses", (route) => route.fulfill({ json: [{ id: 1, name: "Python" }] }));(mock an API response) - Config:
// playwright.config.ts(base URL, traces on retry and retries (import defineConfig from @playwright/test))
Search the box above for the rest of the Playwright entries, or download the full sheet.
Frequently asked questions
Were the snippets checked?
Every code snippet was parsed as valid JavaScript, and the device names used (Desktop Chrome, Pixel 7) were checked against Playwright's device list.
Playwright or Selenium?
Both are widely used. Playwright has auto-waiting and a built-in test runner; Selenium has a longer history and more language bindings.
Is the Playwright cheat sheet free?
Yes. You can search, read and copy every entry in the Playwright cheat sheet without signing up. The first time you download it, we ask once for your name and WhatsApp number; after that downloads and every other Uncodemy tool open straight away.