Skip to main content

Playwright

Playwright has its own dedicated TestivAI SDK โ€” @testivai/witness-playwright. Unlike other frameworks, Playwright does not use the CDP sidecar approach. The SDK integrates directly with Playwright's built-in browser control.


Prerequisitesโ€‹

  • Node.js 18+
  • @playwright/test installed in your project
npm install @playwright/test

1. Install the Playwright SDKโ€‹

npm install @testivai/witness-playwright
No CLI required

Playwright uses a dedicated SDK, not the @testivai/witness-cdp CLI. Do not run testivai run with Playwright.


2. Authenticateโ€‹

Get your API key from the TestivAI Dashboard and add it to your environment:

export TESTIVAI_API_KEY=your-api-key

Or add it to a .env file and load it in your Playwright config.


3. Configure Reporterโ€‹

Add the TestivAI reporter to your playwright.config.ts:

import { defineConfig } from '@playwright/test';

export default defineConfig({
reporter: [
['html'], // Keep Playwright's HTML reporter
['@testivai/witness-playwright/reporter', {
// Optional configuration
debug: false, // Set to true for verbose logging
compression: {
compressUploads: true, // Enable compression for large payloads
compressionThreshold: 5 * 1024 * 1024, // 5MB threshold
}
}]
],
use: {
// Your Playwright configuration
},
});

Reporter Optionsโ€‹

OptionTypeDefaultDescription
debugbooleanfalseEnable verbose logging. Can also be set via TESTIVAI_DEBUG=true
apiUrlstringhttps://core-api.testiv.aiCustom API endpoint URL
apiKeystringprocess.env.TESTIVAI_API_KEYAPI key (overrides environment variable)
compressionobject{}Compression settings for uploads

4. Add Capture Callsโ€‹

Import testivai from the SDK and call testivai.witness(page, testInfo, 'name') in your tests:

import { test } from '@playwright/test';
import { testivai } from '@testivai/witness-playwright';

test('homepage looks correct', async ({ page }, testInfo) => {
await page.goto('http://localhost:3000');
await testivai.witness(page, testInfo, 'homepage');
});

test('login page looks correct', async ({ page }, testInfo) => {
await page.goto('http://localhost:3000/login');
await testivai.witness(page, testInfo, 'login-page');
});

5. Full Working Exampleโ€‹

import { test, expect } from '@playwright/test';
import { testivai } from '@testivai/witness-playwright';

test.describe('Visual Regression', () => {
test('homepage', async ({ page }, testInfo) => {
await page.goto('/');
await testivai.witness(page, testInfo, 'homepage');
});

test('navigation state', async ({ page }, testInfo) => {
await page.goto('/');
await page.click('nav a[href="/about"]');
await testivai.witness(page, testInfo, 'about-page');
});

test('mobile viewport', async ({ page }, testInfo) => {
await page.setViewportSize({ width: 375, height: 812 });
await page.goto('/');
await testivai.witness(page, testInfo, 'homepage-mobile');
});
});

6. Runโ€‹

Run your tests normally with Playwright โ€” no testivai run wrapper needed:

npx playwright test

CI/CDโ€‹

GitHub Actions example:

- name: Install dependencies
run: npm ci

- name: Install Playwright browsers
run: npx playwright install --with-deps chromium

- name: Run visual tests
run: npx playwright test
env:
TESTIVAI_API_KEY: ${{ secrets.TESTIVAI_API_KEY }}

What gets capturedโ€‹

Each testivai.witness() call captures the same 5-layer snapshot as the CDP SDK:

LayerData
ScreenshotFull-viewport PNG
DOMFull HTML snapshot
CSSComputed styles per element
LayoutBounding boxes and positioning
PerformanceWeb Vitals and CDP metrics

How it worksโ€‹

The Playwright SDK uses Playwright's native page.screenshot(), page.evaluate(), and CDP session APIs directly โ€” no external Chrome debugging port required. This makes it the most seamless integration for Playwright users.

โ†’ See all captured layers


Version Historyโ€‹

  • v0.3.1 (Latest) - Fixed dist build (v0.3.0 shipped stale compiled code)
  • v0.3.0 - Fixed reporter crash on .css.json files, added debug logging, unified format with CDP SDK
  • v0.2.4 - Added performance metrics integration
  • v0.2.0 - Added DOM analysis and CSS fingerprinting
  • v0.1.13 - Initial public release