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.

activate pages before visual actions

Aute5cc5d5b43566
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/page-labels.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/page-labels.mjs
index 68eee70..9baccb7 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/page-labels.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/page-labels.mjs
@@ -234,8 +234,8 @@ export function pageBasicOperationsCase() {
assert((await stat(screenshotPath)).size > 0, "page.screenshot writes a non-empty PNG");
assertEqual(
(await currentTab()).targetId,
- second.targetId,
- "target-scoped reads and screenshot do not activate another page"
+ first.targetId,
+ "page.screenshot activates the page it captures"
);
await first.close();
@@ -265,7 +265,7 @@ export function pageActionsAndPopupCase() {
assertEqual((await currentTab()).targetId, budgetFiller.targetId, "the budget page starts active");
assertEqual(await source.fill("#text-input", "page-filled").then(() => source.evaluate("document.querySelector('#text-input').value")), "page-filled", "page.fill writes into the addressed page");
- assertEqual((await currentTab()).targetId, budgetFiller.targetId, "page.fill does not activate its page");
+ assertEqual((await currentTab()).targetId, source.targetId, "page.fill activates and keeps its page current");
await source.evaluate((popupUrl) => {
const link = document.createElement("a");
@@ -273,11 +273,21 @@ export function pageActionsAndPopupCase() {
link.href = popupUrl;
link.target = "_blank";
link.textContent = "Open managed popup";
+ link.style.cssText = "position:fixed;left:16px;top:16px;z-index:2147483647";
+ window.__pagePopupClickTrusted = null;
+ link.addEventListener("click", (event) => {
+ window.__pagePopupClickTrusted = event.isTrusted;
+ });
document.body.append(link);
}, baseUrl + "/secondary?page-actions=popup");
const receipt = await source.click("#page-popup-link");
assertEqual(receipt.popups.length, 1, "page.click reports the popup it opened");
+ assertEqual(
+ await source.evaluate("window.__pagePopupClickTrusted"),
+ true,
+ "page.click reaches the site as a trusted browser event"
+ );
const popup = receipt.popups[0];
assertEqual(typeof popup.label, "string", "the popup receives a durable label");
assertIncludes(
diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjs
index 4b9561d..5366cdf 100644
--- a/package/ego-browser/src/page-model.test.mjs
+++ b/package/ego-browser/src/page-model.test.mjs
@@ -252,7 +252,7 @@ test("snapshot activates the addressed page, not whichever tab was current", asy
});
});
-test("basic Page reads, evaluate, and screenshot stay on the addressed target", async () => {
+test("basic Page reads stay target-scoped and screenshot activates its Page", async () => {
await withFixture(async (fixture) => {
const task = taskForRound(fixture, "round-a");
const first = await task.newPage("https://example.test/first");
@@ -275,14 +275,19 @@ test("basic Page reads, evaluate, and screenshot stay on the addressed target",
await first.evaluate((value) => value, { source: "first" }),
{ source: "first" },
);
+ assert.equal(
+ fixture.activeTarget(),
+ "target-2",
+ "pure reads must not activate the addressed page",
+ );
assert.equal(
await first.screenshot("/tmp/first.png", { full: true }),
"/tmp/first.png",
);
assert.equal(
fixture.activeTarget(),
- "target-2",
- "target-scoped reads must not activate the addressed page",
+ "target-1",
+ "screenshot must activate the page before visual capture",
);
const pageCalls = fixture.calls.filter(
@@ -364,7 +369,11 @@ test("Page click stays target-scoped and adopts only tabs opened by the action",
sessionId === "session:target-1",
),
);
- assert.equal(fixture.activeTarget(), "target-2");
+ assert.equal(
+ fixture.activeTarget(),
+ "target-1",
+ "click must leave its Page active",
+ );
await assert.rejects(
() => task.newPage("https://example.test/blocked"),
/Page budget reached \(3\/2\)/,
@@ -388,7 +397,11 @@ test("Page fill uses its target session and reports no popup when none opened",
{ text: "filled" },
"session:target-1",
]);
- assert.equal(fixture.activeTarget(), "target-2");
+ assert.equal(
+ fixture.activeTarget(),
+ "target-1",
+ "fill must leave its Page active",
+ );
});
});
diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.ts
index b694544..1345239 100644
--- a/package/ego-browser/src/page-model.ts
+++ b/package/ego-browser/src/page-model.ts
@@ -458,10 +458,7 @@ export class Page {
async snapshot(options: Record<string, unknown> = {}): Promise<string> {
const page = await this.#resolve();
return this.#services.gate.withPage(page, async () => {
- await this.#services.cdp("Target.activateTarget", {
- targetId: page.targetId,
- });
- this.#services.setPreferredTarget(page.targetId);
+ await this.#activate(page.targetId);
const result = await this.#services.snapshot({
scope: "full_page",
includeActionMarks: true,
@@ -510,9 +507,10 @@ export class Page {
throw new TypeError("page.screenshot path must be a non-empty string");
}
const page = await this.#resolve();
- return this.#services.gate.withPage(page, ({ sessionId }) =>
- this.#services.screenshot(path, options, sessionId),
- );
+ return this.#services.gate.withPage(page, async ({ sessionId }) => {
+ await this.#activate(page.targetId);
+ return this.#services.screenshot(path, options, sessionId);
+ });
}
async click(
@@ -594,6 +592,7 @@ export class Page {
): Promise<PageActionReceipt> {
const page = await this.#resolve();
return this.#services.gate.withPage(page, async ({ sessionId }) => {
+ await this.#activate(page.targetId);
const before = new Set(
(await this.#services.listTabs()).map((tab) => tab.targetId),
);
@@ -630,6 +629,11 @@ export class Page {
return popups.length > 0 ? { popups } : {};
});
}
+
+ async #activate(targetId: string): Promise<void> {
+ await this.#services.cdp("Target.activateTarget", { targetId });
+ this.#services.setPreferredTarget(targetId);
+ }
}
async function evaluateInSession<T>(