Files
e2e/docs/coding-agents.mdx
T

137 lines
4.7 KiB
Plaintext

---
title: Coding agents
description: Give a coding agent the skill, a live app session, and reports it can use to write and debug tests.
---
`e2e init` installs a skill for writing and running tests, plus MCP
configuration for inspecting the app. Coding agents use the CLI to run
tests and the reports to investigate failures.
## The skill
```bash
npx e2e init
```
| Path | Used by |
| --- | --- |
| `.agents/skills/e2e/` | Agents that read `.agents/skills` |
| `.claude/skills/e2e/` | Claude Code |
Both directories contain `SKILL.md` and topic references. Run `init` again
after upgrading e2e to refresh them.
Without `init`, `npx skills add tester-army/e2e` installs the skill from the
repository, and `npx e2e guide [topic]` prints it from the
installed package.
For agents that need an explicit pointer, add this line to your project
instructions. `init` leaves those files unchanged:
```md title="AGENTS.md"
End-to-end tests use e2e; read .agents/skills/e2e/SKILL.md before writing or running one.
```
## How an agent looks at the app
`e2e mcp` lets the coding agent open and interact with one target. Its
`locate` tool checks a `screen.*` locator against the current app and returns
the test code when exactly one element matches.
`init` registers the server in `.mcp.json` for Claude Code and
`.cursor/mcp.json` for Cursor. To register it with Claude Code by hand:
```bash
claude mcp add e2e -- npx e2e mcp
```
See the [MCP server reference](/reference/mcp) for its tools, session rules,
and flags. Run tests and read reports through the CLI.
## How an agent writes a test
The skill tells the coding agent to read the config and an existing test
first. It uses `agent.act` for one goal at a time and checks the outcome
with `expect`. Known fields and exact values can use `screen` directly:
```ts title="tests/checkout.e2e.ts"
import { test, expect, credentials } from 'e2e';
test('a signed-in user reaches checkout', async ({ screen, app, agent }) => {
const user = credentials.user('customer');
await app.open('/login');
await screen.getByLabel('Email').fill(user.username);
await screen.getByLabel('Password').fill(user.password);
await screen.getByRole('button', { name: 'Sign in' }).click();
await expect(screen.getByRole('heading', { name: 'Welcome back' })).toBeVisible();
await agent.act('Add the cheapest item on the page to the cart and open the cart');
await expect(screen.getByRole('heading', { name: 'Your cart' })).toBeVisible();
});
```
The main rules are:
- One `agent.act` per goal, followed by an `expect`. The assertion makes the
step's cache entry eligible for replay, and it is what fails when the agent
did the wrong thing.
- No sleeps. Locators poll, actions wait for their target, and `expect`
retries until its timeout.
- Secrets through `credentials.user(name)`, declared in the config, never
literal in a test.
## How an agent runs it
```bash
npx e2e run tests/checkout.e2e.ts
```
Run one file while iterating. For model configuration and credentials, see
[Choosing a model](/models). Fix CLI, config, and collection errors before
retrying a run.
## How an agent reads the result
Every run writes `.e2e/report.json`. For a report that is easier to read,
run:
```bash
npx e2e run tests/checkout.e2e.ts --reporter list,markdown
```
This writes `.e2e/summary.md` and a page for each failed or flaky test under
`.e2e/failures/`. Start with the failure page. It includes the failing line,
steps, recent model turns, and the screen at failure.
When reading JSON, check `run.errors` for run-level failures, then the last
attempt of each failed result. Its `error`, `steps`, and `artifacts` show
what failed and how far the test got. `--reporter json` also prints the JSON
report to stdout.
For model-call details, use `--ai-trace` and read the trace with
[unbox-ai](/debugging#agent-steps). Avoid opening the raw trace in an agent's
context; it repeats prompts and can be several megabytes.
## How an agent treats the cache
A verified `agent.act` can replay from `.e2e/cache/` on later runs. Use
`--no-cache` to check the live agent behavior after changing a test or to
investigate a replay failure. Pair it with `--ai-trace` to capture every
model call in the flow.
Do not edit recordings by hand. A failed read-write run removes implicated
entries so a later verified run can replace them. See
[Caching agent steps](/cache).
<CardGroup cols={2}>
<Card title="CLI reference" icon="terminal" href="/reference/cli">
init, guide, mcp, and run, flag by flag.
</Card>
<Card title="MCP server" icon="server" href="/reference/mcp">
Inspect the app before writing a test.
</Card>
<Card title="Debugging a run" icon="bug" href="/debugging">
The report, the artifacts, and the agent flags.
</Card>
</CardGroup>