fix(electron): measure toBeInViewport as Chromium clips the node
What
expect(locator).toBeInViewport() on an Electron target measures a node the way Chromium draws it, in each of these cases. On main, the tests for them fail (see Verified): a clipped node read as in view, and the window's own node failed.
| Page | Now |
|---|---|
A position: fixed box inside a transformed, filtered, or contained ancestor that clips | clipped by that ancestor, as the browser holds it |
A scaled ancestor (transform: scale(2)) | clipped where and at the scale it is drawn |
| A slotted element | clipped by the slot it is drawn in |
clip-path: inset/circle/ellipse/polygon(...) with lengths, %, or calc(), and an optional geometry box (content-box), on the node or an ancestor | clipped by the bounds of the shape, against that box, at the box's scale |
A CSS clip: rect(...) (the visually hidden pattern) | out of view |
overflow: clip with overflow-clip-margin, contain: paint, content-visibility | clips as drawn; the margin counts from the box it names, and widens paint containment and a clip on both axes, never a scroller |
An inline element's overflow, a body whose root element sets overflow, a body or root element with containment | as the browser does |
| A modal dialog or open popover (top layer) | clipped only by the window |
| A locator that matches the window itself, by its title | in view; toHaveCSS reads the root element |
Why
The measurement walked parentElement and clipped by each ancestor's getBoundingClientRect() when its overflow was not visible. That missed every case in the table, so toBeInViewport gave a different answer from web() on pages that use these patterns.
How it works
viewportIntersectionstarts from the node's box and narrows it by every ancestor that clips it, walkingrenderedParent(the assigned slot, the parent, or the shadow host) and stopping at the top layer (packages/electron/src/in-page/node-ops.ts:331-490).- Whether an ancestor holds the node follows CSS containing blocks: any positioned ancestor for an absolute box, and for a fixed box only one with a transform, filter, containment, or
will-changeof those (holdsFixed,node-ops.ts:342). - Each clipping ancestor's client box is scaled from its CSS size to the size it is drawn at (
getBoundingClientRectoveroffsetWidth).overflow-clip-marginis added from the padding, content, or border box it names, as Chromium applies it: to paint containment or anoverflow: clipon both axes, and not to a box that scrolls (hidden,scroll,auto). The body clips as any box does unless its overflow is the window's: the root element's overflow isvisibleand neither element has containment. clipPathOfreadsinset(),circle(),ellipse(), andpolygon()with lengths, percentages, andcalc(), against the geometry box after the shape (padding, content, or margin box; the border box by default). It clips by the shape's bounding box: a circle sized in the box's own pixels, with a keyword radius measured as an absolute distance to the sides, then scaled on each axis.clipRectOfreadsclip: rect()on an absolutely positioned box. Apath(), a URL reference, a mirrored or rotated transform, and the exact outline of a polygon are not modelled.ElectronSurfaceanswersviewportIntersectionandreadStylefor the window's own node (ROOT_ID): it is the viewport, and its styles are the root element's (packages/electron/src/surface.ts:556-580).packages/electron/tests/integration/node-ops.test.tscovers each row in Chromium; the testbed checks the window's own node.
Verified
Ran it locally: yes, on macOS (Apple Silicon) with Electron 45.5.0.
- This branch passes the Electron package's unit and integration tests, the Electron testbed run through the built CLI (
pnpm --filter @e2e-dev/electron-testbed test), andpnpm check, run atf2dbb32b0. After the review fixes, at905e53d66: the unit tests,node-ops.test.ts, typecheck, and oxlint pass, and each new case fails with its fix reverted. - main against this branch: this branch's tests run against
origin/main'spackages/electron/src. These tests fail there and pass here:
| Suite | Test | main | this PR |
|---|---|---|---|
| integration | the window's own node is the viewport, and has its root element's styles | fails | passes |
| integration | nodeOp clips a box by its own clip-path, by a circle of the default radius, and by nothing above the top layer | fails | passes |
| integration | nodeOp clips by content-visibility, and widens an overflow-clip-margin at the scale its box is drawn | fails | passes |
| integration | nodeOp clips by the body when the root element has an overflow of its own, which keeps the body's from being the window's | fails | passes |
| integration | queryAll reads computed styles, and how much of a box its scrollers and the window leave in view | fails | passes |
Each failing test holds several cases; on main the first case that fails stops it.
- Benchmark:
pnpm bench:ab --suite electron-testbed(main against this branch) is still running; its table replaces this line when it finishes. - Video: none attached. The change is in what the engine reads and when it acts, which the tests above pin down; a recording would show the same window either way.
- Local macOS runs, on both sides, started Electron with
-ApplePersistenceIgnoreState YESfrom #2500, applied without committing it. Without it, an Electron the tests killed makes macOS ask to reopen its windows, and the next launch waits out its timeout. The Linux and Windows CI jobs do not need it.
Fresh-context review (Codex gpt-6.2-sol, read-only, on this branch's diff):
- Fixed:
overflow-clip-marginwas always added to the padding box; it now counts from the box it names (70a18fd54, with a test). - Fixed: the reference now says what clips a node, and the changeset is added.
- Dismissed: a polygon clips by its bounding box, so a child inside the bounds but outside the shape counts as in view. Measuring by the shape's bounds is the documented approach (
clipPathOf); intersecting a polygon with a rectangle exactly is not modelled. - Dismissed: a mirrored ancestor (
scaleX(-1)) with aninset()clip keeps the wrong side. The measurement models scale, not reflection or rotation, which would need the whole transform chain.
Review bots, on the previous head:
- Fixed: a
circle()orellipse()centred outside its box took a negative keyword radius (d4ec31262), and aclip-pathwith a geometry box was skipped (bd39a9ce9). - Fixed:
overflow-clip-marginnow widens paint containment and no single-axis clip (2b383754a), and containment on the body or root element keeps the body's overflow its own (5bde8e6a7). Both checked against hit testing in Electron 45. - Fixed: the reference names the
clip-pathshapes it measures (905e53d66). - Dismissed: CodeQL's code-sanitization alert on a test that writes a constant page into a fixture app's source with
JSON.stringify, the same pattern as the other tests inlaunch.test.ts.
🤖 Generated with Claude Code
Summary by CodeRabbit
- New Features
- Viewport visibility checks now account for clipping, containment, transforms, fixed-position elements, and content rendered through slots or shadow trees.
- Electron window checks treat the window as the viewport and can read computed styles from the document’s root element.
- Documentation
- Updated Electron capability details to describe viewport clipping, supported clip-path behavior, and window locator behavior.