expect-cli: AI Agent Browser Testing CLI
expect-cli is a command-line interface tool designed to enable AI agents to test web applications in a real browser environment. It leverages browser automation tools like Playwright to record user sessions, generate test plans, and perform diffing against expected outcomes, often integrating with version control systems like Git. The current stable version is 0.1.3. Given its low version number and frequent updates (as seen in the release logs), it appears to have a rapid, iterative release cadence. It differentiates itself by explicitly targeting AI agents for automated test generation and execution, moving beyond traditional script-based automation towards more autonomous, intelligent testing within a live browser context.
Common errors
-
Error: Playwright browsers are not installed. Please run `npx playwright install`
cause The underlying Playwright dependency needs its browser binaries downloaded to function correctly.fixRun `npx playwright install` in your project directory to download the necessary browser binaries for Playwright. For CI environments, consider `npx playwright install --with-deps`. -
Command not found: expect-cli
cause `expect-cli` is not globally installed or `npx` cannot locate it in `node_modules`.fixIf installed locally, run with `npx expect-cli <command>`. If you intend to use it globally, install with `npm install -g expect-cli` (though `npx` is generally recommended for local CLI tools). -
Unsupported Node.js version. Requires Node.js >=18.
cause The system's Node.js version is older than the minimum required by `expect-cli`.fixUpgrade your Node.js environment to version 18 or newer. Use a tool like `nvm` (Node Version Manager) to easily manage and switch Node.js versions.
Warnings
- breaking Due to its early development stage (version 0.1.3), expect-cli is subject to frequent breaking changes. APIs, CLI commands, and configuration options may change without major version bumps in accordance with semantic versioning pre-1.0.0 guidelines.
- gotcha expect-cli relies on Playwright for browser automation, which requires specific browser binaries to be installed. These are typically installed automatically when Playwright is installed, but can sometimes lead to issues in constrained environments (e.g., CI/CD without necessary OS dependencies or sandboxing disabled).
- gotcha The name 'expect-cli' can be confused with other `expect` libraries (e.g., Jest's `expect` assertion library) or the older Tcl-based `expect` command-line utility for automating interactive processes. This `expect-cli` is specifically for AI-driven browser testing.
- gotcha expect-cli integrates with various AI coding agents (e.g., Claude Code, Codex, GitHub Copilot). Proper functioning requires one of these agents to be installed and configured on the system's PATH, or specified via the `--agent` flag. Misconfiguration or missing agents will prevent testing.
Install
-
npm install expect-cli -
yarn add expect-cli -
pnpm add expect-cli
Imports
- CLI Execution
import { spawn } from 'child_process'; spawn('npx', ['expect-cli', 'test'], { stdio: 'inherit' }); - Configuration Types
import { ExpectCLIOptions } from 'expect-cli'; // Runtime import will likely be undefinedimport type { ExpectCLIOptions } from 'expect-cli'; - Generic Module Import
import * as ExpectCLI from 'expect-cli'; // Will likely result in an empty object or error if no main export
// Primary interaction is via CLI, no direct module import for execution.
Quickstart
import { spawn } from 'child_process';
const agentName = process.env.EXPECT_AGENT_NAME ?? 'claude'; // Example agent
const projectPath = './my-project'; // Path to your project
console.log(`Initializing expect-cli in ${projectPath} with agent: ${agentName}...`);
const initProcess = spawn('npx', [
'expect-cli@latest',
'init',
'--agent', agentName,
projectPath // Or other initialization parameters
], {
stdio: 'inherit', // Pipe child process stdio to parent process
cwd: process.cwd() // Run from current working directory
});
initProcess.on('error', (err) => {
console.error('Failed to start expect-cli init process:', err);
});
initProcess.on('close', (code) => {
if (code === 0) {
console.log('expect-cli initialization completed successfully.');
console.log(`
Now, try running: npx expect-cli@latest test ${projectPath}`);
} else {
console.error(`expect-cli init process exited with code ${code}`);
}
});