mirror of
https://github.com/tester-army/e2e.git
synced 2026-12-03 04:45:58 +08:00
201 lines
7.4 KiB
Plaintext
201 lines
7.4 KiB
Plaintext
---
|
|
title: Custom executors
|
|
description: Customize the agent loop or implement your own step executor.
|
|
---
|
|
|
|
Use a custom executor when a `system` prompt and [project tools](/tools) are
|
|
not enough. There are two options:
|
|
|
|
1. `createToolLoopExecutor` keeps the built-in loop with your prompt and
|
|
tools. It handles budgets, repeated-call detection, verdicts, usage
|
|
accounting, and debug transcripts.
|
|
2. `StepExecutor` lets you implement `runStep(ctx)` yourself. A model and
|
|
the AI SDK are optional.
|
|
|
|
Both receive a deadline, redacted observations, and checked action methods.
|
|
The runner records their verdicts.
|
|
|
|
## Keep the loop, change the vocabulary
|
|
|
|
This example gives the agent tools to read the screen and tap a button by
|
|
its accessible name.
|
|
|
|
```ts title="e2e.config.ts"
|
|
import type { E2EConfig } from 'e2e';
|
|
import { createToolLoopExecutor } from 'e2e/agent';
|
|
import { web, surfaceOf } from '@e2edev/web';
|
|
import { gateway, tool } from 'ai';
|
|
import { z } from 'zod';
|
|
|
|
const engine = web({ url: 'http://localhost:3000' });
|
|
|
|
export default {
|
|
targets: [{ engine }],
|
|
agents: {
|
|
default: createToolLoopExecutor({
|
|
name: 'button-agent',
|
|
model: gateway('openai/gpt-5.6-luna'),
|
|
system: 'Read the screen before acting. Address buttons by their visible label.',
|
|
buildPrompt: (ctx) => ctx.step.instruction,
|
|
tools: (ctx, { guard }) => ({
|
|
observe: tool({
|
|
description: 'Read the screen as text',
|
|
inputSchema: z.object({}),
|
|
execute: () =>
|
|
guard(() => ctx.budgets.runTool({ name: 'observe', mutates: false }, async () => {
|
|
const observation = await ctx.observe();
|
|
return observation.text;
|
|
})),
|
|
}),
|
|
tap: tool({
|
|
description: 'Tap a button by its visible label',
|
|
inputSchema: z.object({ label: z.string() }),
|
|
execute: ({ label }) =>
|
|
guard(() => ctx.budgets.runTool({ name: 'tap', mutates: true }, async () => {
|
|
await surfaceOf(engine)!.page().getByRole('button', { name: label, exact: true }).click({
|
|
timeout: Math.max(1, ctx.budgets.remainingMs()),
|
|
});
|
|
return `Tapped ${label}.`;
|
|
})),
|
|
}),
|
|
}),
|
|
}),
|
|
},
|
|
} satisfies E2EConfig;
|
|
```
|
|
|
|
`buildPrompt` returns a string or message history. Use `prepareMessages`
|
|
between turns to shorten the history or return `{ messages, stop }` when
|
|
the step is not progressing.
|
|
|
|
`guard()` stops new tool calls once the step is concluding. `runTool` reserves
|
|
an action slot, serializes mutations, and records the outcome.
|
|
|
|
A mutating tool must call its engine directly, as `tap` does above. Calling
|
|
`ctx.actions` or `ctx.observe` from inside it would queue work behind its own
|
|
operation and deadlock. Keep tool bodies short and support cancellation. The
|
|
step deadline cannot cancel an underlying operation that ignores its signal.
|
|
|
|
## Replace the executor
|
|
|
|
This model-free executor supports one assertion,
|
|
`agent.assert('the screen says Ready')`. Other instructions return a blocked
|
|
verdict so the executor cannot claim to have checked something it does not
|
|
understand.
|
|
|
|
```ts title="e2e.config.ts"
|
|
import type { E2EConfig, StepExecutor } from 'e2e';
|
|
import { web } from '@e2edev/web';
|
|
|
|
const scripted: StepExecutor = {
|
|
name: 'scripted',
|
|
version: '1',
|
|
cache: 'off',
|
|
async runStep(ctx) {
|
|
if (ctx.step.kind !== 'assert' || ctx.step.instruction !== 'the screen says Ready') {
|
|
return {
|
|
status: 'blocked',
|
|
errorCode: 'AUTOMATION_UNSUPPORTED',
|
|
summary: 'This executor only checks whether the screen says Ready.',
|
|
};
|
|
}
|
|
const screen = await ctx.observe();
|
|
const ready = screen.text.includes('Ready');
|
|
return {
|
|
status: ready ? 'passed' : 'failed',
|
|
summary: ready ? 'The screen says Ready.' : 'Ready is absent from the screen.',
|
|
};
|
|
},
|
|
};
|
|
|
|
export default {
|
|
targets: [{ engine: web({ url: 'http://localhost:3000' }) }],
|
|
agents: { default: scripted },
|
|
} satisfies E2EConfig;
|
|
```
|
|
|
|
The executor receives this context:
|
|
|
|
| Member | What it is |
|
|
| --- | --- |
|
|
| `ctx.step` | The instruction, params, and `index` in the attempt's timeline |
|
|
| `ctx.observe({ tree, pixels })` | Redacted screen text, with an optional node tree and masked screenshot |
|
|
| `ctx.actions` | Checked and recorded actions, such as `tap`, `type`, `typeSecret`, and `navigate` |
|
|
| `ctx.target.verbs` | The actions supported by the target's engine. Offer only these to the model. |
|
|
| `ctx.pixelsTainted` | True after a secret fill. Screenshots are withheld for the rest of the attempt. |
|
|
| `ctx.ledger` | Completed steps in the test |
|
|
| `ctx.attempt` | Test and attempt ids, retry index, abort signal, and a `memory` map that is never reported |
|
|
| `ctx.budgets` | Accounting for custom tools and model calls |
|
|
|
|
`tapAt(point)` uses a node id when a listed control occupies the point and
|
|
otherwise calls the engine's coordinate action. `hitTest(point)` finds the
|
|
listed control without acting. Keyboard-capable engines also provide
|
|
`typeText`, `pressKey`, and `dismissKeyboard` for the focused control.
|
|
|
|
If semantic capture fails, an observation sets `treeUnavailable: true` and
|
|
omits `tree`. It includes a screenshot when policy allows one, even if pixels
|
|
were not requested. Earlier node ids are invalid, and a missing tree does
|
|
not prove a control is absent. Use fresh ids once the tree returns.
|
|
|
|
Secrets in params arrive as name-only placeholders.
|
|
`ctx.actions.typeSecret(target, name)` fills an authorized field without
|
|
passing the plaintext through your executor.
|
|
|
|
A custom executor handles `act` and `assert` through `runStep`. Both use the
|
|
act budgets and default to `config.timeout`. The `assert` options `vision`
|
|
and `screenshot` raise `UNSUPPORTED_CAPABILITY` because your executor chooses
|
|
its observations.
|
|
|
|
`waitFor` and `extract` still use the built-in judgments. They require a
|
|
configured model and raise `MODEL_UNAVAILABLE` without one.
|
|
|
|
### Driving the page yourself
|
|
|
|
Use `surfaceOf(handle)` from `@e2edev/web` to access the current
|
|
attempt's `Page` and `BrowserContext`. Both accessors throw `INVALID_STATE`
|
|
outside an active attempt.
|
|
|
|
```ts
|
|
import type { E2EConfig, StepExecutor } from 'e2e';
|
|
import { web, surfaceOf } from '@e2edev/web';
|
|
|
|
const engine = web({ url: 'http://localhost:3000' });
|
|
const own: StepExecutor = {
|
|
name: 'own-tools',
|
|
version: '1',
|
|
cache: 'off',
|
|
async runStep(ctx) {
|
|
const page = surfaceOf(engine)!.page(); // the page app.open() navigated
|
|
await ctx.budgets.runTool({ name: 'click', mutates: true }, () =>
|
|
page.getByRole('button', { name: 'Save' }).click(),
|
|
);
|
|
return { status: 'passed', summary: 'saved' };
|
|
},
|
|
};
|
|
|
|
export default {
|
|
targets: [{ engine }],
|
|
agents: { default: own },
|
|
} satisfies E2EConfig;
|
|
```
|
|
|
|
Direct Playwright calls bypass the runner's action authorization and trace
|
|
recording. The runner still enforces the step deadline and records the
|
|
verdict, but these calls have no automatic replay or artifacts. Use
|
|
`ctx.budgets` to count custom tool and model calls.
|
|
|
|
## The verdict
|
|
|
|
Return `passed`, `failed`, or `blocked` with a summary. A blocked verdict
|
|
also needs an error code. [How agent steps work](/agent-steps#how-a-step-ends)
|
|
lists the codes and resulting exits.
|
|
|
|
<CardGroup cols={2}>
|
|
<Card title="agent reference" icon="wand-magic-sparkles" href="/reference/agent">
|
|
Every method, option, and budget.
|
|
</Card>
|
|
<Card title="Writing an engine" icon="plug" href="/writing-an-engine">
|
|
Add support for another platform.
|
|
</Card>
|
|
</CardGroup>
|