Browser Integration SDK
The TestivAI Witness SDK (@testivai/witness) is a framework-agnostic visual testing solution that works with any browser automation tool that supports Chrome/Chromium remote debugging.
Overview
The Witness SDK operates as a sidecar process that:
- Connects to your browser via remote debugging
- Captures comprehensive snapshots when invoked
- Uploads data to TestivAI for analysis
- Works with any framework (Selenium, Playwright, Puppeteer, etc.)
Quick Start
1. Install the Witness SDK
npm install -g @testivai/witness
# or
yarn global add @testivai/witness
2. Start the Witness Process
# Start with default port (9222)
testivai start
# Or specify custom port
testivai start --port 9333
3. Configure Your Browser
Add the remote debugging flag when launching your browser:
// Chrome/Chromium
--remote-debugging-port=9222
// Example with Selenium (Java)
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-debugging-port=9222");
4. Add Witness Calls
In your tests, call the global testivaiWitness function:
// JavaScript
await page.evaluate(() => window.testivaiWitness('homepage'));
// Python with Selenium
driver.execute_script("window.testivaiWitness('homepage')");
// Java with Selenium
((JavascriptExecutor)driver).executeScript("window.testivaiWitness('homepage')");
// Ruby with Capybara
page.driver.execute_script("window.testivaiWitness('homepage')")
Framework Integration
Selenium (JavaScript)
const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
// Configure Chrome with remote debugging
const options = new chrome.Options();
options.addArguments('--remote-debugging-port=9222');
options.addArguments('--headless');
options.addArguments('--no-sandbox');
options.addArguments('--disable-dev-shm-usage');
const driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
// Take a snapshot
await driver.get('https://example.com');
await driver.executeScript("window.testivaiWitness('homepage')");
Selenium (Python)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Configure Chrome with remote debugging
options = Options()
options.add_argument('--remote-debugging-port=9222')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
# Take a snapshot
driver.get('https://example.com')
driver.execute_script("window.testivaiWitness('homepage')")
Selenium (Java)
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-debugging-port=9222");
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
WebDriver driver = new ChromeDriver(options);
driver.get("https://example.com");
// Take a snapshot
((JavascriptExecutor)driver).executeScript("window.testivaiWitness('homepage')");
Playwright
const { chromium } = require('playwright');
// Connect to existing Chrome instance
const browser = await chromium.connect('ws://localhost:9222/devtools/browser');
const context = await browser.newContext();
const page = await context.newPage();
// Take a snapshot
await page.goto('https://example.com');
await page.evaluate(() => window.testivaiWitness('homepage'));
Puppeteer
const puppeteer = require('puppeteer');
// Connect to existing Chrome instance
const browser = await puppeteer.connect({
browserURL: 'http://localhost:9222'
});
const page = await browser.newPage();
await page.goto('https://example.com');
// Take a snapshot
await page.evaluate(() => window.testivaiWitness('homepage'));
Cypress
// In cypress/support/e2e.js
before(() => {
// Start Witness process before tests
cy.exec('testivai start --port 9222', { failOnNonZeroExit: false });
});
// In your test file
it('should capture homepage', () => {
cy.visit('/');
cy.window().then((win) => {
win.testivaiWitness('homepage');
});
});
Configuration Options
CLI Options
testivai start --help
Options:
--port <number> Port for browser debugging (default: 9222)
--host <string> Host to bind to (default: localhost)
--timeout <number> Connection timeout in ms (default: 5000)
--verbose Enable verbose logging
Environment Variables
# API endpoint
TESTIVAI_API_URL=https://api.testiv.ai
# API key (alternative to testivai auth)
TESTIVAI_API_KEY=your-api-key
# Debug mode
DEBUG=testivai:*
What Gets Captured
Each testivaiWitness() call captures:
| Layer | Description |
|---|---|
| Screenshot | Full-viewport PNG image |
| DOM Tree | Complete HTML structure |
| CSS Styles | Computed styles for all elements |
| Layout Info | Element positions and dimensions |
| Performance | Web Vitals and timing metrics |
| Metadata | Browser info, timestamp, viewport |
Advanced Usage
Multiple Browser Instances
# Start multiple witness instances
testivai start --port 9222 &
testivai start --port 9223 &
testivai start --port 9224 &
Custom Namespacing
// Use custom names for better organization
window.testivaiWitness('feature-login-form');
window.testivaiWitness('feature-dashboard-chart');
window.testivaiWitness('feature-user-profile');
Conditional Capturing
// Only capture on specific conditions
if (process.env.NODE_ENV === 'test') {
window.testivaiWitness('critical-page');
}
// Or based on feature flags
if (featureFlags.visualTesting) {
window.testivaiWitness('new-feature');
}
CI/CD Integration
GitHub Actions
- name: Start Witness Process
run: |
testivai start &
sleep 2 # Wait for Witness to start
- name: Run Tests
run: npm test
env:
TESTIVAI_API_KEY: ${{ secrets.TESTIVAI_API_KEY }}
Docker
FROM selenium/standalone-chrome:latest
# Install TestivAI Witness SDK
RUN npm install -g @testivai/witness
# Expose debugging port
EXPOSE 9222
# Start Witness with Chrome
CMD ["sh", "-c", "testivai start & /opt/bin/entry_point.sh"]
Troubleshooting
Connection Refused
- Ensure Chrome is running with
--remote-debugging-port=9222 - Check if the port is available:
lsof -i :9222 - Try a different port if needed
Witness Function Not Found
- Verify the Witness SDK is running
- Check browser console for errors
- Ensure you're on an HTTP/HTTPS page (not file://)
Slow Performance
- Use
--headlessmode in CI - Disable unnecessary Chrome flags
- Consider using parallel execution
Best Practices
- Use descriptive names for witness calls
- Group related snapshots with consistent naming
- Clean up old baselines regularly
- Use environment variables for configuration
- Run Witness in background for long-running test suites
Migration from Framework-Specific SDKs
If you're using a framework-specific SDK (e.g., @testivai/playwright), migrating to the Witness SDK is simple:
- Install
@testivai/witness - Replace framework-specific imports with global function calls
- Add Chrome debugging flags
- Start the Witness process before tests
The benefit is a unified approach that works across all frameworks!
Need Help?
- Check our Troubleshooting Guide
- Visit our GitHub Issues
- Contact support at hello@testiv.ai