Getting Started With Playwright: Your First Reliable Test in TypeScript
This guide takes you from an empty folder to a reliable Playwright test running in CI. It assumes basic JavaScript or TypeScript and Node.js installed.
1. Install
mkdir shop-tests && cd shop-tests
npm init playwright@latest
Accept TypeScript, a tests folder, and the GitHub Actions workflow. This installs @playwright/test, the browsers and an example config.
Locators by role and accessible name (getByRole) survive redesigns and also nudge the app towards accessibility
Web-first assertions (await expect(locator).toHaveText(...)) retry until they pass or time out, so no sleep() is needed
4. Run and debug
npx playwright test --ui # interactive runner
npx playwright test --headed # watch the browser
npx playwright test --debug # step through
npx playwright codegen https://your-app.example # record locators
When a test fails in CI, open the trace (npx playwright show-trace trace.zip) to see every action, DOM snapshot, network request and console message. It's the single most useful debugging feature.
5. Keep tests independent
Each test should create the state it needs and not depend on other tests:
(The endpoint here is illustrative. Use whatever setup API your app provides.)
6. Sign in once, reuse the session
Use a setup project that signs in and saves storageState, then reuse it in other tests so you don't log in through the UI every time. See "Authentication" in the Playwright docs.
7. Run in CI
The generated GitHub Actions workflow installs browsers, runs the tests and uploads the HTML report as an artifact. Keep CI runs:
Fast: parallel workers, sharding for big suites
Deterministic: stable test data, no shared accounts between parallel tests
Informative: traces on retry, reports as artifacts
Reliability checklist
No hard waits (waitForTimeout) except to diagnose
Role, label or test ID locators, no long CSS chains
Each test independent and runnable alone
Assertions check user-visible outcomes
Retries only to collect traces, not to hide flakiness
What to automate next
Start with your app's smoke tests and money paths: sign in, the core action, checkout or booking. Then add regression tests for bugs that have escaped before.
Want ideas for high-value tests? Find the bugs by hand first in QAUnlocked Practice Labs, then write the Playwright test that would have caught each one. It's a great exercise and a great portfolio piece.
An honest comparison of the three most popular browser automation tools, covering architecture, language support, browsers, speed, debugging and ecosystem, and which to pick for your situation.
Flaky tests destroy trust in automation. The most common causes (timing, shared state, order dependence, environments, brittle locators), how to diagnose them, and fixes that actually work.
Most broken UI tests break on locators. A practical priority order for locators (role, label, text, test ID, CSS), with examples in Playwright and Cypress and the anti-patterns to avoid.
Be the first to comment.