Selenium (JavaScript)
Add visual regression testing to your existing Selenium + JavaScript test suite. Works with Jest, Mocha, or any other JS test runner.
Prerequisitesโ
- Node.js 18+
- Chrome browser
selenium-webdriverinstalled in your project
npm install selenium-webdriver
1. Install the CLIโ
npm install -g @testivai/witness-cdp
2. Run the Setup Wizardโ
npx testivai init
Select when prompted:
? Select your language: โบ JavaScript / TypeScript
? Select your test framework: โบ Selenium
? Where are your test files? โบ tests
The wizard generates two files:
| File | Purpose |
|---|---|
testivai-witness.js | witness(driver, name) helper function |
tests/visual-example.test.js | Working example test |
3. Authenticateโ
npx testivai auth <your-api-key>
Get your API key from the TestivAI Dashboard.
4. Chrome Setupโ
The generated example adds --remote-debugging-port=9222 to your Chrome options. Add it to your existing driver setup:
const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const options = new chrome.Options();
options.addArguments('--remote-debugging-port=9222');
const driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
5. Add Capture Callsโ
Import witness from the generated helper and call it after navigating:
const { witness } = require('../testivai-witness');
it('homepage looks correct', async () => {
await driver.get('http://localhost:3000');
await witness(driver, 'homepage');
});
it('login page looks correct', async () => {
await driver.get('http://localhost:3000/login');
await witness(driver, 'login-page');
});
The generated helper file (testivai-witness.js):
// TestivAI Visual Regression Helper
async function witness(driver, name) {
return driver.executeScript(`return window.testivaiWitness('${name}')`);
}
module.exports = { witness };
6. Full Working Exampleโ
The wizard generates this example at tests/visual-example.test.js:
// TestivAI Visual Regression Example
const { Builder } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const { witness } = require('../testivai-witness');
describe('Visual Regression', () => {
let driver;
beforeAll(async () => {
const options = new chrome.Options();
options.addArguments('--remote-debugging-port=9222');
driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
});
afterAll(async () => await driver.quit());
it('captures homepage', async () => {
await driver.get('http://localhost:3000');
await witness(driver, 'homepage');
});
});
7. Runโ
testivai run "npx jest tests/"
Or with Mocha:
testivai run "npx mocha tests/**/*.test.js"
CI/CDโ
Add --headless and --no-sandbox for CI environments:
options.addArguments('--remote-debugging-port=9222');
options.addArguments('--headless');
options.addArguments('--no-sandbox');
options.addArguments('--disable-dev-shm-usage');
GitHub Actions example:
- name: Run visual tests
run: testivai run "npx jest tests/"
env:
TESTIVAI_API_KEY: ${{ secrets.TESTIVAI_API_KEY }}
How it worksโ
witness(driver, name) calls driver.executeScript() to invoke window.testivaiWitness(name) โ the global function injected by the CDP SDK. The SDK captures a full 5-layer snapshot and uploads it for analysis.