|
| 1 | +import { expect, Page, TestInfo } from '@playwright/test' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as path from 'path' |
| 4 | + |
| 5 | +/** |
| 6 | + * CSS injected before every visual snapshot to hide dynamic content |
| 7 | + * that changes between runs. Playwright's toHaveScreenshot() already |
| 8 | + * handles animations (animations: 'disabled') and caret (caret: 'hide'), |
| 9 | + * so we only target app-specific volatile elements here. |
| 10 | + */ |
| 11 | +const STABILISING_CSS = ` |
| 12 | + /* Hide environment select (contains dynamic API key) */ |
| 13 | + #environment-select { |
| 14 | + visibility: hidden !important; |
| 15 | + } |
| 16 | +
|
| 17 | + /* Hide timestamps and relative dates */ |
| 18 | + .ago, |
| 19 | + time, |
| 20 | + [data-test*="timestamp"], |
| 21 | + [data-test*="ago"], |
| 22 | + .text-muted:has(> .ago), |
| 23 | + .relative-date { |
| 24 | + visibility: hidden !important; |
| 25 | + } |
| 26 | +
|
| 27 | + /* Hide loading spinners */ |
| 28 | + .spinner, |
| 29 | + .loader, |
| 30 | + [class*="spinner"], |
| 31 | + [class*="loader"] { |
| 32 | + display: none !important; |
| 33 | + } |
| 34 | +
|
| 35 | + /* Hide any live chat / support widgets */ |
| 36 | + .intercom-launcher, |
| 37 | + #intercom-container, |
| 38 | + .drift-widget, |
| 39 | + [class*="chatbot"], |
| 40 | + iframe[title*="chat"], |
| 41 | + iframe[title*="Chat"] { |
| 42 | + display: none !important; |
| 43 | + } |
| 44 | +
|
| 45 | + /* Stabilise scrollbars across platforms */ |
| 46 | + ::-webkit-scrollbar { |
| 47 | + display: none !important; |
| 48 | + } |
| 49 | + * { |
| 50 | + scrollbar-width: none !important; |
| 51 | + } |
| 52 | +` |
| 53 | + |
| 54 | +/** Directory where screenshots are captured during E2E runs */ |
| 55 | +const SCREENSHOTS_DIR = path.resolve(process.cwd(), 'e2e', 'visual-regression-screenshots') |
| 56 | + |
| 57 | +/** Directory where baselines live (downloaded from main in CI) */ |
| 58 | +const BASELINES_DIR = path.resolve(process.cwd(), 'e2e', 'visual-regression-snapshots') |
| 59 | + |
| 60 | +/** |
| 61 | + * Whether visual regression snapshots are enabled for this run. |
| 62 | + */ |
| 63 | +export function isVisualRegressionEnabled(): boolean { |
| 64 | + return process.env.VISUAL_REGRESSION === '1' |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Wait for the page to settle before taking a screenshot. |
| 69 | + */ |
| 70 | +async function preparePage(page: Page): Promise<void> { |
| 71 | + await page.addStyleTag({ content: STABILISING_CSS }) |
| 72 | + |
| 73 | + // Wait for images to finish loading |
| 74 | + await page |
| 75 | + .evaluate(() => { |
| 76 | + return Promise.all( |
| 77 | + Array.from(document.images) |
| 78 | + .filter((img) => !img.complete) |
| 79 | + .map( |
| 80 | + (img) => |
| 81 | + new Promise((resolve) => { |
| 82 | + img.addEventListener('load', resolve) |
| 83 | + img.addEventListener('error', resolve) |
| 84 | + setTimeout(resolve, 5000) |
| 85 | + }), |
| 86 | + ), |
| 87 | + ) |
| 88 | + }) |
| 89 | + .catch(() => {}) |
| 90 | + |
| 91 | + // Double rAF to ensure paint is complete |
| 92 | + await page.evaluate(() => { |
| 93 | + return new Promise((resolve) => { |
| 94 | + requestAnimationFrame(() => { |
| 95 | + requestAnimationFrame(() => { |
| 96 | + resolve(undefined) |
| 97 | + }) |
| 98 | + }) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + // Small settle time for any final layout shifts |
| 103 | + await page.waitForTimeout(500) |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Take a screenshot during E2E tests and save it to the screenshots directory. |
| 108 | + * |
| 109 | + * This ONLY captures the screenshot — it does NOT compare against baselines. |
| 110 | + * Comparison happens as a separate step after all E2E retries have completed, |
| 111 | + * via `npx tsx e2e/compare-visual-regression.ts`. |
| 112 | + * |
| 113 | + * @param page Playwright page |
| 114 | + * @param name Descriptive snapshot name, e.g. "features-list" |
| 115 | + * @param testInfo Playwright testInfo for resolving the test file name |
| 116 | + */ |
| 117 | +export async function visualSnapshot( |
| 118 | + page: Page, |
| 119 | + name: string, |
| 120 | + testInfo: TestInfo, |
| 121 | +): Promise<void> { |
| 122 | + if (!isVisualRegressionEnabled()) return |
| 123 | + |
| 124 | + await preparePage(page) |
| 125 | + |
| 126 | + // Save with the sanitised name Playwright's toMatchSnapshot expects: |
| 127 | + // {testFileName}--{name} with dots replaced by dashes |
| 128 | + const testFileName = path.basename(testInfo.file) |
| 129 | + const sanitisedName = `${testFileName}--${name}`.replace(/\./g, '-') |
| 130 | + fs.mkdirSync(SCREENSHOTS_DIR, { recursive: true }) |
| 131 | + |
| 132 | + const screenshotPath = path.join(SCREENSHOTS_DIR, `${sanitisedName}.png`) |
| 133 | + await page.screenshot({ |
| 134 | + path: screenshotPath, |
| 135 | + fullPage: true, |
| 136 | + animations: 'disabled', |
| 137 | + caret: 'hide', |
| 138 | + scale: 'css', |
| 139 | + }) |
| 140 | +} |
0 commit comments