--- permalink: /agents title: Agentic Testing --- # Agentic Testing ## What Makes CodeceptJS Agent-friendly CodeceptJS 4 is designed for agent testing. It ships with its own MCP and official skills that teach agents best practices in test automation—how to write tests, choose locators, test them live in the browser, and refactor to page objects. Agents get full control over test and browser execution: - **Full HTML access and ARIA snapshots.** Agents see buttons with icons, empty labels, and other elements Playwright MCP's accessibility tree omits. - **Query HTML directly with CSS, ARIA, Semantic Locators or XPath** Agents can freely run XPath or CSS to efficiently search over HTML. - **Browser logs and page state as files.** Agents read via native filesystem tools instead of extra requests, eliminating redundant context dumps. - **Same locators for tests and agents.** Tests and agent scripts share identical selectors. No elements by reference indexes, no clicks by coordinates. Agents inherit battle-tested patterns without guessing. - **Testing-first framework, not browser control.** CodeceptJS is built for testing, so agents stay focused on test scenarios instead of raw browser commands. CodeceptJS is token-efficient: it stores HTML, ARIA, logs, and HTTP request data as files instead of streaming them through MCP. Agents read these files with their native shell tools—no extra API calls, no redundant context. ## Essential Setup Two things make agent testing work: the **skills** that teach the agent CodeceptJS, and the **MCP server** that lets it drive the browser. Set both up once, from your project directory: ```bash npx codeceptjs init:agent claude ``` Use `codex`, `cursor`, or `opencode` instead of `claude` for other agents, or omit the name to pick from a list. `npx codeceptjs init` offers the same step at the end. ### Manual setup Install the skills: ```bash npx skills add codeceptjs/skills ``` Connect the MCP server (`npx codeceptjs-mcp`). **Claude Code:** ```bash claude mcp add codeceptjs -- npx codeceptjs-mcp ``` **Codex:** ```bash codex mcp add codeceptjs -- npx codeceptjs-mcp ``` **Cursor** — add to `.cursor/mcp.json`: ```json { "mcpServers": { "codeceptjs": { "command": "npx", "args": ["codeceptjs-mcp"] } } } ``` See [/mcp](/mcp) for full client setup. Now the agent is ready to run the loop. ## The loop Whether the agent is writing a new test or fixing an old one, it follows the same cycle. 1. **Open the page.** Run a stub test (new work) or set a breakpoint at the failing step (fix). The browser lands at the right starting point and yields control to the agent. 2. **Read the page.** MCP saves HTML, ARIA, and screenshot of the page to files (and the agent can call the `snapshot` tool to refresh them). The agent reads those files before deciding what to try next, controlling its token usage. 3. **Run a CodeceptJS command.** The agent tries `I.*` commands like `I.click('Add to cart')`, `I.fillField('Email', secret(process.env.EMAIL))`, `I.see('Confirmed')`. On success, that line goes into the test — same syntax. 4. **Check the result.** The response after each command shows the new page state. If the URL changed and the modal opened, the line goes into the verified sequence. If not, the agent reads the page again and tries a different locator or a wait. 5. **Move forward.** The agent looks at the new state and chooses the next command. Steps 2–4 repeat until the scenario is whole. 6. **Commit to the file.** The agent edits the test — replaces `pause()` (new tests) or the broken line (fixes) with the verified sequence — then reruns end-to-end and reads the trace to confirm. ## How It Works CodeceptJS ships an **MCP server and a skillset** that lets an AI agent (Claude Code, Cursor, Codex, others) write and fix tests by driving the real browser. The agent runs the same `I.*` commands the test does, reads how the page responds, and only commits the lines that succeeded. ## Why MCP The traditional agent testing loop is test/fix/retry, where the agent executes a test, watches it fail, reads artifacts, performs code fixes, and reruns the test. The agent applies fixes by intelligent guess — looking at the ARIA tree, HTML, and screenshot — then assumes the fix is enough and reruns the test hoping it will pass. If the guess is wrong and the test runs for over a minute, it may take dozens of minutes of iteration and a lot of wasted tokens. To improve that flow, the agent can spawn a browser and open the page the way the test does. This lets it interact with the page more freely and perform multi-step actions. But putting that experience back into test code is not efficient either: actions executed in the browser may not be relevant in test context, so the agent ends up in another guess-and-try loop. The problem is that **the test runs in a different context than the agent**. The agent can launch a test but can't control it while it's running. It can't access the browser. It can't set a breakpoint. This is where CodeceptJS MCP steps in. Connected to the agent, it can: - run a test and pause it on failure - interact with the browser in a test context - test locators and perform actions live while the test is running - write successful actions to the test file This lets the agent get a test working in one iteration. The agent can live-write the test before your eyes by exploring the page and performing actions that eventually land in the CodeceptJS test file. **Live debugging of tests** is what CodeceptJS MCP provides. The agent receives feedback faster — not from a whole test execution but from specific actions on a specific page — so it can adjust and react faster, trying different approaches. The MCP server is the agent-facing equivalent of the `pause()` REPL — same access, driven by tool calls instead of keystrokes. Full tool reference at [/mcp](/mcp). ## How the agent reads the page MCP commands are token efficient — they don't stream large HTML pages back to the model. MCP writes artifacts to disk under `output/trace_*/` and returns file paths. The agent reads each artifact with its own bash tools — `cat`, `grep`, `jq`. A `run_code` response, for example, looks like this: ```json { "status": "success", "artifacts": { "url": "http://localhost:8000/", "html": "file:///output/trace_run_code_.../mcp_page.html", "aria": "file:///output/trace_run_code_.../mcp_aria.txt", "screenshot": "file:///output/trace_run_code_.../mcp_screenshot.png", "console": "file:///output/trace_run_code_.../mcp_console.json", "storage": "file:///output/trace_run_code_.../mcp_storage.json" } } ``` Only `url` is inline. The rest are paths the agent opens with the right tool: | Artifact | How the agent reads it | |----------|------------------------| | `*_screenshot.png` | As an image — most agents are multimodal | | `*_aria.txt` | Whole — small and structured | | `*_page.html` | With `grep` — too large for context, searchable for specific elements/attributes | | `*_console.json` | With `jq` — filter for errors, 4xx/5xx, deprecation warnings | | `*_storage.json` | Whole — cookies and `localStorage` snapshot | | `trace.md` | Whole — markdown index linking every step to its artifacts | Saved HTML is formatted, with non-semantic elements stripped out: `