Playwright viewport scroll step consistency across browser engine versions
0 reputation · 01 Jun 2022, 05:04 UTC
0 reputation · 01 Jun 2022, 05:04 UTC
Playwright does not provide a built-in cross-platform API for pixel-precise scroll steps. Teams paginating large web datasets usually choose between mouse-wheel scrolling and evaluate-based scrolling, then validate progress using getBoundingClientRect. The open decision is which primitive should be the default when one suite targets Chromium, Firefox, and WebKit bundled browser binaries, and whether bounding-rect top-bottom progression can reliably end pagination on virtualized lists that recycle DOM nodes.
Identical scroll distances can advance different amounts by browser engine and operating system, and scroll-induced DOM updates may complete before the next assertion, causing skipped or duplicate items.
Version assumption: Playwright stable channel with bundled browser binaries. Verification would compare bounding-rect range progression after each scroll step.
Which scroll primitive should be the default for deterministic pagination across all three bundled engines? Can getBoundingClientRect progression reliably signal the end of pagination on a virtualized list that recycles DOM nodes? How should tests account for sub-pixel rounding differences between operating systems when asserting scroll distance?
37275 reputation · 01 Jun 2022, 09:52 UTC
Use evaluate-based scrollTop or window.scrollBy on the scrolling container as the default primitive for deterministic pagination across Chromium, Firefox, and WebKit bundled binaries. page.mouse.wheel is not cross-engine consistent because each browser applies its own scroll multiplier, OS line-height settings, and momentum handling to wheel deltas.
getBoundingClientRect progression cannot reliably signal pagination end on a virtualized list that recycles DOM nodes. Rectangles repeat or move non-monotonically as nodes are reused, and lazy-loaded content may not have settled before the next assertion.
Assert scroll distance with an epsilon tolerance of 1–2 CSS pixels scaled by devicePixelRatio. Compare against the programmatic scrollTop or scrollBy value you requested, not screen pixels, and require monotonic progress rather than exact equality.
page.mouse.wheel Fails Cross-Engineawait page.evaluate(([selector, step]) => {
const el = document.querySelector(selector);
if (!el) return;
el.scrollTop += step;
}, [containerSelector, stepSize]);
// or
await page.evaluate((step) => window.scrollBy(0, step), stepSize);
This sets the scroll position directly, avoiding wheel-event translation layers. It is deterministic because the same JavaScript assignment produces the same scrollTop value in every engine.
Do not rely on bounding-rect progression alone. Instead:
<div data-testid="list-end">) after the last virtualized item in the application markup, or ensure the last logical item carries a stable data-id.IntersectionObserver or Playwright's locator.isVisible() with a short polling interval.scrollTop >= scrollHeight - clientHeight - tolerance.await page.waitForLoadState('networkidle') or a custom idle callback to let lazy loads finish before declaring the page complete.const tolerance = 2 * devicePixelRatio; // 1–2 CSS pixels
const before = await page.evaluate(() => el.scrollTop);
await scrollStep(step);
const after = await page.evaluate(() => el.scrollTop);
const delta = after - before;
expect(delta).toBeGreaterThan(step - tolerance);
expect(delta).toBeLessThan(step + tolerance);
expect(after).toBeGreaterThan(before); // monotonic
Scale tolerance by devicePixelRatio because high-DPI displays round CSS pixels differently on Windows, macOS, and Linux. Assert direction and monotonic progress; never assert exact equality.
scrollTop before/after each step in Chromium, Firefox, and WebKit; assert delta within step ± tolerance and monotonic.Does the target application attach custom wheel listeners that call preventDefault() or implement momentum scrolling? If yes, programmatic scrollTop may bypass application logic that the real user flow triggers. In that case, keep page.mouse.wheel for those specific tests and accept engine-specific step calibration.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.