Automated layout validation tool using Playwright. Ensures responsive design accuracy across multiple viewports by inspecting element positions and visual alignment.
Ensuring consistent visual alignment across responsive layouts is a recurring challenge in front-end development. Small CSS changes or unintended layout shifts can break the alignment between related elements (for example, labels and inputs, icons and text blocks, or mirrored components across mobile and desktop pages), and these regressions are often hard to catch with functional tests alone. Visual test snapshots help, but they can be noisy and brittle for layout-specific assertions.
This library provides Playwright matchers focused specifically on geometric relationships between elements: horizontal and vertical alignment, equal widths or heights, centered positioning, and other spatial checks. By expressing layout expectations as deterministic assertions, teams can:
The matchers integrate naturally with Playwright tests and return clear diagnostics (positions and sizes) to make failures actionable and reduce the feedback loop for front-end fixes.
npm install --save-dev flexpect
To register all matchers automatically, you can still use:
// playwright.config.ts or global-setup file
import 'flexpect/register'; // registers custom expect matchers
If you prefer not to register all matchers provided by flexpect, you can import and register only the ones you need manually in your Playwright setup (or any other test runner like Jest or Vitest):
import { expect } from '@playwright/test';
import { toBeAlignedWith, toHaveAspectRatio } from 'flexpect';
// Register only the matchers you need
expect.extend({ toBeAlignedWith, toHaveAspectRatio });
This approach allows you to:
Example: assert two elements are horizontally aligned within a 2px tolerance.
// test.spec.ts
test('labels align with inputs', async ({ page }) => {
const label = await page.locator('label[for="email"]');
const input = await page.locator('#email');
await expect(label).toBeAlignedWith(input, Axis.Horizontal, Alignment.Start, {
tolerance: 2,
toleranceUnit: ToleranceUnit.Pixel,
});
});
Example: assert same width
await expect(cardA).toHaveSameWidthAs(cardB, { tolerance: 17, toleranceUnit: ToleranceUnit.Percent });
See the test folder for full test files and fixtures.
For detailed documentation on all available matchers and their usage, please visit the documentation.
Contributions welcome. Workflow:
Please follow the existing code style and include unit and integration tests for new matchers.
This project is licensed under the MIT License - see the LICENSE file for details.