Write Playwright, Cypress or Puppeteer E2E tests for a user flow with role/label locators, auto-waiting assertions and credentials from env vars.
---
name: e2e-test-writer
description: Writes end-to-end browser tests for a web app user flow - login, sign-up, checkout, forms, navigation, error states - using the project's existing framework (Playwright, Cypress or Puppeteer), with role- and label-based locators, auto-waiting assertions and credentials read from environment variables. Use when asked to write E2E tests, browser tests, UI flow tests, or Playwright or Cypress specs for a page or feature.
compatibility: Running the tests needs Node.js, the chosen framework installed in the project, and a running app (local dev server or test environment). Writing them needs only read access to the app code.
metadata:
version: "1.0.0"
---
# E2E Test Writer
Writes tests that act like a user and fail only when the user-visible behavior breaks. Default to the framework already in the repo (`@playwright/test`, `cypress`, or `puppeteer` in `package.json`); suggest Playwright Test only if none is present, and ask before adding a dependency.
## Inputs to collect
- The flow to cover, step by step, and the expected visible outcome (URL, heading, message).
- Base URL of the app under test and how it starts (`playwright.config` `use.baseURL` / `webServer`, `cypress.config` `baseUrl`).
- Test account handling: environment variable names for credentials, or a seeded test user. Never use real personal accounts or hard-code secrets in the spec.
- The relevant page/component source, so locators match real labels, roles and button names.
## Workflow
1. **Read the UI code** for the flow: labels, button text, headings, `data-testid` attributes already present, and the success/error states. Do not guess selectors.
2. **Choose locators in this order** (Playwright names; Cypress equivalents via Testing Library if installed):
1. `page.getByRole('button', { name: 'Sign in' })`
2. `page.getByLabel('Email')`, `getByPlaceholder`, `getByText` for non-interactive text
3. `page.getByTestId('checkout-total')` (matches `data-testid` by default)
4. CSS/XPath only as a last resort, with a comment why.
3. **Write one test per behavior**: happy path, one or two meaningful failure paths (validation error, wrong password, empty cart), and a navigation check if relevant. Each test starts from a known state (`beforeEach` goto, fresh test data or API seeding), and does not depend on another test's leftovers.
4. **Assert with auto-waiting (web-first) assertions**: `await expect(page).toHaveURL(...)`, `await expect(locator).toBeVisible()`, `toHaveText`, `toContainText`. No fixed sleeps (`waitForTimeout`) and no manual polling.
5. **Keep secrets out**: read credentials from `process.env` and skip with a clear reason when they are missing. For many logged-in tests, use a setup project that saves `storageState` once instead of logging in through the UI every time.
6. **Run what you can**: `npx playwright test tests/login.spec.ts` (or `npx cypress run --spec ...`) against the running app. Report the actual result. If the app or browsers are not available, say the tests were written but not executed.
## Example (Playwright Test, TypeScript)
```ts
import { test, expect } from '@playwright/test';
const email = process.env.E2E_USER_EMAIL ?? '';
const password = process.env.E2E_USER_PASSWORD ?? '';
test.describe('login', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/login');
});
test('signs in with valid credentials and lands on the dashboard', async ({ page }) => {
test.skip(!email || !password, 'Set E2E_USER_EMAIL and E2E_USER_PASSWORD for a test account');
await page.getByLabel('Email').fill(email);
await page.getByLabel('Password').fill(password);
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page).toHaveURL(/\/dashboard$/);
await expect(page.getByRole('heading', { level: 1 })).toHaveText(/welcome/i);
});
test('shows an error and stays on /login for a wrong password', async ({ page }) => {
await page.getByLabel('Email').fill('nobody@example.test');
await page.getByLabel('Password').fill('wrong-password');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByRole('alert')).toContainText(/invalid email or password/i);
await expect(page).toHaveURL(/\/login$/);
});
});
```
Assumes `use.baseURL` is set in `playwright.config.ts` and the login form has visible labels "Email" and "Password", a "Sign in" button, an `h1` on the dashboard and an error element with `role="alert"`. Adjust names to the real UI.
## Output format
- Spec file(s) with the path where they belong (match the existing test directory).
- Notes on required env vars, test data and config.
- Execution status: command run and its pass/fail output, or "not executed" with the reason.
## Limitations
- E2E tests cover flows, not every branch; keep unit tests for logic.
- Flaky tests usually mean shared state or a missing assertion on the state change; fix the cause instead of adding retries or sleeps.
- Does not create real user accounts or run against production unless the user explicitly provides a safe test environment.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A team with Playwright Test already configured wants regression coverage for its login page before redesigning it.
Write Playwright tests for /login. The form has labels 'Email' and 'Password' and a 'Sign in' button. Success goes to /dashboard with an h1 'Welcome back'. A wrong password shows role=alert 'Invalid email or password' and stays on /login. Test account creds are in E2E_USER_EMAIL / E2E_USER_PASSWORD.
tests/login.spec.ts like the skill's example: getByLabel/getByRole locators, a happy-path test that skips with a reason when the env vars are absent, a wrong-password test asserting the alert text and that the URL stays /login, only web-first assertions (toHaveURL, toHaveText, toContainText), no waitForTimeout and no hard-coded real credentials. Followed by the run command and either its actual result or 'not executed' with the reason.
AI Agent Skills are reusable workflow packages that extend your AI coding assistant with multi-step capabilities. Install e2e-test-writer by following these steps.
SKILL.md inside, in the location documented by your client. Do not assume every AI application reads the same directory.Requires an Agent Skills-compatible client; supported locations and activation vary by version. Read the SKILL.md setup guide. MCP connects tools and data and does not, by itself, install a skill.