Skip to content

Commit 5cab4e9

Browse files
committed
fix(server): capture recordings after finite page transitions
1 parent 6e64358 commit 5cab4e9

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎apps/server/src/preview/ServerBrowserPage.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,41 @@ describe("server browser snapshot screenshot", () => {
430430
}).toEqual({ size: [1280, 800], fullSize: [2560, 1600], colors: ["blue", "red", "red"] });
431431
});
432432

433+
it("records the destination of a finite entrance animation without waiting on infinite ones", async () => {
434+
const page = await context.newPage();
435+
await page.setContent(`
436+
<style>
437+
body { margin: 0; background: #2563eb; }
438+
@keyframes enter { from { opacity: 0; } to { opacity: 1; } }
439+
@keyframes pulse { from { opacity: 0.5; } to { opacity: 1; } }
440+
button { animation: pulse 2s infinite; }
441+
#paused { animation: enter 2s paused; }
442+
#overlay { display: none; position: fixed; inset: 0; background: #ef4444; }
443+
#overlay.open { display: block; animation: enter 200ms both; }
444+
</style>
445+
<button onclick="document.querySelector('#overlay').classList.add('open')">Open</button>
446+
<div id="paused"></div>
447+
<div id="overlay"></div>
448+
`);
449+
await page.getByRole("button", { name: "Open" }).click();
450+
const frame = await ServerBrowserPage.captureFinalRecordingFrame(
451+
await context.newCDPSession(page),
452+
80,
453+
);
454+
expect(frame).toBeDefined();
455+
const pixel = (await page.evaluate(`(async () => {
456+
const bytes = Uint8Array.from(atob(${JSON.stringify(frame)}), char => char.charCodeAt(0));
457+
const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/jpeg" }));
458+
const canvas = document.createElement("canvas");
459+
canvas.width = bitmap.width;
460+
canvas.height = bitmap.height;
461+
const context = canvas.getContext("2d");
462+
context.drawImage(bitmap, 0, 0);
463+
return Array.from(context.getImageData(bitmap.width / 2, bitmap.height / 2, 1, 1).data);
464+
})()`)) as [number, number, number, number];
465+
expect(colorOf(pixel)).toBe("red");
466+
});
467+
433468
it("shrinks the screenshot of an XML document, whose createElement makes no canvas", async () => {
434469
const page = await context.newPage();
435470
await page.goto("data:text/xml,<a/>");

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,22 @@ export const captureViewport = async (
478478
};
479479

480480
export const captureFinalRecordingFrame = (cdp: CDPSession, quality: number) =>
481-
answered(captureViewport(cdp, { format: "jpeg", quality }));
481+
answered(
482+
cdp
483+
.send("Runtime.evaluate", {
484+
// Capture the destination of finite transitions, not their initially transparent frame.
485+
expression: `(async () => {
486+
const paint = () => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
487+
await paint();
488+
await Promise.allSettled(document.getAnimations()
489+
.filter(animation => animation.playState === "running" && animation.effect?.getComputedTiming().iterations !== Infinity)
490+
.map(animation => animation.finished));
491+
await paint();
492+
})()`,
493+
awaitPromise: true,
494+
})
495+
.then(() => captureViewport(cdp, { format: "jpeg", quality })),
496+
);
482497

483498
/** Runs in the page's own isolated world, as the server has no image library. */
484499
const SHRINK_PNG = `async function (png, width, height) {

0 commit comments

Comments
 (0)