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

Test a tool on the page

Run a tool on a real page in Chromium: the cursor types into the fields and presses the button, and you check what it did with trusted input and worded matchers.

On the page, a tool is more than its function. The cursor finds the control by its name, types each parameter into the field with that name, and presses the button. Those steps are what the person sees, and they break when the markup changes. Test them in a browser-mode file (*.browser.test.tsx), which runs in Chromium.

A complete example

A form whose tool sets a quantity:

import { useWebTool } from "@webmcp-agents/react";
import { useState } from "react";
import { z } from "zod";

export function QuantityForm() {
  const [qty, setQty] = useState(1);

  // The cursor types `qty` into the field named "qty", then
  // clicks the button named "Set quantity".
  useWebTool({
    id: "set_quantity",
    title: "Set quantity",
    description: "Set how many mugs to buy. Answers the new quantity.",
    inputSchema: z.object({
      qty: z.number().int().min(1).describe("How many, e.g. 3."),
    }),
    execute: ({ qty }) => {
      setQty(qty);
      return `Quantity set to ${qty}.`;
    },
  });

  return (
    <form onSubmit={(event) => event.preventDefault()}>
      <label>
        Quantity <input name="qty" type="number" defaultValue={qty} />
      </label>
      <button type="submit">Set quantity</button>
    </form>
  );
}

Its tests:

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

import { QuantityForm } from "./app/quantity-form";

test("types the quantity where the person sees it", async ({
  tools,
  events,
  mount,
}) => {
  mount(
    <WebAgentsProvider>
      <QuantityForm />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("set_quantity");

  const result = await tools.call("set_quantity", { qty: 3 });

  expect(result).toBeToolResult("Quantity set to 3.");
  expect(events).toHaveFilled("qty", "3");
  expect(events).toHaveEventOrder(["start", "fill", "result"]);
});

test("a stand-in tool answers what you tell it", async ({ tools }) => {
  // One page per test file: a unique name keeps tests apart.
  const name = uniqueName("check_stock");
  const stock = tools.mock(name, async () => "4 left.");

  expect(await tools.call(name, { id: "mug-01" })).toBeToolResult(
    "4 left.",
  );
  expect(stock).toHaveBeenCalledOnce();
});

mount renders the component inside the provider, and removes it after the test. tools.call calls the tool the way an agent does, through document.modelContext, so the cursor runs its steps. events records every step it took: toHaveFilled("qty", "3") checks the field it typed into, and toHaveEventOrder the order of the steps.

Wait for the tool

A component’s tools register after it mounts, not during the call to mount. Wait for them with expect.poll(() => tools.list()) before you call one, as above, or the call fails with “no such tool”.

Why the input has to be trusted

The cursor gets out of the way the moment the person uses the page, and only input the browser marks as trusted counts as the person: a script’s element.click() does not. So a test that checks what happens when the person acts uses the user fixture (user.click, user.type, user.scroll, user.confirm, user.moveOn), which sends real input through Playwright. A test that only checks what the agent does needs no user at all.

Stand-in tools and unique names

tools.mock(name, impl) registers a tool that answers what you tell it and records its calls (a vi.fn). Use it for a tool your code under test depends on, such as a lookup the agent calls first.

Each test file shares one page, so a tool name used by two tests in the same file can collide while one is still being released. uniqueName("check_stock") gives check_stock_1, then check_stock_2: use it for tools a test creates itself.

See also

Was this page helpful?