Free lesson

Create an isolated test project

Create an isolated test project — Test an AI-built site with Playwright. Install the official runner; Create a synthetic site and server; Configure baseURL and webServer. Practical assignment: Lab evidence.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • Install the official runner
  • Create a synthetic site and server
  • Configure baseURL and webServer

Install the official runner

Initialize Playwright in a new directory. Choose TypeScript and tests; defer the generated CI workflow until lesson six. Install only the supported browser binaries needed for the exercise.

The target is a synthetic local site you own, using a separate browser context created by the test runner. It contains no real accounts, purchases or messages. Write assertions for visible behavior rather than for how an assistant happened to implement the page. A passing test covers only its stated cases; it does not prove complete accessibility, security or every device combination. Keep manual review for areas the suite does not exercise.

mkdir browser-lab
cd browser-lab
npm init playwright@latest
npx playwright --version
npx playwright install chromium

Create a synthetic site and server

Save the three files below at project root. The form changes local status only; privacy is a real local route. No external requests or real subscriptions occur.

Keep each file’s language separate when copying the combined listing: HTML belongs in index.html and privacy.html, while the Node module belongs in server.mjs. The listing is a teaching bundle, not one executable file. Remove generated example specs that target external demonstration sites before running this suite.

Save examples under the shown test paths and keep application startup separate from test execution. Use the configured baseURL instead of repeating environment-specific addresses. Let web-first assertions wait for observable results; fixed sleeps make failures slower without proving readiness. When a test fails, preserve the trace and reproduce its exact input before editing selectors. A missing accessible label is often an application defect, not a reason for a brittle CSS selector.

<!-- index.html -->
<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Workshop demo</title><style>body{font:1.1rem/1.6 system-ui;margin:1rem;max-width:60rem}input{max-width:100%;font:inherit}p{overflow-wrap:anywhere}:focus-visible{outline:3px solid #9b4300}</style><main><h1>Workshop demo</h1><a href="#details">Session details</a><section id="details"><h2>Session details</h2><p>Tuesday, six places. Fictional example.</p></section><form><label for="email">Fictional email</label><input id="email" type="email" required><button>Check demo</button><p role="status"></p></form><a href="/privacy">Privacy</a></main><script>document.querySelector('form').onsubmit=e=>{e.preventDefault();document.querySelector('[role=status]').textContent='Demo checked. Nothing sent.';};</script></html>
<!-- privacy.html: save the following as a separate file -->
<!doctype html><html lang="en"><title>Privacy demo</title><main><h1>Privacy demo</h1><p>No submission is sent by this synthetic page.</p><a href="/">Home</a></main></html>
// server.mjs: separate file
import http from 'node:http';import {readFile} from 'node:fs/promises';
const routes={'/':'index.html','/privacy':'privacy.html'};
http.createServer(async(req,res)=>{const file=req.method==='GET'?routes[req.url]:null;if(!file){res.writeHead(404);return res.end('Not found');}try{const data=await readFile(file);res.writeHead(200,{'Content-Type':'text/html; charset=utf-8'});res.end(data);}catch{res.writeHead(500);res.end('Fixture unavailable');}}).listen(4173,'127.0.0.1');

Configure baseURL and webServer

Replace the generated config with this focused Chromium setup. The runner starts a fresh local server; an occupied port is a setup error rather than another app to reuse silently.

Keep tests independent and use only fictional inputs such as learner@example.test. Do not reuse a personal logged-in browser profile or copy production credentials into artifacts. Screenshots and traces can retain page text, requests and entered values, so inspect them before sharing. Treat a retry pass as evidence of possible flakiness, not permission to ignore the first failure. Record command, browser project and actual result when reporting coverage.

// playwright.config.ts
import {defineConfig,devices} from '@playwright/test';
export default defineConfig({testDir:'./tests',use:{baseURL:'http://127.0.0.1:4173',trace:'retain-on-failure'},webServer:{command:'node server.mjs',url:'http://127.0.0.1:4173',reuseExistingServer:false},projects:[{name:'chromium',use:{...devices['Desktop Chrome']}}]});

Practical assignment

Lab evidence

Continue to learning

Course outline

  1. Create an isolated test project
  2. Use fixtures for reliable setup
  3. Choose selectors users can recognize
  4. Test forms and navigation outcomes
  5. Check responsive behavior and isolation
  6. Run in CI and interpret failures