E2E tests for a web app with Playwright agents
Web Development Lab
Project description
The participant learns how to ask the agent for a test plan and then for maintainable tests.
Tools used
The agent can be replaced with any similar coding agent; the steps are written so that they do not depend on a single tool.
Requirements
- Node.js (a version Playwright supports)
- A local web app
Steps
Installation
Code / commandsnpm init playwright@latestThe test plan
Prompt for this stepRead the sign-in pages ...Tests and running
Prompt for this stepTurn the plan into tests ...Code / commandsnpx playwright test
Ready-made prompts
Read the sign-in and sign-up pages in this project and write a test plan in Markdown: positive cases, negative cases and edge cases.Turn the plan into Playwright Test tests in TypeScript, and use getByRole and getByLabel instead of CSS selectors.An example of the resulting code
import { test, expect } from '@playwright/test';
test('login shows error for wrong password', async ({ page }) => {
await page.goto('http://localhost:3000/login');
await page.getByLabel('Email').fill('user@example.com');
await page.getByLabel('Password').fill('wrong');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByRole('alert')).toBeVisible();
});An illustrative example of what the agent is expected to produce; the actual output may differ, so always review and test it.
Tests
- npx playwright test
- npx playwright show-report
Expected results
- An HTML report of the results
Common mistakes and their fixes
| Mistake | Fix |
|---|---|
| Flaky tests caused by fixed waits | Use expect, which waits automatically, instead of waitForTimeout |
