Your First Playwright Test
Install Playwright, open a real website, and assert the page title — all in about 15 minutes.
1 Goal
By the end of this exercise you will have a working Playwright project on your machine, a single test file that opens the public playwright.dev homepage, asserts the page title contains "Playwright", and drops a screenshot into your project folder. You will also have run the test once and seen it pass in the terminal. That is enough foundation to build every other test in this track.
2 Install the tool (Node.js + Playwright)
Playwright runs on Node.js. You will install Node.js first, then let Playwright's own installer pull down the browsers and a starter project.
-
Download and install Node.js 20 LTS
Go to nodejs.org and grab the LTS installer for your OS. Accept all defaults. The installer includes
npmandnpx, which you will use below. -
Verify the install
Open a fresh terminal window (important — an existing window won't see the new PATH) and run:
node --version npm --version
node --version npm --version
node --version npm --version
You should see something like
v20.11.1and10.2.4. Anything on v18 or newer is fine. -
Create a project folder and install Playwright
Pick a folder you can find again (Desktop is fine). The
initcommand scaffolds the config, example tests, and a GitHub Actions workflow — answer the prompts as shown.cd $HOME\Desktop mkdir pw-practice-01 cd pw-practice-01 npm init playwright@latest
cd ~/Desktop mkdir pw-practice-01 cd pw-practice-01 npm init playwright@latest
When prompted, choose: TypeScript → tests folder → No GitHub Actions (not needed yet) → Yes install browsers.
-
Confirm browsers downloaded
The installer downloads Chromium, Firefox, and WebKit into a cache. If you skipped the browser step above, run this now:
This is safe to re-run — it is idempotent.
npx playwright install
3 Project setup
The installer created a folder that looks like this:
pw-practice-01/ ├── node_modules/ ├── tests/ │ └── example.spec.ts ├── tests-examples/ ├── package.json ├── playwright.config.ts └── .gitignore
Delete the example file — you are going to write your own.
Remove-Item tests\example.spec.ts Remove-Item tests-examples -Recurse
rm tests/example.spec.ts rm -rf tests-examples
Now create a new empty file at tests/homepage.spec.ts. Any text editor will do — VS Code is recommended if you don't have a preference.
4 Write the script
Paste this exact content into tests/homepage.spec.ts:
import { test, expect } from '@playwright/test';
test('playwright homepage has the right title', async ({ page }) => {
await page.goto('https://playwright.dev/');
await expect(page).toHaveTitle(/Playwright/);
await page.screenshot({ path: 'homepage.png', fullPage: true });
});Line-by-line:
import { test, expect } from '@playwright/test';— pulls in the test runner and the assertion helper. Every spec file starts with this.test('...', async ({ page }) => { })— declares one test. The first argument is the human-readable name you will see in the report. Thepageobject is a fresh browser tab, already opened for you.await page.goto('https://playwright.dev/')— navigates the tab.awaitis mandatory — Playwright actions are asynchronous, and forgettingawaitis the #1 cause of mysterious failures.await expect(page).toHaveTitle(/Playwright/)— the assertion. The/Playwright/is a regex that matches any title containing the word "Playwright" (currently "Fast and reliable end-to-end testing for modern web apps | Playwright"). Playwright retries this assertion for up to 5 seconds by default, so you don't have to add explicit waits.await page.screenshot(...)— saves a PNG to your project folder.fullPage: truecaptures the whole scrollable page, not just the viewport.
playwright.dev because it is stable, fast, and explicitly allows automation. Never point practice scripts at live client sites, resync.nz, or bootcamp.resync.nz — you will hammer production and waste your own bandwidth. Stick to playwright.dev and example.com for anything in this track.
5 Run & verify
From the project root, run:
npx playwright test
Expected output (timings will differ):
Running 3 tests using 3 workers 3 passed (6.2s) To open last HTML report run: npx playwright show-report
Three tests? Yes — by default the config runs your one test against Chromium, Firefox, and WebKit. That is Playwright's cross-browser-by-default behaviour. All three should pass.
Check your project folder: you will now have a homepage.png file. Open it — it should show the Playwright homepage.
Want the UI? Run:
npx playwright test --ui
This opens a watch-mode runner with a timeline, DOM snapshots per step, and a re-run button. Extremely useful when you start debugging.
6 Troubleshooting
Error: browserType.launch: Executable doesn't exist
Playwright downloads its own browser binaries separate from the Chrome you have installed. If the download was skipped or interrupted, you will see this.
Fix:
npx playwright install
If you are behind a corporate proxy, set HTTPS_PROXY before running the command.
Error: 'npx' is not recognized as an internal or external command
Node.js isn't on your PATH — usually because you are in the same terminal window you had open before installing it.
Fix: close the terminal completely, open a new one, and try again. On Windows, log out and back in if a fresh terminal still fails.
Assertion times out: Timed out 5000ms waiting for expect(page).toHaveTitle()
Either the site is slow, the title changed, or you are offline. Try opening https://playwright.dev/ manually in your browser.
Fix options: check your internet; loosen the regex to /.+/ temporarily to confirm the navigation worked; or bump the timeout in playwright.config.ts by adding timeout: 30_000 to the top-level config.
Test runs but no screenshot appears
The test failed before reaching page.screenshot(). Assertions that fail throw and stop execution.
Fix: move the screenshot call to before the assertion while you debug, or attach the screenshot to the test report using test.info().attach().
7 Challenge
Stretch yourself
Extend the test to add two more assertions without changing the navigation line:
- Assert the heading "Playwright enables reliable end-to-end testing for modern web apps." is visible. Use
page.getByRole('heading', { name: /reliable end-to-end/i })and.toBeVisible(). - Assert the "Get started" link is present —
page.getByRole('link', { name: 'Get started' })with.toBeVisible().
If you want more: add a second test in the same file that visits https://example.com and asserts the heading is "Example Domain". You now have a two-test, two-site suite.