Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

test(ego-browser): cover iframe snapshots and stable Page refs

JackWanga23768efd0ea
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/index.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/index.mjs
index 52c3b97..7c78c96 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/index.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/index.mjs
@@ -15,8 +15,20 @@ import { pageJavaScriptDialogHandlingCase } from "./page-dialogs.mjs";
import { pageMediaPlaybackCase } from "./page-media-playback.mjs";
import { pageScrolledScreenshotCase } from "./page-screenshot.mjs";
import { pageSnapshotLocatorCase } from "./page-snapshot-locators.mjs";
-import { pageSnapshotSubtreeCase } from "./page-snapshot-subtree.mjs";
+import {
+ pageSnapshotIframeViewportCase,
+ pageSnapshotLazyIframeCase,
+ pageSnapshotNestedIframeCase,
+ pageSnapshotReplacedIframeCase,
+ pageSnapshotSiblingIframeCase,
+ pageSnapshotSubtreeCase,
+} from "./page-snapshot-subtree.mjs";
import { pageShadowDomCase } from "./page-shadow-dom.mjs";
+import {
+ pageRefIdentityCase,
+ pageRefPrepareRoundCase,
+ pageRefResumeRoundCase,
+} from "./page-ref-identity.mjs";
import { pageTextLocatorCase } from "./page-text-locators.mjs";
import { pageSelectorCompatibilityCase } from "./page-selector-compatibility.mjs";
import { pagePopupWaiterCase } from "./page-popup-waiter.mjs";
@@ -104,7 +116,27 @@ export const e2eCases = [
{ name: "Page media playback", body: pageMediaPlaybackCase },
{ name: "Page scrolled screenshot", body: pageScrolledScreenshotCase },
{ name: "Page snapshot locator quality", body: pageSnapshotLocatorCase },
+ {
+ name: "Page snapshot iframe viewport",
+ body: pageSnapshotIframeViewportCase,
+ },
{ name: "Page snapshot subtree", body: pageSnapshotSubtreeCase },
+ {
+ name: "Page ref identity across partial snapshots",
+ body: pageRefIdentityCase,
+ },
+ { name: "Page refs across rounds: prepare", body: pageRefPrepareRoundCase },
+ { name: "Page refs across rounds: resume", body: pageRefResumeRoundCase },
+ { name: "Page snapshot lazy iframe", body: pageSnapshotLazyIframeCase },
+ {
+ name: "Page snapshot sibling iframes",
+ body: pageSnapshotSiblingIframeCase,
+ },
+ { name: "Page snapshot nested iframes", body: pageSnapshotNestedIframeCase },
+ {
+ name: "Page snapshot replaced iframe",
+ body: pageSnapshotReplacedIframeCase,
+ },
{ name: "Page open Shadow DOM locators", body: pageShadowDomCase },
{ name: "Page text locators", body: pageTextLocatorCase },
{
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/page-ref-identity.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/page-ref-identity.mjs
new file mode 100644
index 0000000..1460fd6
--- /dev/null
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/page-ref-identity.mjs
@@ -0,0 +1,76 @@
+export function pageRefIdentityCase() {
+ return String.raw`
+ const task = await taskSpace(taskName);
+ for (const scope of ['subtree', 'only_within_viewport']) {
+ const page = await newPageAt(task, baseUrl + '/nav-target');
+ try {
+ await page.evaluate((offscreen) => {
+ document.body.innerHTML = '<button id="first" aria-label="First ref action">First ref action</button><button id="second" aria-label="Second ref action">Second ref action</button>';
+ window.__refClicks = { first: 0, second: 0 };
+ for (const id of ['first', 'second']) {
+ document.getElementById(id).onclick = () => window.__refClicks[id]++;
+ }
+ if (offscreen) {
+ document.body.style.height = '2000px';
+ document.getElementById('first').style.cssText = 'position:absolute;top:1500px';
+ }
+ }, scope === 'only_within_viewport');
+ const full = await page.snapshot({ scope: 'full_page' });
+ const refFor = (snapshot, name) => {
+ const line = snapshot.split('\n').find((line) => line.includes(name));
+ const match = line && line.match(/\[ref=(\d+)/);
+ assert(match, 'snapshot exposes ' + name + ': ' + snapshot);
+ return '@' + match[1];
+ };
+ const firstRef = refFor(full, 'First ref action');
+ const secondRef = refFor(full, 'Second ref action');
+ const partial = await page.snapshot({ scope, ...(scope === 'subtree' ? { root: secondRef } : {}) });
+ assertEqual(refFor(partial, 'Second ref action'), secondRef, scope + ' preserves the second button ref');
+ assert(!partial.includes('First ref action'), scope + ' omits the first button');
+ await page.click(firstRef);
+ const clicks = await page.evaluate(() => window.__refClicks);
+ assertEqual(clicks.first, 1, scope + ' keeps the omitted first ref bound to the first button');
+ assertEqual(clicks.second, 0, scope + ' never redirects that ref to the second button');
+ } finally {
+ await page.close();
+ }
+ }
+ `;
+}
+
+export function pageRefPrepareRoundCase() {
+ return String.raw`
+ const task = await taskSpace(taskName);
+ const page = await newPageAt(task, baseUrl + '/nav-target');
+ await page.evaluate(() => {
+ document.body.innerHTML = '<button id="first" aria-label="First round action">First round action</button><button id="second" aria-label="Second round action">Second round action</button>';
+ window.__refClicks = { first: 0, second: 0 };
+ for (const id of ['first', 'second']) document.getElementById(id).onclick = () => window.__refClicks[id]++;
+ });
+ const initial = await page.snapshot();
+ const line = initial.split('\n').find((line) => line.includes('Second round action'));
+ const match = line && line.match(/\[ref=(\d+)/);
+ assert(match, 'initial snapshot publishes the second button ref');
+ const subtree = await page.snapshot({ scope: 'subtree', root: '@' + match[1] });
+ const published = subtree.split('\n').find((line) => line.includes('Second round action')).match(/\[ref=(\d+)/);
+ assert(published, 'the final snapshot publishes a ref for the second button');
+ await writeFile(join(tempDir, 'page-ref-round.json'), JSON.stringify({ label: page.label, ref: '@' + published[1] }));
+ `;
+}
+
+export function pageRefResumeRoundCase() {
+ return String.raw`
+ const saved = JSON.parse(await readFile(join(tempDir, 'page-ref-round.json'), 'utf8'));
+ const task = await taskSpace(taskName);
+ const page = task.page(saved.label);
+ try {
+ await page.click(saved.ref);
+ const clicks = await page.evaluate(() => window.__refClicks);
+ assertEqual(clicks.second, 1, 'the published subtree ref still clicks the second button in a new round');
+ assertEqual(clicks.first, 0, 'restoring a ref never renumbers it to the first button');
+ await assertRejects(() => page.click(saved.ref), 'Stale ref', 'actions invalidate saved refs');
+ } finally {
+ await page.close();
+ }
+ `;
+}
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/page-snapshot-subtree.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/page-snapshot-subtree.mjs
index 3287cb4..dba739b 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/page-snapshot-subtree.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/page-snapshot-subtree.mjs
@@ -60,83 +60,86 @@ export function pageSnapshotSubtreeCase() {
await page.close();
- async function assertDeferredIframeSubtree(mode, frameLabel) {
+ async function assertIframeSubtree(mode, frameLabel) {
const framePage = await newPageAt(
task,
baseUrl + "/snapshot-subtree-frame-host?mode=" + mode
);
- const actionName = "Run " + frameLabel + " subtree action";
- const frameMarker = frameLabel + " subtree content";
+ try {
+ const actionName = "Run " + frameLabel + " subtree action";
+ const frameMarker = frameLabel + " subtree content";
- await framePage.waitForSelector(
- 'loc=role:button[name="' + actionName + '"]',
- { state: "visible" }
- );
+ await framePage.waitForSelector(
+ 'loc=role:button[name="' + actionName + '"]',
+ { state: "visible" }
+ );
- const initial = await framePage.snapshot();
- assertIncludes(
- initial,
- "Snapshot iframe host",
- frameLabel + " host is visible in the default snapshot"
- );
- assert(
- !initial.includes(frameMarker),
- frameLabel + " contents are deferred in the default snapshot"
- );
- const iframeLine = initial
- .split("\\n")
- .find((line) => line.trimStart().startsWith("iframe "));
- const iframeMatch = iframeLine && iframeLine.match(/\\[ref=([0-9]+)/);
- assert(iframeMatch, frameLabel + " iframe exposes a subtree root ref");
- const iframeRoot = "@" + iframeMatch[1];
+ const initial = await framePage.snapshot();
+ assertIncludes(
+ initial,
+ "Snapshot iframe host",
+ frameLabel + " host is visible in the default snapshot"
+ );
+ assertIncludes(
+ initial,
+ frameMarker,
+ frameLabel + " visible contents appear in the default snapshot"
+ );
+ const iframeLine = initial
+ .split("\\n")
+ .find((line) => line.trimStart().startsWith("iframe "));
+ const iframeMatch = iframeLine && iframeLine.match(/\\[ref=([0-9]+)/);
+ assert(iframeMatch, frameLabel + " iframe exposes a subtree root ref");
+ const iframeRoot = "@" + iframeMatch[1];
- const frameSubtree = await framePage.snapshot({
- scope: "subtree",
- root: iframeRoot,
- });
- assert(
- frameSubtree
+ const frameSubtree = await framePage.snapshot({
+ scope: "subtree",
+ root: iframeRoot,
+ });
+ assert(
+ frameSubtree
+ .split("\\n")
+ .some((line) => line.trimStart().startsWith("iframe ")),
+ frameLabel + " subtree retains the iframe root: " + frameSubtree
+ );
+ assertIncludes(
+ frameSubtree,
+ frameMarker,
+ frameLabel + " subtree includes the frame contents"
+ );
+ assert(
+ !frameSubtree.includes("Host sibling marker"),
+ frameLabel + " subtree excludes host siblings"
+ );
+ const actionLine = frameSubtree
.split("\\n")
- .some((line) => line.trimStart().startsWith("iframe ")),
- frameLabel + " subtree retains the iframe root: " + frameSubtree
- );
- assertIncludes(
- frameSubtree,
- frameMarker,
- frameLabel + " subtree includes the deferred frame contents"
- );
- assert(
- !frameSubtree.includes("Host sibling marker"),
- frameLabel + " subtree excludes host siblings"
- );
- const actionLine = frameSubtree
- .split("\\n")
- .find((line) => line.includes(actionName));
- const actionMatch = actionLine && actionLine.match(/\\[ref=([0-9]+)/);
- assert(actionLine, frameLabel + " subtree includes its action");
- if (mode === "same-origin") {
+ .find((line) => line.includes(actionName));
+ const actionMatch = actionLine && actionLine.match(/\\[ref=([0-9]+)/);
+ assert(actionLine, frameLabel + " subtree includes its action");
assert(
actionMatch,
frameLabel + " subtree returns an actionable ref: " + frameSubtree
);
- }
- await framePage.click(
- actionMatch
- ? "@" + actionMatch[1]
- : 'loc=role:button[name="' + actionName + '"]'
- );
- assertIncludes(
- await framePage.snapshot({ scope: "full_page" }),
- frameLabel + " clicked",
- frameLabel + " expanded subtree remains actionable"
- );
- await framePage.close();
+ await framePage.click("@" + actionMatch[1]);
+ assertIncludes(
+ await framePage.snapshot({ scope: "full_page" }),
+ frameLabel + " clicked",
+ frameLabel + " expanded subtree remains actionable"
+ );
+ } finally {
+ await framePage.close();
+ }
}
- await assertDeferredIframeSubtree("same-origin", "Same-origin iframe");
- await assertDeferredIframeSubtree("cross-origin", "Cross-origin OOPIF");
+ await assertIframeSubtree("same-origin", "Same-origin iframe");
+ await assertIframeSubtree("cross-origin", "Cross-origin OOPIF");
+ `;
+}
+export function pageSnapshotLazyIframeCase() {
+ return `
+ const task = await taskSpace(taskName);
const lazyRun = "lazy-" + Date.now();
const lazyPage = await newPageAt(
task,
@@ -144,258 +147,411 @@ export function pageSnapshotSubtreeCase() {
"/snapshot-subtree-frame-host?mode=same-origin&lazy=true&run=" +
lazyRun
);
- const lazyInitial = await lazyPage.snapshot();
- assert(
- !lazyInitial.includes("Deferred snapshot subtree frame"),
- "an offscreen native lazy iframe is absent from the initial viewport snapshot"
- );
- assertEqual(
- (await (await fetch(
- baseUrl + "/snapshot-subtree-frame-state?run=" + lazyRun
- )).json()).requests,
- 0,
- "the offscreen loading=lazy iframe has not been requested"
- );
- await lazyPage.evaluate(
- "document.querySelector('#snapshot-subtree-frame').scrollIntoView()"
- );
- await lazyPage.waitForSelector(
- 'loc=role:button[name="Run Same-origin iframe subtree action"]',
- { state: "visible" }
- );
- assertEqual(
- (await (await fetch(
- baseUrl + "/snapshot-subtree-frame-state?run=" + lazyRun
- )).json()).requests,
- 1,
- "scrolling the lazy iframe into view loads it once"
- );
- const lazyVisible = await lazyPage.snapshot();
- assert(
- !lazyVisible.includes("Same-origin iframe subtree content"),
- "the loaded lazy iframe remains collapsed in a viewport snapshot"
- );
- const lazyIframeLine = lazyVisible
- .split("\\n")
- .find((line) => line.trimStart().startsWith("iframe "));
- const lazyIframeMatch =
- lazyIframeLine && lazyIframeLine.match(/\\[ref=([0-9]+)/);
- assert(lazyIframeMatch, "the visible lazy iframe exposes a subtree root ref");
- assertIncludes(
- await lazyPage.snapshot({
- scope: "subtree",
- root: "@" + lazyIframeMatch[1],
- }),
- "Same-origin iframe subtree content",
- "the lazy iframe subtree expands after loading"
- );
- await lazyPage.close();
+ try {
+ const hasIframe = (snapshot) => snapshot
+ .split("\\n")
+ .some((line) => /^\\s*iframe(?:\\s|$)/.test(line));
+ const lazyInitial = await lazyPage.snapshot();
+ assert(
+ !hasIframe(lazyInitial),
+ "an offscreen native lazy iframe is absent from the initial viewport snapshot: " + lazyInitial
+ );
+ assertEqual(
+ (await (await fetch(
+ baseUrl + "/snapshot-subtree-frame-state?run=" + lazyRun
+ )).json()).requests,
+ 0,
+ "the offscreen loading=lazy iframe has not been requested"
+ );
+ await lazyPage.evaluate(
+ "document.querySelector('#snapshot-subtree-frame').scrollIntoView()"
+ );
+ await lazyPage.waitForSelector(
+ 'loc=role:button[name="Run Same-origin iframe subtree action"]',
+ { state: "visible" }
+ );
+ assertEqual(
+ (await (await fetch(
+ baseUrl + "/snapshot-subtree-frame-state?run=" + lazyRun
+ )).json()).requests,
+ 1,
+ "scrolling the lazy iframe into view loads it once"
+ );
+ const lazyVisible = await lazyPage.snapshot();
+ assertIncludes(
+ lazyVisible,
+ "Same-origin iframe subtree content",
+ "the loaded lazy iframe appears in a viewport snapshot"
+ );
+ const lazyIframeLine = lazyVisible
+ .split("\\n")
+ .find((line) => line.trimStart().startsWith("iframe "));
+ const lazyIframeMatch =
+ lazyIframeLine && lazyIframeLine.match(/\\[ref=([0-9]+)/);
+ assert(lazyIframeMatch, "the visible lazy iframe exposes a subtree root ref");
+ assertIncludes(
+ await lazyPage.snapshot({
+ scope: "subtree",
+ root: "@" + lazyIframeMatch[1],
+ }),
+ "Same-origin iframe subtree content",
+ "the lazy iframe subtree expands after loading"
+ );
+ await lazyPage.evaluate("window.scrollTo(0, 0)");
+ const loadedOffscreen = await lazyPage.snapshot();
+ assert(
+ !hasIframe(loadedOffscreen) &&
+ !loadedOffscreen.includes("Same-origin iframe subtree content"),
+ "a loaded iframe and its contents disappear after scrolling out of the viewport: " + loadedOffscreen
+ );
+ assertIncludes(
+ await lazyPage.snapshot({ scope: "full_page" }),
+ "Same-origin iframe subtree content",
+ "the offscreen frame remains loaded and observable in a full-page snapshot"
+ );
+ assertEqual(
+ (await (await fetch(
+ baseUrl + "/snapshot-subtree-frame-state?run=" + lazyRun
+ )).json()).requests,
+ 1,
+ "observing the offscreen frame does not reload it"
+ );
+ await lazyPage.evaluate(
+ "document.querySelector('#snapshot-subtree-frame').scrollIntoView()"
+ );
+ assertIncludes(
+ await lazyPage.snapshot(),
+ "Same-origin iframe subtree content",
+ "the loaded iframe reappears when scrolled back into the viewport"
+ );
+ } finally {
+ await lazyPage.close();
+ }
+ `;
+}
+
+export function pageSnapshotSiblingIframeCase() {
+ return `
+ const task = await taskSpace(taskName);
const siblingPage = await newPageAt(
task,
baseUrl +
"/snapshot-subtree-frame-host?mode=same-origin&layout=siblings"
);
- await siblingPage.waitForSelector(
- 'loc=role:button[name="Run First sibling iframe subtree action"]',
- { state: "visible" }
- );
- await siblingPage.waitForSelector(
- 'loc=role:button[name="Run Second sibling iframe subtree action"]',
- { state: "visible" }
- );
- const siblingInitial = await siblingPage.snapshot();
- const siblingRoots = siblingInitial
- .split("\\n")
- .filter((line) => line.trimStart().startsWith("iframe "))
- .map((line) => line.match(/\\[ref=([0-9]+)/))
- .filter(Boolean)
- .map((match) => "@" + match[1]);
- assertEqual(siblingRoots.length, 2, "both sibling iframe roots are visible");
- assert(
- !siblingInitial.includes("First sibling iframe subtree content") &&
- !siblingInitial.includes("Second sibling iframe subtree content"),
- "the default snapshot collapses both sibling iframe subtrees"
- );
- const firstSibling = await siblingPage.snapshot({
- scope: "subtree",
- root: siblingRoots[0],
- });
- assertIncludes(
- firstSibling,
- "First sibling iframe subtree content",
- "the first sibling root expands only its own frame"
- );
- assert(
- !firstSibling.includes("Second sibling iframe subtree content"),
- "the first sibling subtree excludes the second sibling"
- );
- // No full snapshot in between: a subtree snapshot must keep the other
- // iframe root from the earlier viewport snapshot usable.
- const secondSibling = await siblingPage.snapshot({
- scope: "subtree",
- root: siblingRoots[1],
- });
- assertIncludes(
- secondSibling,
- "Second sibling iframe subtree content",
- "the second sibling root expands only its own frame"
- );
- assert(
- !secondSibling.includes("First sibling iframe subtree content"),
- "the second sibling subtree excludes the first sibling"
- );
- const firstActionLine = firstSibling
- .split("\\n")
- .find((line) =>
- line.includes("Run First sibling iframe subtree action")
- );
- const firstActionMatch =
- firstActionLine && firstActionLine.match(/\\[ref=([0-9]+)/);
- assert(firstActionMatch, "the first sibling action has a ref");
- await siblingPage.click("@" + firstActionMatch[1]);
- const siblingAfterClick = await siblingPage.snapshot();
- const siblingRootsAfterClick = siblingAfterClick
- .split("\\n")
- .filter((line) => line.trimStart().startsWith("iframe "))
- .map((line) => line.match(/\\[ref=([0-9]+)/))
- .filter(Boolean)
- .map((match) => "@" + match[1]);
- assertEqual(
- siblingRootsAfterClick.length,
- 2,
- "both sibling iframe roots stay visible after the click"
- );
- assertIncludes(
- await siblingPage.snapshot({
+ try {
+ await siblingPage.waitForSelector(
+ 'loc=role:button[name="Run First sibling iframe subtree action"]',
+ { state: "visible" }
+ );
+ await siblingPage.waitForSelector(
+ 'loc=role:button[name="Run Second sibling iframe subtree action"]',
+ { state: "visible" }
+ );
+ const siblingInitial = await siblingPage.snapshot();
+ const siblingRoots = siblingInitial
+ .split("\\n")
+ .filter((line) => line.trimStart().startsWith("iframe "))
+ .map((line) => line.match(/\\[ref=([0-9]+)/))
+ .filter(Boolean)
+ .map((match) => "@" + match[1]);
+ assertEqual(siblingRoots.length, 2, "both sibling iframe roots are visible");
+ assert(
+ siblingInitial.includes("First sibling iframe subtree content") &&
+ siblingInitial.includes("Second sibling iframe subtree content"),
+ "the default snapshot includes both visible sibling iframe subtrees"
+ );
+ const firstSibling = await siblingPage.snapshot({
scope: "subtree",
- root: siblingRootsAfterClick[0],
- }),
- "First sibling iframe clicked",
- "the first sibling ref acts in the correct frame"
- );
- assertIncludes(
- await siblingPage.snapshot({
+ root: siblingRoots[0],
+ });
+ assertIncludes(
+ firstSibling,
+ "First sibling iframe subtree content",
+ "the first sibling root expands only its own frame"
+ );
+ assert(
+ !firstSibling.includes("Second sibling iframe subtree content"),
+ "the first sibling subtree excludes the second sibling"
+ );
+ // No full snapshot in between: a subtree snapshot must keep the other
+ // iframe root from the earlier viewport snapshot usable.
+ const secondSibling = await siblingPage.snapshot({
scope: "subtree",
- root: siblingRootsAfterClick[1],
- }),
- "Second sibling iframe idle",
- "the second sibling frame remains unchanged"
- );
- await siblingPage.close();
+ root: siblingRoots[1],
+ });
+ assertIncludes(
+ secondSibling,
+ "Second sibling iframe subtree content",
+ "the second sibling root expands only its own frame"
+ );
+ assert(
+ !secondSibling.includes("First sibling iframe subtree content"),
+ "the second sibling subtree excludes the first sibling"
+ );
+ const firstActionLine = firstSibling
+ .split("\\n")
+ .find((line) =>
+ line.includes("Run First sibling iframe subtree action")
+ );
+ const firstActionMatch =
+ firstActionLine && firstActionLine.match(/\\[ref=([0-9]+)/);
+ assert(firstActionMatch, "the first sibling action has a ref");
+ await siblingPage.click("@" + firstActionMatch[1]);
+ const siblingAfterClick = await siblingPage.snapshot();
+ const siblingRootsAfterClick = siblingAfterClick
+ .split("\\n")
+ .filter((line) => line.trimStart().startsWith("iframe "))
+ .map((line) => line.match(/\\[ref=([0-9]+)/))
+ .filter(Boolean)
+ .map((match) => "@" + match[1]);
+ assertEqual(
+ siblingRootsAfterClick.length,
+ 2,
+ "both sibling iframe roots stay visible after the click"
+ );
+ assertIncludes(
+ await siblingPage.snapshot({
+ scope: "subtree",
+ root: siblingRootsAfterClick[0],
+ }),
+ "First sibling iframe clicked",
+ "the first sibling ref acts in the correct frame"
+ );
+ assertIncludes(
+ await siblingPage.snapshot({
+ scope: "subtree",
+ root: siblingRootsAfterClick[1],
+ }),
+ "Second sibling iframe idle",
+ "the second sibling frame remains unchanged"
+ );
+ } finally {
+ await siblingPage.close();
+ }
+ `;
+}
+export function pageSnapshotNestedIframeCase() {
+ return `
+ const task = await taskSpace(taskName);
const nestedPage = await newPageAt(
task,
baseUrl + "/snapshot-subtree-frame-host?mode=same-origin&layout=nested"
);
- await nestedPage.waitForSelector(
- 'loc=role:button[name="Run Nested inner iframe subtree action"]',
- { state: "visible" }
- );
- const nestedInitial = await nestedPage.snapshot();
- assert(
- !nestedInitial.includes("Nested outer iframe subtree content") &&
- !nestedInitial.includes("Nested inner iframe subtree content"),
- "the default snapshot collapses the complete nested iframe tree"
- );
- const outerLine = nestedInitial
- .split("\\n")
- .find((line) => line.trimStart().startsWith("iframe "));
- const outerMatch = outerLine && outerLine.match(/\\[ref=([0-9]+)/);
- assert(outerMatch, "the outer iframe exposes a subtree root ref");
- const outerSubtree = await nestedPage.snapshot({
- scope: "subtree",
- root: "@" + outerMatch[1],
- });
- assertIncludes(
- outerSubtree,
- "Nested outer iframe subtree content",
- "the outer subtree includes its own content"
- );
- assertIncludes(
- outerSubtree,
- "Nested inner iframe subtree content",
- "the outer subtree includes its nested iframe content"
- );
- const nestedIframeLines = outerSubtree
- .split("\\n")
- .filter((line) => line.trimStart().startsWith("iframe "));
- const innerMatch =
- nestedIframeLines[1] && nestedIframeLines[1].match(/\\[ref=([0-9]+)/);
- assert(innerMatch, "the expanded outer subtree exposes the inner iframe ref");
- const innerSubtree = await nestedPage.snapshot({
- scope: "subtree",
- root: "@" + innerMatch[1],
- });
- assertIncludes(
- innerSubtree,
- "Nested inner iframe subtree content",
- "the nested iframe root expands its own content"
- );
- assert(
- !innerSubtree.includes("Nested outer iframe subtree content"),
- "the nested iframe subtree excludes its outer frame ancestor"
- );
- await nestedPage.close();
+ try {
+ await nestedPage.waitForSelector(
+ 'loc=role:button[name="Run Nested inner iframe subtree action"]',
+ { state: "visible" }
+ );
+ const nestedInitial = await nestedPage.snapshot();
+ assertIncludes(
+ nestedInitial,
+ "Nested outer iframe subtree content",
+ "the default snapshot includes the visible outer iframe content"
+ );
+ assertIncludes(
+ nestedInitial,
+ "Nested inner iframe subtree content",
+ "the default snapshot includes the visible inner iframe content"
+ );
+ const innerActionLine = nestedInitial
+ .split("\\n")
+ .find((line) => line.includes("Run Nested inner iframe subtree action"));
+ const innerActionMatch =
+ innerActionLine && innerActionLine.match(/\\[ref=([0-9]+)/);
+ assert(
+ innerActionMatch,
+ "the default snapshot exposes an inner iframe action ref"
+ );
+ await nestedPage.click("@" + innerActionMatch[1]);
+ const afterInnerAction = await nestedPage.snapshot();
+ assertIncludes(
+ afterInnerAction,
+ "Nested inner iframe clicked",
+ "the inner viewport ref acts in its own frame"
+ );
+ assertIncludes(
+ afterInnerAction,
+ "Nested outer iframe idle",
+ "the inner viewport ref leaves the outer frame unchanged"
+ );
+ const outerLine = afterInnerAction
+ .split("\\n")
+ .find((line) => line.trimStart().startsWith("iframe "));
+ const outerMatch = outerLine && outerLine.match(/\\[ref=([0-9]+)/);
+ assert(outerMatch, "the outer iframe exposes a subtree root ref");
+ const outerSubtree = await nestedPage.snapshot({
+ scope: "subtree",
+ root: "@" + outerMatch[1],
+ });
+ assertIncludes(
+ outerSubtree,
+ "Nested outer iframe subtree content",
+ "the outer subtree includes its own content"
+ );
+ assertIncludes(
+ outerSubtree,
+ "Nested inner iframe subtree content",
+ "the outer subtree includes its nested iframe content"
+ );
+ const nestedIframeLines = outerSubtree
+ .split("\\n")
+ .filter((line) => line.trimStart().startsWith("iframe "));
+ const innerMatch =
+ nestedIframeLines[1] && nestedIframeLines[1].match(/\\[ref=([0-9]+)/);
+ assert(innerMatch, "the expanded outer subtree exposes the inner iframe ref");
+ const innerSubtree = await nestedPage.snapshot({
+ scope: "subtree",
+ root: "@" + innerMatch[1],
+ });
+ assertIncludes(
+ innerSubtree,
+ "Nested inner iframe subtree content",
+ "the nested iframe root expands its own content"
+ );
+ assert(
+ !innerSubtree.includes("Nested outer iframe subtree content"),
+ "the nested iframe subtree excludes its outer frame ancestor"
+ );
+ } finally {
+ await nestedPage.close();
+ }
+ `;
+}
+export function pageSnapshotReplacedIframeCase() {
+ return `
+ const task = await taskSpace(taskName);
const replacedPage = await newPageAt(
task,
baseUrl + "/snapshot-subtree-frame-host?mode=same-origin"
);
- await replacedPage.waitForSelector(
- 'loc=role:button[name="Run Same-origin iframe subtree action"]',
- { state: "visible" }
- );
- const beforeReplacement = await replacedPage.snapshot();
- const oldFrameLine = beforeReplacement
- .split("\\n")
- .find((line) => line.trimStart().startsWith("iframe "));
- const oldFrameMatch =
- oldFrameLine && oldFrameLine.match(/\\[ref=([0-9]+)/);
- assert(oldFrameMatch, "the original iframe exposes a root ref");
- const originalSubtree = await replacedPage.snapshot({
- scope: "subtree",
- root: "@" + oldFrameMatch[1],
- });
- const oldActionLine = originalSubtree
- .split("\\n")
- .find((line) => line.includes("Run Same-origin iframe subtree action"));
- const oldActionMatch =
- oldActionLine && oldActionLine.match(/\\[ref=([0-9]+)/);
- assert(oldActionMatch, "the original iframe action exposes a ref");
- await replacedPage.evaluate(() => {
- const previous = document.querySelector("#snapshot-subtree-frame");
- const replacement = document.createElement("iframe");
- replacement.id = "snapshot-subtree-frame";
- replacement.title = "Replacement subtree frame";
- replacement.src =
- "/snapshot-subtree-frame-content?mode=same-origin&frame=replacement";
- previous.replaceWith(replacement);
- });
- await replacedPage.waitForSelector(
- 'loc=role:button[name="Run Replacement iframe subtree action"]',
- { state: "visible" }
- );
- await assertRejects(
- () => replacedPage.click("@" + oldActionMatch[1]),
- "Stale ref",
- "an iframe replacement invalidates its former descendant refs"
- );
- const afterReplacement = await replacedPage.snapshot();
- const replacementLine = afterReplacement
- .split("\\n")
- .find((line) => line.trimStart().startsWith("iframe "));
- const replacementMatch =
- replacementLine && replacementLine.match(/\\[ref=([0-9]+)/);
- assert(replacementMatch, "the replacement iframe exposes a fresh root ref");
- assertIncludes(
- await replacedPage.snapshot({
+ try {
+ await replacedPage.waitForSelector(
+ 'loc=role:button[name="Run Same-origin iframe subtree action"]',
+ { state: "visible" }
+ );
+ const beforeReplacement = await replacedPage.snapshot();
+ const oldFrameLine = beforeReplacement
+ .split("\\n")
+ .find((line) => line.trimStart().startsWith("iframe "));
+ const oldFrameMatch =
+ oldFrameLine && oldFrameLine.match(/\\[ref=([0-9]+)/);
+ assert(oldFrameMatch, "the original iframe exposes a root ref");
+ const originalSubtree = await replacedPage.snapshot({
scope: "subtree",
- root: "@" + replacementMatch[1],
- }),
- "Replacement iframe subtree content",
- "a fresh snapshot expands the replacement iframe"
- );
- await replacedPage.close();
+ root: "@" + oldFrameMatch[1],
+ });
+ const oldActionLine = originalSubtree
+ .split("\\n")
+ .find((line) => line.includes("Run Same-origin iframe subtree action"));
+ const oldActionMatch =
+ oldActionLine && oldActionLine.match(/\\[ref=([0-9]+)/);
+ assert(oldActionMatch, "the original iframe action exposes a ref");
+ await replacedPage.evaluate(() => {
+ const previous = document.querySelector("#snapshot-subtree-frame");
+ const replacement = document.createElement("iframe");
+ replacement.id = "snapshot-subtree-frame";
+ replacement.title = "Replacement subtree frame";
+ replacement.width = previous.width;
+ replacement.height = previous.height;
+ replacement.src =
+ "/snapshot-subtree-frame-content?mode=same-origin&frame=replacement";
+ previous.replaceWith(replacement);
+ });
+ await replacedPage.waitForSelector(
+ 'loc=role:button[name="Run Replacement iframe subtree action"]',
+ { state: "visible" }
+ );
+ await assertRejects(
+ () => replacedPage.click("@" + oldActionMatch[1]),
+ "Stale ref",
+ "an iframe replacement invalidates its former descendant refs"
+ );
+ const afterReplacement = await replacedPage.snapshot();
+ assertIncludes(
+ afterReplacement,
+ "Replacement iframe subtree content",
+ "the default snapshot includes the replacement iframe contents"
+ );
+ assert(
+ !afterReplacement.includes("Same-origin iframe subtree content"),
+ "the default snapshot excludes the detached iframe contents"
+ );
+ const replacementLine = afterReplacement
+ .split("\\n")
+ .find((line) => line.trimStart().startsWith("iframe "));
+ const replacementMatch =
+ replacementLine && replacementLine.match(/\\[ref=([0-9]+)/);
+ assert(replacementMatch, "the replacement iframe exposes a fresh root ref");
+ assertIncludes(
+ await replacedPage.snapshot({
+ scope: "subtree",
+ root: "@" + replacementMatch[1],
+ }),
+ "Replacement iframe subtree content",
+ "a fresh snapshot expands the replacement iframe"
+ );
+ } finally {
+ await replacedPage.close();
+ }
+ `;
+}
+
+export function pageSnapshotIframeViewportCase() {
+ return `
+ const task = await taskSpace(taskName);
+ for (const [mode, frameLabel] of [
+ ["same-origin", "Same-origin iframe"],
+ ["cross-origin", "Cross-origin OOPIF"],
+ ]) {
+ const page = await newPageAt(
+ task,
+ baseUrl + "/snapshot-subtree-frame-host?mode=" + mode
+ );
+ try {
+ const actionName = "Run " + frameLabel + " subtree action";
+ const actionLocator = 'loc=role:button[name="' + actionName + '"]';
+ await page.waitForSelector(actionLocator, { state: "visible" });
+
+ if (mode === "cross-origin") {
+ const targets = (await task.cdp("Target.getTargets")).targetInfos;
+ assert(
+ targets.some((target) =>
+ target.type === "iframe" &&
+ target.parentId === page.targetId &&
+ target.url.includes("/snapshot-subtree-frame-content")
+ ),
+ "the cross-origin fixture runs in an OOPIF target"
+ );
+ }
+ const initial = await page.snapshot();
+ assertIncludes(
+ initial,
+ frameLabel + " subtree content",
+ frameLabel + " visible contents appear in the default snapshot"
+ );
+ const actionLine = initial
+ .split("\\n")
+ .find((line) => line.includes(actionName));
+ const actionMatch = actionLine && actionLine.match(/\\[ref=([0-9]+)/);
+ assert(actionLine, frameLabel + " viewport includes its action: " + initial);
+ assert(
+ actionMatch,
+ frameLabel + " viewport returns an actionable ref: " + initial
+ );
+
+ await page.click("@" + actionMatch[1]);
+ assertIncludes(
+ await page.snapshot(),
+ frameLabel + " clicked",
+ frameLabel + " viewport action works without a full-page snapshot"
+ );
+ cliLog({ frame: frameLabel, verifiedAction: "snapshot ref" });
+ } finally {
+ await page.close();
+ }
+ }
`;
}
diff --git a/package/ego-browser/scripts/real-browser-e2e/fixture.mjs b/package/ego-browser/scripts/real-browser-e2e/fixture.mjs
index bc2d8f9..88981ea 100644
--- a/package/ego-browser/scripts/real-browser-e2e/fixture.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/fixture.mjs
@@ -246,6 +246,8 @@ export async function startFixtureServer(taskName) {
? `<iframe
id="nested-subtree-frame"
title="Nested subtree frame"
+ width="400"
+ height="200"
src="/snapshot-subtree-frame-content?mode=same-origin&frame=nested-inner"
></iframe>`
: "";
@@ -309,6 +311,8 @@ export async function startFixtureServer(taskName) {
: `<iframe
id="snapshot-subtree-frame"
title="Deferred snapshot subtree frame"
+ width="${layout === "nested" ? 500 : 400}"
+ height="${layout === "nested" ? 450 : 300}"
${lazy ? 'loading="lazy"' : ""}
src="${frameUrl(layout === "nested" ? "nested-outer" : "")}"
></iframe>`;