Skip to content

MQ37/layoutguard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” LayoutGuard

LayoutGuard is a command-line interface (CLI) tool designed to automate visual regression testing for web applications. It uses Playwright to drive a browser, execute user-defined scenarios, take screenshots, and compare them against previously approved "golden" snapshots.

πŸ€– Note: This project was entirely generated using the Qwen3-Coder-Plus model, showcasing the capabilities of AI in software development.

🌟 Features

  • 🎭 Scenario-based Testing: Define test scenarios in simple TypeScript files.
  • πŸ“ Centralized Snapshot Management: Manage all artifacts in a root .layoutguard directory.
  • βœ… Interactive Approval Workflow: Review and approve new or changed snapshots.
  • πŸ€– CI/CD Integration: Exits with a non-zero status code if visual differences are detected.
  • 🎯 Flexible Targeting: Specify which element to capture in a screenshot.
  • πŸ–ΌοΈ Immediate Diff Viewing: Automatically open a visual diff image when a test fails.

πŸš€ Installation

npm install -g layoutguard

Or use it directly with npx:

npx layoutguard init

After initializing, you'll need to install the Playwright browsers you want to use:

npx playwright install chromium  # For Chromium/Chrome
npx playwright install firefox   # For Firefox
npx playwright install webkit    # For Safari

You can also install specific versions or channels:

npx playwright install chromium@beta

πŸ“– Usage

πŸ†• Initialize a new project

npx layoutguard init

This command sets up the necessary configuration and directory structure:

  • Creates a layoutguard.config.json file
  • Creates a .layoutguard directory with snapshots and failures subdirectories
  • Creates an examples directory with an example test file

After running init, you'll need to install the Playwright browsers you want to use:

npx playwright install chromium  # For Chromium/Chrome
npx playwright install firefox   # For Firefox
npx playwright install webkit    # For Safari

βš™οΈ Configuration

The layoutguard.config.json file allows you to customize behavior:

{
  "testMatch": ["**/*.spec.ts"],
  "baseUrl": "http://localhost:3000",
  "playwright": {
    "browserName": "chromium"
  },
  "diffThreshold": 0.01
}
  • testMatch: A glob pattern to find test files.
  • baseUrl: The base URL to prepend to navigation actions.
  • playwright.browserName: Which browser to use (chromium, firefox, or webkit).
  • diffThreshold: Threshold for pixel difference (0 to 1). 0 means zero tolerance.

✍️ Writing Tests

Test scenarios are defined in .spec.ts files. Each file exports a default object that conforms to the LayoutTest interface:

import { Page } from 'playwright';
import type { LayoutTest } from 'layoutguard';

const test: LayoutTest = {
  name: 'Dashboard Add Form Popup',
  scenario: async (page: Page) => {
    await page.goto('/dashboard');
    await page.click('#add-new-item-button');
    await page.waitForSelector('.add-item-popup', { state: 'visible' });
    await page.waitForTimeout(500); // Wait for CSS transitions
  },
  selector: '.add-item-popup', // Optional: specify an element to capture
};

export default test;
  • name: A unique, descriptive name for the test. This is used for the snapshot filename.
  • scenario: The sequence of actions to perform before taking the screenshot.
  • selector: Optional. A CSS selector for the element to capture. If omitted, the full page is captured.

▢️ Running Tests

npx layoutguard check [test]

This command executes visual regression tests:

  • If [test] is omitted, layoutguard will run all tests found.
  • [test] can be either the test name (from the test object's name field) or a direct path to a test file (relative or absolute).
  • Use the --show-diff option to automatically open the visual diff image if a test fails.

βœ… Approving Snapshots

npx layoutguard approve [test]

This command runs the specified test(s) and replaces the existing golden snapshots with the newly generated ones:

  • If [test] is omitted, it will update snapshots for all found tests.
  • [test] can be either the test name (from the test object's name field) or a direct path to a test file (relative or absolute).

πŸ“ Directory Structure

my-awesome-project/
β”œβ”€β”€ .layoutguard/
β”‚   β”œβ”€β”€ snapshots/
β”‚   β”‚   β”œβ”€β”€ dashboard-add-form-popup.png  // Golden snapshot
β”‚   β”‚   └── another-test.png
β”‚   └── failures/
β”‚       └── dashboard-add-form-popup/
β”‚           β”œβ”€β”€ new.png       // The latest screenshot
β”‚           β”œβ”€β”€ original.png  // The golden snapshot it was compared against
β”‚           └── diff.png      // A visual diff of the two
β”œβ”€β”€ node_modules/
β”œβ”€β”€ src/
β”‚   └── components/
β”‚       └── dashboard/
β”‚           β”œβ”€β”€ Dashboard.tsx
β”‚           └── dashboard.spec.ts  // Test file co-located with component
β”œβ”€β”€ layoutguard.config.json
β”œβ”€β”€ package.json
└── tsconfig.json

πŸ“„ License

MIT

About

πŸ” CLI tool for visual regression testing with Playwright

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors