Skip to content

Commit d16cf82

Browse files
Copilotarii
andcommitted
refactor: extend waitForVRTReady with optional targetWidth; trim expect.poll comment
Co-authored-by: arii <342438+arii@users.noreply.github.com>
1 parent 7cda125 commit d16cf82

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

tests/playwright/lib/visual.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,22 @@ export const SCREENSHOT_OPTIONS = {
3636
/**
3737
* Waits for the page to be ready for visual regression testing.
3838
*
39-
* Minimum contract:
40-
* 1. Awaits fonts readiness.
41-
* 2. Awaits network idle.
42-
* 3. Forces layout reflow to ensure geometry is stable.
43-
*
4439
* @param page - The Playwright Page object to prepare.
40+
* @param targetWidth - Optional expected viewport width; polls until `clientWidth` matches.
4541
*/
46-
export async function waitForVRTReady(page: Page): Promise<void> {
42+
export async function waitForVRTReady(
43+
page: Page,
44+
targetWidth?: number
45+
): Promise<void> {
4746
await page.evaluateHandle(() => document.fonts.ready)
4847
await page.waitForLoadState('networkidle')
4948
await page.evaluate(() => document.body.offsetHeight)
49+
if (targetWidth !== undefined) {
50+
await page.waitForFunction(
51+
(w) => document.body.clientWidth === w,
52+
targetWidth
53+
)
54+
}
5055
}
5156

5257
/**
@@ -77,10 +82,8 @@ export async function takeScreenshot(
7782
await checkAccessibility(target)
7883
}
7984

80-
// For Locator targets, poll until scrollHeight stabilizes across two consecutive reads.
81-
// This ensures CSS Grid/Flexbox reflows (e.g. after setViewportSize) are fully settled
82-
// before the snapshot is taken. A simple one-shot evaluate would capture the unsettled
83-
// height immediately, since JS evaluates arguments before the function runs.
85+
// Poll until scrollHeight stabilizes: CSS Grid/Flexbox reflows after setViewportSize
86+
// settle asynchronously; two consecutive matching reads confirm layout is done.
8487
if (isLocator) {
8588
const locator = target as Locator
8689
let previousHeight: number | null = null

tests/playwright/vrt-dashboard.spec.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
setupVisualRegressionTest,
66
resetServerState,
77
} from './lib'
8-
import { takeScreenshot, assertFixedDimensions } from './lib/visual'
8+
import { takeScreenshot, assertFixedDimensions, waitForVRTReady } from './lib/visual'
99
import { waitForPageReady } from './lib/waits'
1010
import { VRT_TIMEOUTS } from './lib/timeouts'
1111
import { stopTimer } from './lib/setup'
@@ -152,11 +152,7 @@ test.describe('Visual Regression Tests', () => {
152152
// NEW: Responsive breakpoint tests
153153
test('mobile viewport', async () => {
154154
await dashboardPage.setViewportSize(MOBILE_VIEWPORT)
155-
// Lock viewport width before snapshot to prevent geometry drift in CI
156-
await dashboardPage.waitForFunction(
157-
(w) => document.body.clientWidth === w,
158-
MOBILE_VIEWPORT.width
159-
)
155+
await waitForVRTReady(dashboardPage, MOBILE_VIEWPORT.width)
160156
const dashboard = dashboardPage.getByTestId('dashboard')
161157
await takeScreenshot(dashboard, 'dashboard-mobile.png', {
162158
mask: getDynamicContentMasks(dashboardPage),
@@ -166,11 +162,7 @@ test.describe('Visual Regression Tests', () => {
166162

167163
test('tablet viewport', async () => {
168164
await dashboardPage.setViewportSize(TABLET_VIEWPORT)
169-
// Lock viewport width before snapshot to prevent geometry drift in CI
170-
await dashboardPage.waitForFunction(
171-
(w) => document.body.clientWidth === w,
172-
TABLET_VIEWPORT.width
173-
)
165+
await waitForVRTReady(dashboardPage, TABLET_VIEWPORT.width)
174166
const dashboard = dashboardPage.getByTestId('dashboard')
175167
await takeScreenshot(dashboard, 'dashboard-tablet.png', {
176168
mask: getDynamicContentMasks(dashboardPage),

0 commit comments

Comments
 (0)