This example demonstrates using autobrowse-agent with CucumberJS and Playwright for BDD testing.
- Install dependencies:
npm install- Configure environment variables:
cp .env.example .env- Edit
.envand add your API key:
# Choose your agent type
AGENT_TYPE=langchain
# Add your API key
ANTHROPIC_API_KEY=your_api_key_here
# Optional: Increase recursion limit for complex scenarios
RECURSION_LIMIT=100npm test # Run tests in headless mode
npm run test:headed # Run with visible browser
npm run test:report # Generate HTML reportThis example shows how to:
- Write BDD scenarios in Gherkin syntax
- Use AI agent in Cucumber step definitions
- Share browser context with Playwright and the AI agent
- Generate HTML reports from test execution
cucumberjs/
├── features/
│ ├── playwright-home.feature # Gherkin feature files
│ └── step_definitions/
│ └── steps.js # Step definitions with agent
├── cucumber.js # Cucumber configuration
└── package.json
Feature: Playwright Home Page
Scenario: Check title
Given I navigate to "https://playwright.dev/"
When I click link "Get started"
Then I should see "Installation" in the titleThe key innovation is using a single generic step that handles ALL steps with AI:
import { defineStep, Before, After } from '@cucumber/cucumber';
import { chromium } from 'playwright';
import { runAgent, claudeCode } from 'openqa';
Before(async function () {
browser = await chromium.launch({ headless: true });
context = await browser.newContext();
page = await context.newPage();
});
After(async function () {
await page.close();
await context.close();
await browser.close();
});
// Generic AI step - handles ALL Given/When/Then steps
defineStep(/^(.*)$/, async function (action) {
await runAgent(claudeCode('claude-haiku-4-5'), action, context, { verbose: false });
});That's it! One generic step definition handles all scenarios. No need to write individual step definitions for each Given/When/Then.
- Before hook sets up browser, context, and page
- Generic step (
defineStep(/^(.*)$/, ...)) matches ALL Given/When/Then - ALL steps are handled by AI agent using natural language
- After hook cleans up browser resources
- Browser context is shared between Playwright and agent
- No code required - just write scenarios and run tests!
- Zero code: Write tests in pure natural language
- One generic step: Single
defineStephandles ALL Given/When/Then - Standard CucumberJS: Uses vanilla
@cucumber/cucumber - Before/After hooks: Manage browser lifecycle
- HTML reports: Generate detailed test reports
- Shared context: Agent and Playwright see the same browser state
After running tests, view the HTML report:
open reports/cucumber-report.htmlThe report includes:
- Scenario pass/fail status
- Step-by-step execution details
- Execution time
The cucumber.js file configures:
- Feature file paths
- Step definition paths
- Report formats and output
- Default settings
Configure the AI agent via environment variables in .env:
| Variable | Description | Default |
|---|---|---|
AGENT_TYPE |
Agent implementation: claude or langchain |
claude |
DEFAULT_PROVIDER |
AI provider for langchain: anthropic, openai, or google |
anthropic |
RECURSION_LIMIT |
Maximum steps for complex scenarios | 100 |
ANTHROPIC_API_KEY |
API key for Claude models | - |
OPENAI_API_KEY |
API key for GPT models | - |
GOOGLE_API_KEY |
API key for Gemini models | - |
Recursion Limit: If you encounter "GraphRecursionError: Recursion limit of 25 reached", increase the RECURSION_LIMIT environment variable:
# For complex multi-step scenarios
RECURSION_LIMIT=150The recursion limit controls how many agent steps can be executed. Complex browser automation tasks may require higher limits.
Unlike the playwright-bdd example, this uses:
- Vanilla CucumberJS (not playwright-bdd wrapper)
- Manual browser setup in Before/After hooks
- Direct Playwright API usage
- Standard Cucumber configuration
Both approaches are valid - choose based on your team's preference and existing setup.