Skip to content
WebMCP Agents
Esc
↑↓navigate↵open⌘Jpreview
On this page

user

Trusted input in a browser-mode test: confirm, moveOn, scroll, click, type.

The user fixture: the person at the page, as trusted input (Playwright through vitest/browser). The cursor yields to the person and a confirmation goes on only for them, and both look at isTrusted: a dispatchEvent never counts.

Import

import { test } from "@webmcp-agents/testing";

test("…", async ({ user }) => {});

Example

import { expect, test } from "@webmcp-agents/testing";
import { WebAgentsProvider } from "@webmcp-agents/react";

import { Checkout } from "./app/checkout";
import { orders } from "./app/orders";

test("waits for the person before placing the order", async ({
  tools,
  events,
  user,
  mount,
}) => {
  orders.clear();
  mount(
    <WebAgentsProvider>
      <Checkout />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("place_order");

  const call = tools.call("place_order");
  await expect.poll(() => events).toHaveParked("place_order");
  expect(orders.get()).toHaveLength(0);

  // A trusted click on the parked control.
  await user.confirm("Place order");
  expect(await call).toBeToolResult();
  expect(orders.get()).toHaveLength(1);
});

test("gives up when the person moves on", async ({
  tools,
  events,
  user,
  mount,
}) => {
  mount(
    <WebAgentsProvider>
      <Checkout />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("place_order");

  const call = tools.call("place_order");
  await expect.poll(() => events).toHaveParked();
  // A trusted click away from the control.
  await user.moveOn();
  expect(await call).toBeToolError("cancelled");
  // The cursor got out of the way of the person's pointer.
  expect(events).toHaveYielded("pointer");
});

Options

The FakeUser’s methods. fakeUser() builds one outside the fixture.

Name Type Default Description
confirm (name?: string) => Promise<void> — Click the control a confirmation waits on ([data-agent-active]), or the button or link named name.
moveOn () => Promise<void> — A click away from every control: cancels a waiting confirmation.
scroll (deltaY?: number) => Promise<void> 200 Scroll with the mouse wheel.
click (target) => Promise<void> — Click an element or a locator.
type (target, text) => Promise<void> — Type into it, key by key ({Enter} presses Enter).

Returns

A FakeUser.

Notes

Browser mode only: in Node the fixture fails with a sentence saying so.

UserTarget

What click and type take: an Element, or anything with an element() method (a vitest/browser locator).

See also

Was this page helpful?