Visual regression


Visual regression compares a screenshot of the current page against a stored baseline and records the result as pass or fail on the run. Use it to guard against unintended visual changes on a page you have already validated.

Running it in CI? See Visual regression in CI for the baseline-cache pattern, Job Summary snippet and the two options to publish the HTML report.

#Take a checkpoint

Call visualCheckpoint($label) at any point in a step where you want to compare against a baseline. The label identifies the checkpoint across runs.

Copied!
1$this
2->it('the checkout addresses step is unchanged', function () use ($frontOfficeCheckoutPage) {
3 $frontOfficeCheckoutPage->confirmAddresses();
4 
5 $this->visualCheckpoint('checkout-tunnel-addresses');
6});

The first time this checkpoint is seen, the screenshot is stored as the baseline and the check reports pass. On subsequent runs, the current screenshot is diffed against the baseline: identical → pass, above the similarity threshold → pass, below the threshold → fail.

#Full-page or viewport

By default visualCheckpoint captures the full page (including content scrolled out of view). Pass false as the second argument to capture only the current viewport.

Copied!
1// Full-page (default): great for landing pages and long tunnels.
2$this->visualCheckpoint('cart');
3 
4// Viewport-only: great for a header, a toolbar, a modal.
5$this->visualCheckpoint('order-shipping-modal', false);

#Tag a checkpoint

Baselines are keyed by (name, tag), so the same checkpoint can hold several baselines side-by-side — one per PS major version, viewport size or locale. Files are named <name>--<tag>.png.

visualCheckpoint() takes an optional last argument:

Copied!
1public function visualCheckpoint(
2 string $name,
3 ?string $selector = null,
4 float $threshold = 0.98,
5 bool $fullPage = true,
6 string $tag = 'auto',
7 array $masks = [], // since 1.8.0, see "Mask moving parts"
8): void
  • 'auto' (default) — the tag is derived from the current PS major version, the viewport size and the locale, in the shape auto-v9-1280x800-fr. Any missing segment becomes a literal ? so the shape stays stable.
  • A free string — used as-is, must match ^[a-z0-9._-]+$. An invalid tag raises InvalidArgumentException — the library never silently rewrites it.
Copied!
1// Auto tag — different PS majors get their own baseline.
2$this->visualCheckpoint('cart');
3 
4// Free tag — pin an alternative baseline for a promo variant.
5$this->visualCheckpoint('home', null, 0.98, true, 'promo-black-friday');

#visual[] block in results.json

Every checkpoint is exported in the current test's visual[] array of results.json:

Copied!
1{
2 "visual": [
3 {
4 "name": "checkout-tunnel-addresses",
5 "tag": "auto-v9-1280x800-fr",
6 "status": "pass",
7 "score": 0.9987,
8 "threshold": 0.98,
9 "actual_relpath": null,
10 "diff_relpath": null
11 }
12 ]
13}

status is one of baseline (first sighting — stored as reference), pass or fail. actual_relpath / diff_relpath are populated only for fail entries (the CI action needs them to upload the artefacts) and are null otherwise. This block is what feeds the MVP2 API-side baseline / approve UI loop.

#Frame a viewport capture on an anchor

Viewport-only captures show whatever happens to be at the top of the page — often a header, a promo banner or a cookie strip, not the piece you actually want to compare. CommonPage::scrollToAnchor($selector, $settleMs = 400) scrolls the matching element to the top of the viewport before the capture, so the frame lands on the business content.

Copied!
1$this->frontOfficePage->waitFor('.login-form'); // guarantee presence
2$this->frontOfficePage->scrollToAnchor('.login-form');
3$this->frontOfficePage->visualCheckpoint('login', null, 0.98, false);

Best-effort by design: if the selector is absent, the call is a no-op — the library doesn't know your project's DOM, so it's up to the suite to guarantee the anchor is present (via waitFor or similar) before calling. The optional $settleMs gives the browser a moment for a smooth-scroll animation to finish before the screenshot is taken.

#Mask moving parts

A carousel, a countdown or a "last viewed" block changes from one run to the next and makes a checkpoint fail for no reason. Pass their selectors as $masks: each matching element is hidden (visibility: hidden, so the layout does not move) for the capture only.

Copied!
1$frontOfficePage->visualCheckpoint(
2 'home',
3 null,
4 0.98,
5 true,
6 'auto',
7 ['.carousel', '#countdown', '.js-product-viewed'],
8);

Each selector gets its own CSS rule, so an invalid one does not stop the others from being masked. The masks are removed after the capture even when it fails.

#Wait for a stable page, and skip the header

Two best-effort helpers that never throw, since a capture that differs is better evidence than a helper that crashes:

  • waitForStable($timeout = 10000) waits until the page has fully loaded, its fonts are ready and its images are decoded. Lazy images below the fold are ignored, since they would never load without a scroll.
  • scrollBelow($selector, $settleMs = 400) scrolls to just below the matching element, for a viewport capture that starts after the header or a banner. It is the counterpart of scrollToAnchor(), which puts the element itself at the top.
Copied!
1$frontOfficePage->goToUrl($url);
2$frontOfficePage->waitForStable();
3$frontOfficePage->scrollBelow('#header');
4$frontOfficePage->visualCheckpoint('category-listing', null, 0.98, false);

#Data-driven visual suites

VisualTestsSuite covers the common case, "these pages, on these devices, in these languages", without writing a step. You declare three literal arrays and the suite generates one step per checkpoint. This is also the format the PrestaFlow app edits.

Copied!
1namespace Tests\Visual;
2 
3use PrestaFlow\Library\Tests\VisualTestsSuite;
4 
5class StorefrontVisual extends VisualTestsSuite
6{
7 protected array $devices = ['desktop', 'mobile'];
8 protected array $locales = ['fr', 'en'];
9 
10 protected array $checkpoints = [
11 ['name' => 'home', 'path' => '/', 'zone' => 'full', 'masks' => ['.carousel']],
12 ['name' => 'login', 'path' => '/connexion', 'paths' => ['en' => '/login'], 'waitFor' => '.login-form'],
13 ['name' => 'footer', 'path' => '/', 'zone' => 'element', 'selector' => '#footer', 'excludeDevices' => ['mobile']],
14 ['name' => 'promotions', 'path' => '/promotions', 'paths' => ['en' => false]],
15 ];
16}

One run covers one device × locale combination. The device comes from the PRESTAFLOW_DEVICE environment variable, the locale from PRESTAFLOW_LOCALE. Without them, the suite uses the first device and the first locale it declares. A value the suite does not declare fails the run. To cover the whole matrix, run it once per combination:

Copied!
1PRESTAFLOW_DEVICE=mobile PRESTAFLOW_LOCALE=en vendor/bin/prestaflow run tests/Visual

Each checkpoint takes:

Key Default Meaning
name (required) Checkpoint name, as in visualCheckpoint().
path '' Path under the front-office URL. The locale prefix is added when PRESTAFLOW_PREFIX_LOCALE is on.
paths [] Per-locale override of path. false skips the checkpoint in that locale.
zone viewport viewport, full (full page) or element.
selector null The element to capture when zone is element.
waitFor null A selector to wait for before the capture.
scrollBelow null A selector to scroll past before a viewport capture.
threshold 0.98 Similarity threshold, clamped between 0.5 and 1.0.
excludeDevices [] Devices on which the checkpoint is skipped.
masks [] Selectors to hide during the capture.

Every step waits for a stable page before its capture. A checkpoint excluded on the current device, or disabled in the current locale, is reported as skipped rather than left out, so the report shows the whole matrix. Baselines use the auto tag, which includes the viewport size and the locale, so every combination keeps its own.

#Devices

The presets live in PrestaFlow\Library\Visual\VisualDevices:

Device Window User agent
desktop 1920 × 1080 Chrome on Linux
tablet 768 × 1024 Safari on iPad
mobile 390 × 844 Safari on iPhone

Each user agent ends with PrestaFlow. Switching device relaunches the browser, since a running browser keeps the size it was launched with.

#Browser options

When you drive the library from your own code (see Without CLI), you can set the window size and user agent for the next browser launch:

Copied!
1use PrestaFlow\Library\Tests\TestsSuite;
2 
3TestsSuite::useBrowserOptions(390, 844, 'Mozilla/5.0 (iPhone; …) PrestaFlow');
4TestsSuite::resetBrowser(); // close the current browser so the next one uses them
  • useBrowserOptions($width, $height, $userAgent) wins over PRESTAFLOW_WINDOW_SIZE_* and PRESTAFLOW_USER_AGENT. Pass null for a value to keep its default, or three nulls to clear the override.
  • browserOptions() returns what the next launch will use.
  • resetBrowser() closes the browser and forgets it. The options only apply to a new browser, so call it after changing them.

A visual suite leaves options set this way untouched.

#Baselines

Baselines are stored in a persistent folder (outside the ephemeral screens/ folder), so they survive between runs. Commit the baseline files alongside the suites that produce them so CI can compare against them.

To refresh a baseline after an intentional visual change, delete the file(s) for the affected checkpoint(s) and re-run the suite: the next run stores the new screenshot as the baseline.

#Report

Ask the CLI to write an HTML report at the end of the run:

Copied!
1bin/prestaflow run tests --visual-report

Or pick a custom path:

Copied!
1bin/prestaflow run tests --visual-report=reports/visual/2026-07-06.html

A visual-results.json side-file is written next to the HTML report. The report shows the reference, the actual capture and the diff side by side for each failing checkpoint.

#Time zone of the stamp

The report includes a "generated at" timestamp. By default it is in UTC; set PRESTAFLOW_TZ (or pass --visual-report-tz=…) to render the human timestamp in your local zone (the ISO attribute keeps its offset).

Copied!
1PRESTAFLOW_TZ=Europe/Paris bin/prestaflow run tests --visual-report