Core concepts
Goals, assertions, and locators, and how to structure and run tests with them.
A test mixes three types of step:
With a custom executor, act and assert need no model.
import { describe, test, expect } from 'e2e';
describe('todos', () => {
test('the Done filter shows only completed todos', async ({ app, agent, screen }) => {
await app.open('/todos');
await agent.act('add todos "Buy milk" and "Walk the dog"');
await agent.act('complete "Buy milk"');
await agent.waitFor('"Buy milk" is completed and "Walk the dog" is open');
await screen.getByRole('tab', 'Done').tap();
await expect(screen.getByTestId('todo')).toHaveCount(1);
await agent.assert('the only todo listed is "Buy milk"');
});
});Each test checks one behavior in three blocks: arrange the starting state, act on the behavior, then assert the result.
Await each agent call, screen call, and expect.poll. If a test returns
while a step still runs, the test fails with STEP_NOT_AWAITED.
Goals
agent.act takes one goal and does the actions it needs. It returns when
the goal is complete and throws when the goal fails.
await agent.act('complete checkout with the test card');
await agent.act('invite {email} as an editor', { params: { email: 'ada@example.test' } });Keep each goal atomic: one outcome that one check confirms. When two parts of an instruction each leave a state worth checking, they are two goals. A test can have many goals.
await agent.act('open the Pro plan, set the seats to {seats}, and check out', {
params: { seats: 3 },
});test('checks out the Pro plan', async ({ app, agent, screen }) => {
await app.open('/plans');
await agent.act('open the Pro plan');
await agent.act('set the seats to {seats}', { params: { seats: 3 } });
await agent.waitFor('the cart shows the Pro plan with 3 seats');
await agent.act('check out');
await expect(screen.getByText('Order confirmed')).toBeVisible();
});The setup goals come first and end with a wait for the state they reach. The goal under test comes next. The assertions on its result come last. A failure then names the goal that broke. Each goal replays from its own recording and gets its own model-call and action budgets. The actions toward one outcome are not goals: opening the screen or menu that holds a control, scrolling to it, closing a banner, or filling the fields of one form.
- You control the order of the goals. The agent picks the actions.
- End the test with an
expecton the result. The replay cache records a goal only after a later check or wait passes. - Use the words that the screen shows.
- Give test data in
params. For a password, give aSecret. The model never gets its value as text. See Authentication. - For a value that changes on each run, use
unique()so the cache can replay the step:
import { unique } from 'e2e';
const email = `ada+${Date.now()}@example.test`;
await agent.act('sign up with email {email}', { params: { email: unique(email) } });act gets screenshots when it needs them. On some engines, it can tap and
type at points in the image.
If a goal fails, read the report and make the instruction more specific.
Put words that many goals use in the agent’s context.
Assertions
An assertion judges the screen. The model sees the statement, a redacted
text snapshot of the current screen, its location, and the agent’s context. It does not
see the earlier steps or the summaries of act.
agent.assertexamines the screen now. A false statement fails withASSERTION_FAILED. When the evidence is not enough, it fails withASSERTION_INCONCLUSIVE.agent.waitForwaits until a condition is true. It calls the model only when the screen or its location changes.agent.extractreads data from the screen into a Standard Schema type, for example Zod.
import { expect } from 'e2e';
import { z } from 'zod';
await agent.assert('the dashboard shows a trial badge');
await agent.waitFor('a download link appears', { timeout: 120_000 });
await agent.assert('the chart trends upward', { vision: true });
const data = await agent.extract('every todo title', {
schema: z.object({ titles: z.array(z.string()) }),
});
expect(data.titles).toContain('Buy milk');For text that the model wrote, examine the meaning, not the full sentence. When the exact text is important, use a locator.
For a visual check, such as a chart or a layout, add vision: true. To
judge only the screenshot, use vision: 'only'. Images add input tokens.
See the agent reference.
Locators
A locator finds a known control, with no model call. Use one when you know the exact action or the exact value.
// Role and name first, label second, test ID last.
await screen.getByRole('button', 'Sign in').tap();
await screen.getByLabel('Email').fill('ada@example.test');
// Scope into a row.
const row = screen.getByRole('listitem').filter({ hasText: 'Design review' });
await row.getByRole('button', 'Archive').tap();
// Gestures and coordinates.
await screen.swipe({ direction: 'up' });
await screen.getByRole('image', 'Seat map').tap({ position: { x: 120, y: 48 } });
// Assertions retry until they pass or time out.
await expect(screen.getByRole('status')).toHaveText('2 remaining');- An action, a read, or a single-node assertion fails when its query finds
more than one node. Use
.filter(),.first(), or.nth()to find one node. - A direct read, such as
textContent(), waits for its match, then reads once and does not retry for a value.
See the screen reference and the expect reference. For routes, cookies, dialogs, and downloads, see Working with the browser.
Organize tests
import { beforeEach, describe, test } from 'e2e';
describe('billing', { tags: ['billing'] }, () => {
beforeEach(async ({ app }) => { await app.open('/billing'); });
test('upgrades', { retries: 2, timeout: 60_000 }, async ({ agent }) => {
await agent.act('upgrade to Pro');
});
});
test('needs two organizations', async ({ app }) => {
test.skip((await countOrganizations()) < 2, 'nothing to switch to');
await app.open('/organizations');
});- Each test runs in its own attempt. Start it near the screen it checks, so every run skips the clicks that lead there. Create its data in fixtures or hooks. In a browser, restore a saved session and open the screen by its path. On a device, open a deep link.
test.skip(condition, reason)stops the test asskippedwhen the condition is true.- For a flow across many tests, set
{ serial: true }on the group. The tests run in sequence on one worker and retry together. - For setup and cleanup, use
test.extend. See Writing fixtures. - Import
testfrom@e2e-dev/webor@e2e-dev/mobileto type thebrowserordevicefixture.
The test reference shows all options.
Run one test
npx e2e run tests/signup.e2e.ts:12 # the test at line 12
npx e2e run --grep checkout # tests with a matching title
npx e2e run --target web # one targetpnpm exec e2e run tests/signup.e2e.ts:12 # the test at line 12
pnpm exec e2e run --grep checkout # tests with a matching title
pnpm exec e2e run --target web # one targetbunx e2e run tests/signup.e2e.ts:12 # the test at line 12
bunx e2e run --grep checkout # tests with a matching title
bunx e2e run --target web # one targetThe CLI reference shows all flags.
