@digital-commons-official/codeceptjs-visual-helper

中文文档
3.0.2发布于 2026/11/03EUPL-1.2TypeScriptCommonJS近 30 天下载 1.5万

Visual regression testing helper for CodeceptJS with Playwright, Puppeteer, WebDriver, Appium & TestCafe support

原始使用文档
文档目录18 节
  1. Overview 📖
  2. Key Features
  3. Installation 📦
  4. Configuration ⚙️
  5. Configuration Options
  6. Usage 🚀
  7. Asserting Visual Matches
  8. Comparing Without Assertions
  9. Capturing Screenshots
  10. Comparison Options
  11. Baseline Variations
  12. Comparison Result Object
  13. Development 🛠️
  14. Project Structure
  15. Running Tests
  16. Integration Tests
  17. Release & Publishing
  18. Licence 📜

👁️ codeceptjs-visual-helper

pipeline status

A visual regression testing helper for CodeceptJS, providing pixel-by-pixel screenshot comparison with comprehensive support for Playwright, Puppeteer, WebDriver, Appium, WebDriverIO and TestCafe.

Overview 📖

This helper enables automated visual regression testing by comparing screenshots captured during test execution against established baseline images. When differences are detected, the helper generates diff images that highlight the discrepancies, facilitating rapid identification of unintended visual changes.

Key Features

  • Multi-driver Support — Seamlessly integrates with Playwright, Puppeteer, WebDriver, Appium, WebDriverIO and TestCafe
  • Configurable Tolerance — Define acceptable thresholds for pixel differences
  • Ignore Regions — Exclude dynamic content areas from comparison
  • Baseline Variations — Support multiple baseline versions for responsive or themed layouts
  • Detailed Reporting — Comprehensive diff images and statistics for each comparison

Installation 📦

Requires Node.js 22.19.0 or later. The package is tested with CodeceptJS 4 on Node 22.23.4, 24.22.1 and 26.12.1. Node 18 and 20 are no longer supported.

npm install @digital-commons-official/codeceptjs-visual-helper --save-dev

Configuration ⚙️

Add the helper to your codecept.conf.js configuration file:

helpers: {
  Playwright: {
    // Your browser helper configuration
  },
  VisualHelper: {
    require: '@digital-commons-official/codeceptjs-visual-helper',
    baselineDir: './tests/screenshots/baseline/',
    diffDir: './tests/screenshots/diff/',
    actualDir: './tests/output/',
    tolerance: 1.5,
    threshold: 0.1
  }
}

Configuration Options

OptionTypeDefaultDescription
baselineDirstring./tests/screenshots/baseline/Directory containing baseline images
diffDirstringfalse./tests/screenshots/diff/Directory for generated diff images; set to false to disable
actualDirstringglobal.output_dirDirectory for captured screenshots
tolerancenumber0Permitted percentage of differing pixels (0–100)
thresholdnumber0.1Pixelmatch colour comparison threshold (0–1)
diffPrefixstringDiff_Filename prefix for diff images
imageFormatstring'webp''webp' (lossless) or 'png' for older baselines
saveIntermediateImagesbooleanfalseRetain intermediate processing images for debugging
captureActualboolean'missing''missing'Automatic capture of actual screenshots
captureBaselineboolean'missing''missing'Automatic capture of baseline images

Usage 🚀

Asserting Visual Matches

The assertVisualMatch method compares the current viewport against a baseline image, failing the test if differences exceed the configured tolerance:

// Basic full-page comparison
await I.assertVisualMatch('homepage');

// With custom tolerance
await I.assertVisualMatch('dashboard', { tolerance: 2.5 });

// Element-specific comparison
await I.assertVisualMatch('navigation', { element: '#main-nav' });

// Excluding dynamic regions
await I.assertVisualMatch('profile-page', {
  ignoreRegions: [
    { x: 10, y: 10, width: 200, height: 50 }  // Timestamp area
  ]
});

Comparing Without Assertions

The compareVisual method performs comparison and returns detailed results without throwing exceptions:

const result = await I.compareVisual('checkout-form');

if (!result.isMatch) {
  console.log(`Visual difference detected: ${result.differencePercent}%`);
  console.log(`Diff image saved to: ${result.diffImagePath}`);
}

Capturing Screenshots

// Capture current viewport as actual image
await I.captureScreenshot('landing-page');

// Capture and save as new baseline
await I.captureScreenshot('landing-page', 'baseline');

// Capture specific element
await I.captureScreenshot('footer', 'baseline', 'footer.main');

Comparison Options

OptionTypeDescription
tolerancenumberOverride the default tolerance for this comparison
compareWithstringSpecify a custom baseline image name
elementstringCSS selector for element-specific comparison
boundsobjectBounding box { x, y, width, height } to constrain comparison
ignoreRegionsarrayArray of regions [{ x, y, width, height }] to exclude
pixelmatchArgsobjectOverride pixelmatch library options

Baseline Variations

The helper supports multiple baseline variations using the tilde (~) suffix convention:

homepage.png           # Primary baseline
homepage~dark.png      # Dark theme variant
homepage~tablet.png    # Tablet viewport variant

During comparison, the helper evaluates all matching variations and reports the best match, enabling comprehensive testing across different visual states.

Comparison Result Object

interface VisualComparisonResult {
  isMatch: boolean;           // Whether difference is within tolerance
  differencePercent: number;  // Percentage of differing pixels
  differentPixels: number;    // Count of differing pixels
  totalPixels: number;        // Total pixels in the image
  comparedPixels: number;     // Pixels compared (excluding masked regions)
  variationName: string;      // Name of the matched variation
  diffImagePath: string;      // Path to the generated diff image
  variations: Array<...>;     // Results for all evaluated variations
}

Development 🛠️

Project Structure

project/helper/
├── src/
│   ├── index.js              # Main VisualHelper class
│   ├── index.d.ts            # TypeScript declarations
│   ├── config/
│   │   └── defaults.js       # Default configuration values
│   ├── drivers/
│   │   └── adapter.js        # Browser driver abstraction layer
│   ├── services/
│   │   ├── comparator.js     # Image comparison logic
│   │   └── path-builder.js   # File path construction
│   └── utils/
│       ├── conversion.js     # Type conversion utilities
│       ├── filesystem.js     # File system operations
│       └── image.js          # PNG and WebP manipulation functions
└── tests/
    ├── config/               # Configuration tests
    ├── services/             # Service tests
    ├── utils/                # Utility tests
    └── integration/          # Visual comparison integration tests

Running Tests

# Execute all tests (unit, integration, e2e)
task test

# Execute helper unit tests only
task project:test:helper

# Execute integration tests with visual diff output
task project:test:helper:integration

# Execute CodeceptJS end-to-end tests
task project:test:helper:e2e

Integration Tests

The integration tests generate actual diff images that may be inspected visually. Following execution, examine the project/helper/test-output/ directory to review:

  • Identical comparison — Baseline and actual images with zero differences
  • Different comparison — Images with intentional differences and highlighted diff output
  • Tolerance comparison — Demonstration of strict versus lenient tolerance settings
  • UI simulation — Complex page comparison with ignore regions

Release & Publishing

The helper is designed to be published as an npm package. The following tasks support the release workflow:

# Inspect package contents locally without publishing
task project:release:helper

# Check the publishing authorization rules in an isolated runner
task project:test:npm

GitLab CI publishes from the protected default branch using npm trusted publishing (OIDC). The release job takes the default runner tag, served by the group's self-hosted runners, and requests the npm identity. npm rejects provenance from self-hosted runners, so versions after 3.0.0 carry no provenance attestation. No long-lived npm token or local .npmrc is needed.

Licence 📜

EUPL-1.2

版本与发布记录

@digital-commons-official/codeceptjs-visual-helper 共发布 44 个版本,首次发布于 2025/11/29;过去 12 个月发布了 44 个版本。

版本发布日期距今
3.0.2latest2026/11/038 天前
3.0.12026/11/039 天前
3.0.02026/11/039 天前
2.0.132026/10/1329 天前
2.0.122026/10/1230 天前
2.0.112026/10/121 个月前

关于 @digital-commons-official/codeceptjs-visual-helper 的常见问题

@digital-commons-official/codeceptjs-visual-helper 是什么?

Visual regression testing helper for CodeceptJS with Playwright, Puppeteer, WebDriver, Appium & TestCafe support

如何安装 @digital-commons-official/codeceptjs-visual-helper?

在项目目录运行 npm install @digital-commons-official/codeceptjs-visual-helper,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:codeceptjs(>=3.0.0)、@codeceptjs/helper(>=1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@digital-commons-official/codeceptjs-visual-helper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@digital-commons-official/codeceptjs-visual-helper 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@digital-commons-official/codeceptjs-visual-helper 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@digital-commons-official/codeceptjs-visual-helper 可以商用吗?

@digital-commons-official/codeceptjs-visual-helper 使用 EUPL-1.2 许可。声明的许可为 EUPL-1.2,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@digital-commons-official/codeceptjs-visual-helper 的下载量有多少?

根据 npm 官方下载统计,@digital-commons-official/codeceptjs-visual-helper 在 2026/10/12 – 2026/11/10 这 30 天内被下载 14,938 次,日均约 498 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digital-commons-official/codeceptjs-visual-helper 还在维护吗?

最新版本 3.0.2 发布于 2026/11/03(8 天前);至今共发布 44 个版本,其中过去 12 个月发布了 44 个;首次发布于 2025/11/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。