examples/integration-browser/headless/README.md
A browser automation example demonstrating how to test web applications using Playwright.
You can run this example with:
npx promptfoo@latest init --example integration-browser/headless
cd integration-browser/headless
This example demonstrates how to:
Ensure you have Python 3 and Node.js installed on your system.
npm install playwright @playwright/browser-chromium playwright-extra puppeteer-extra-plugin-stealth
pip install -r requirements.txt
That's it! No additional setup scripts or configuration needed.
python gradio_demo.py
This starts a local server at http://localhost:7860
npx promptfoo@latest eval -c promptfooconfig.yaml
npx promptfoo@latest view
The main configuration (promptfooconfig.yaml) tests a chatbot interface with a 100% pass rate:
┌─────────────────────────────────────────────────┬─────────────────────────────────────────────────┐
│ topic │ [browser-provider] Tell me about {{topic}} │
├─────────────────────────────────────────────────┼─────────────────────────────────────────────────┤
│ testing browser automation │ [PASS] Test successful! The browser automation │
│ │ is working correctly. │
├─────────────────────────────────────────────────┼─────────────────────────────────────────────────┤
│ how the system works │ [PASS] I received your message: 'Tell me about │
│ │ how the system works'. This is a simple demo │
│ │ response! │
├─────────────────────────────────────────────────┼─────────────────────────────────────────────────┤
│ a simple greeting │ [PASS] I received your message: 'Tell me about │
│ │ a simple greeting'. This is a simple demo │
│ │ response! │
└─────────────────────────────────────────────────┴─────────────────────────────────────────────────┘
The calculator-example.yaml demonstrates form interactions with a 100% pass rate:
┌───────────────────┬───────────────────┬───────────────────┬───────────────────┬───────────────────┐
│ num1 │ num2 │ operation │ operationSelector │ [browser-provider]│
├───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┤
│ 10 │ 5 │ Add │ #operation │ [PASS] Calculator │
│ │ │ │ label:nth-child(1)│ interaction │
│ │ │ │ │ successful │
├───────────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────┤
│ 20 │ 4 │ Multiply │ #operation │ [PASS] Calculator │
│ │ │ │ label:nth-child(3)│ interaction │
│ │ │ │ │ successful │
└───────────────────┴───────────────────┴───────────────────┴───────────────────┴───────────────────┘
This example demonstrates:
The example configurations demonstrate key concepts:
transformResponse for data extractionThe Gradio application provides consistent selectors:
textarea[data-testid="textbox"] - Message input fieldbutton#submit-button - Submit buttondiv[data-testid="bot"]:last-of-type .prose - Latest bot responsebutton[value="calculator"] - Calculator tab buttoninput[type="radio"] - Operation selectionurl in the navigation stepFor single-page applications or AJAX content:
- action: waitForNewChildren
args:
parentSelector: '#results-container'
timeout: 10000
Chain multiple actions for sophisticated workflows:
steps:
- action: navigate
args:
url: 'http://localhost:3000'
- action: click
args:
selector: '#menu-button'
- action: wait
args:
ms: 1000
- action: click
args:
selector: '#dropdown-option-2'
| Issue | Solution |
|---|---|
| Elements not found | Use browser DevTools to verify selectors |
| Timing issues | Increase wait times or use waitForNewChildren |
| Want to see the browser | Set headless: false in the configuration |
| Need detailed logs | Run with npx promptfoo@latest eval --verbose |