Project to run automated tests with Stagehand against applications on localhost.
- β AI-powered automated tests using Stagehand
- β Page navigation and verification
- β Form and element interaction
- β Automatic screenshots
- β Colorful logging system
- Node.js 18 or higher
- An application running on localhost (port 3000 by default)
- Anthropic API key (for Stagehand AI features)
- Clone or copy this project.
- Install dependencies:
npm install- Copy the example environment file and configure it:
cp .env- Edit the
.envfile and add your Anthropic API key:
ANTHROPIC_API_KEY=your_api_key_here
LOCALHOST_URL=http://localhost:3000
HEADLESS=false
VERBOSE=1npm testnpm run devnpm run build
node dist/tests/test-basic-navigation.jsOr in development:
npx ts-node src/tests/test-basic-navigation.tsnpm run buildnpm run cleanstagehand-localhost-tests/
βββ src/
β βββ tests/ # Stagehand tests
β β βββ test-basic-navigation.ts # Basic navigation test
β β βββ test-form-interaction.ts # Form interaction test
β β βββ test-data-extraction.ts # AI data extraction test
β β βββ run-all.ts # Runner for all tests
β βββ utils/ # Utilities
β β βββ stagehand-config.ts # Stagehand configuration
β β βββ logger.ts # Logging system
β βββ types/ # TypeScript types (optional)
βββ screenshots/ # Screenshots generated by tests
βββ package.json
βββ tsconfig.json
βββ .env.example
βββ README.md
- Navigates to the localhost URL
- Verifies the page loads correctly
- Gets the page title
- Takes a screenshot
| Variable | Description | Default |
|---|---|---|
ANTHROPIC_API_KEY |
Anthropic API key (required) | - |
LOCALHOST_URL |
Base URL of your application | http://localhost:3000 |
LOCALHOST_PORT |
Port of your application | 3000 |
HEADLESS |
Run browser in headless mode | false |
VERBOSE |
Verbosity level (0β2) | 1 |
The tests are designed to be easily customizable. You can:
- Change the URL: Modify
LOCALHOST_URLin.env. - Adjust routes: Use
getLocalhostUrl("/path")in your tests. - Modify actions: Change the instructions in
act()according to your application. - Customize extraction: Adjust the
extract()schemas to your needs.
import { Stagehand } from "@browserbasehq/stagehand";
import { createStagehand, closeStagehand, getLocalhostUrl } from "../utils/stagehand-config";
import { TestLogger } from "../utils/logger";
export async function testMyNewTest(): Promise<boolean> {
const logger = new TestLogger("My New Test");
logger.startTest();
let stagehand: Stagehand | null = null;
try {
stagehand = await createStagehand();
logger.success("Stagehand initialized");
// Your test code here
await stagehand.page.goto(getLocalhostUrl("/my-route"));
// ... more actions ...
logger.endTest(true);
return true;
} catch (error) {
logger.error("Error:", error);
logger.endTest(false);
return false;
} finally {
if (stagehand) {
await closeStagehand(stagehand);
}
}
}Screenshots are automatically saved in the screenshots/ folder with descriptive names:
basic-navigation.pngform-interaction.pngdata-extraction.png- And any others you capture in your custom tests
Run npm install to install dependencies.
Make sure you created the .env file and added your API key.
Change LOCALHOST_URL in your .env file to the correct port.
The example tests use generic instructions. Adjust the act() and extract() instructions according to the real elements in your application.
- The tests are designed to run with the browser visible (
headless: false) so you can see whatβs happening. - Each test takes screenshots to make debugging easier.
- The logging system uses colors to improve readability.
- Tests run sequentially with small pauses between them.
Feel free to add more tests or improve existing ones according to your applicationβs needs.
MIT