Skip to main content

Selenium + pytest

Add visual regression testing to your existing Selenium + pytest suite. The witness(driver, name) helper integrates with pytest's fixture pattern โ€” no changes to your existing test structure.


Prerequisitesโ€‹

  • Python 3.8+
  • Chrome browser
  • selenium and pytest installed
pip install selenium pytest

1. Install the CLIโ€‹

npm install -g @testivai/witness-cdp
note

The CLI is a Node.js tool. Node.js 18+ is required even for Python projects.


2. Run the Setup Wizardโ€‹

npx testivai init

Select when prompted:

? Select your language:        โ€บ Python
? Select your test framework: โ€บ Selenium (pytest)
? Where are your test files? โ€บ tests

The wizard generates two files:

FilePurpose
testivai_witness.pywitness(driver, name) helper function
tests/test_visual_example.pyWorking example test with pytest fixture

3. Authenticateโ€‹

npx testivai auth <your-api-key>

Get your API key from the TestivAI Dashboard.


4. Chrome Setupโ€‹

The generated fixture adds --remote-debugging-port=9222 to Chrome options. Add it to your existing fixture or conftest.py:

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@pytest.fixture
def driver():
options = Options()
options.add_argument('--remote-debugging-port=9222')
drv = webdriver.Chrome(options=options)
yield drv
drv.quit()

5. Add Capture Callsโ€‹

Import witness from the generated helper and call it after navigating:

from testivai_witness import witness

def test_homepage(driver):
driver.get('http://localhost:3000')
witness(driver, 'homepage')

def test_login_page(driver):
driver.get('http://localhost:3000/login')
witness(driver, 'login-page')

The generated helper file (testivai_witness.py):

# TestivAI Visual Regression Helper
def witness(driver, name):
"""Capture a visual snapshot."""
return driver.execute_script(f"return window.testivaiWitness('{name}')")

6. Full Working Exampleโ€‹

The wizard generates this example at tests/test_visual_example.py:

# TestivAI Visual Regression Example
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from testivai_witness import witness


@pytest.fixture
def driver():
options = Options()
options.add_argument('--remote-debugging-port=9222')
drv = webdriver.Chrome(options=options)
yield drv
drv.quit()


def test_homepage(driver):
driver.get('http://localhost:3000')
witness(driver, 'homepage')

7. Runโ€‹

testivai run "pytest tests/ -v"

conftest.py patternโ€‹

For sharing the driver fixture across multiple test files, put it in conftest.py:

# conftest.py
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@pytest.fixture(scope='session')
def driver():
options = Options()
options.add_argument('--remote-debugging-port=9222')
drv = webdriver.Chrome(options=options)
yield drv
drv.quit()

CI/CDโ€‹

Add headless Chrome args for CI:

options.add_argument('--remote-debugging-port=9222')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

GitHub Actions example:

- name: Run visual tests
run: testivai run "pytest tests/ -v"
env:
TESTIVAI_API_KEY: ${{ secrets.TESTIVAI_API_KEY }}

How it worksโ€‹

witness(driver, name) calls driver.execute_script() 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.

โ†’ See how the CDP sidecar model works