Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(interaction-harness): a trace waits out the recorder's last frame (#9962)

Christopher Chedeaud0cf29a20676
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index 224204cd66..d62e2483d8 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -288,7 +288,9 @@ harness's own animation recorder stands down while the trace runs, since its
frame loop would make every frame a main-thread one, restyling each running
animation's target and laying out an anchored one: it reads only the element a
CSS animation or transition event names, once, when that animation starts, and
-catches up afterwards. A `waitForRest` awaited inside the traced body stands
+catches up afterwards. The frame it requested before the trace runs before the
+start mark, so the whole window counts none of its work. A `waitForRest`
+awaited inside the traced body stands
down too: it waits on timers for the scheduled ends of the element's curves
(looking `frames` times a frame apart before settling on none, so an
animation the action starts a frame or two later is waited for) and polls no
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index f1735bdfbb..a9ad422a61 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -1398,6 +1398,54 @@ test.describe('frame evidence', () => {
expect(top.counts.Layout ?? 0).toBeGreaterThanOrEqual(top.frames - 1);
});
+ test("the recorder's last frame before a trace runs before its start mark: an anchored layer that animates its transform counts no Layout in the whole window", async ({
+ page,
+ browserName,
+ }) => {
+ test.skip(
+ browserName !== 'chromium',
+ 'rendering traces are Chromium-only (README: engine limits)',
+ );
+ // An anchored layer lays out again in every frame that runs a frame
+ // callback, and the recorder's frame loop requests one each frame.
+ await page.setContent(
+ `<style>body { margin: 0; } #anchor { anchor-name: --a; position: absolute; left: 40px; top: 40px; width: 80px; height: 40px; } #layer { position: absolute; position-anchor: --a; top: anchor(bottom); left: anchor(left); width: 160px; height: 120px; background: #ddd; }</style><button id="anchor">Anchor</button><div id="layer"></div>`,
+ );
+ const layer = page.locator('#layer');
+ await layer.evaluate(el => {
+ el.animate([{transform: 'scale(1)'}, {transform: 'scale(1.2)'}], {
+ duration: 3000,
+ iterations: Infinity,
+ direction: 'alternate',
+ });
+ });
+ // Arms the recorder: from here its frame loop runs.
+ await waitForAnimation(layer);
+ await frames(page, 3);
+ const steady = (trace: Awaited<ReturnType<typeof traceRendering>>) =>
+ countBetween(trace, trace.window.from + 100, trace.window.to - 100)
+ .Layout ?? 0;
+ const quiet = await traceRendering(page, () => page.waitForTimeout(500));
+ // None over the whole window: the frame the recorder requested before it
+ // stood down runs before the start mark.
+ expect(quiet.layout).toBe(0);
+ // Control: a frame loop of the page's own makes the layer lay out each
+ // frame, and the trace counts it.
+ const busy = await traceRendering(page, async () => {
+ await page.evaluate(() => {
+ const end = performance.now() + 500;
+ const frame = () => {
+ if (performance.now() < end) {
+ requestAnimationFrame(frame);
+ }
+ };
+ requestAnimationFrame(frame);
+ });
+ await page.waitForTimeout(500);
+ });
+ expect(steady(busy)).toBeGreaterThan(10);
+ });
+
test("the trace counts no Layout from the harness's own animation recorder, which stands down while it runs", async ({
page,
browserName,
diff --git a/internal/interaction-harness/src/trace.ts b/internal/interaction-harness/src/trace.ts
index eb45378e42..d24b95d57b 100644
--- a/internal/interaction-harness/src/trace.ts
+++ b/internal/interaction-harness/src/trace.ts
@@ -22,6 +22,7 @@
*/
import type {CDPSession, Page} from '@playwright/test';
+import {frames} from './story';
/**
* The mark a harness function puts on its own page-side work
@@ -184,6 +185,10 @@ export async function traceRendering(
}
}, on);
await standDown(true);
+ // The frame the recorder requested before it stood down runs now, before the
+ // start mark. A frame callback makes its frame a main-thread one, in which an
+ // anchored layer that animates lays out.
+ await frames(page, 1);
let fromEpoch: number;
let toEpoch: number;
try {