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.

fix(ego-browser): align waitForURL semantics

0x053d4871fcc5b
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 2cb1985..8a346a3 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/index.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/index.mjs
@@ -24,6 +24,7 @@ import { workflowCases } from "./workflows.mjs";
import { interactionsCases } from "./interactions.mjs";
import { canvasCases } from "./canvas.mjs";
import { downloadCases } from "./downloads.mjs";
+import { playwrightUrlWaitCases } from "./playwright-url-waits.mjs";
export const e2eCases = [
{ name: "environment initialization", body: environmentCase },
@@ -54,4 +55,5 @@ export const e2eCases = [
...downloadCases,
...interactionsCases,
...canvasCases,
+ ...playwrightUrlWaitCases,
];
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/playwright-url-waits.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/playwright-url-waits.mjs
new file mode 100644
index 0000000..35e6857
--- /dev/null
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/playwright-url-waits.mjs
@@ -0,0 +1,46 @@
+import { caseBody } from "./shared.mjs";
+
+export const playwrightUrlWaitCases = [
+ {
+ name: "regression PWB-01 waitForURL predicate URL",
+ body: caseBody(`
+ await page.goto(baseUrl + "/nav-target", { waitUntil: "load" });
+ let observed;
+ assert(
+ await page.waitForURL((url) => {
+ observed = {
+ isUrl: url instanceof URL,
+ href: url.href,
+ pathname: url.pathname,
+ };
+ return url.pathname === "/nav-target";
+ }, { timeout: 3000 }),
+ "PWB-01 waitForURL matches the current URL"
+ );
+ assert(observed?.isUrl, "PWB-01 predicate receives a URL object");
+ assertEqual(observed.pathname, "/nav-target", "PWB-01 URL pathname is available");
+ `),
+ },
+ {
+ name: "regression PWB-02 waitForURL default load",
+ body: caseBody(`
+ const target = baseUrl + "/regression/slow-load?ms=1200&run=" + Date.now();
+ await page.goto(target, { waitUntil: "commit", timeout: 5000 });
+ const startedAt = Date.now();
+ assert(
+ await page.waitForURL(
+ (url) => url.pathname === "/regression/slow-load",
+ { timeout: 5000 }
+ ),
+ "PWB-02 waitForURL reaches its default load state"
+ );
+ const elapsedMs = Date.now() - startedAt;
+ assertEqual(
+ await page.evaluate(() => document.readyState),
+ "complete",
+ "PWB-02 default waitForURL returns after document load"
+ );
+ assert(elapsedMs >= 700, "PWB-02 waitForURL did not return at URL commit");
+ `),
+ },
+];
diff --git a/package/ego-browser/scripts/real-browser-e2e/fixture.mjs b/package/ego-browser/scripts/real-browser-e2e/fixture.mjs
index 424a34b..3e2d7b2 100644
--- a/package/ego-browser/scripts/real-browser-e2e/fixture.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/fixture.mjs
@@ -106,6 +106,35 @@ export async function startFixtureServer(taskName) {
res.end(`status ${code}`);
return;
}
+ if (url.pathname === "/regression/delayed-asset.svg") {
+ const delayMs = boundedDelay(url.searchParams.get("ms"), 1200);
+ setTimeout(() => {
+ res.writeHead(200, {
+ "content-type": "image/svg+xml",
+ "cache-control": "no-store",
+ });
+ res.end(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"><rect width="4" height="4" fill="#2457ff"/></svg>',
+ );
+ }, delayMs);
+ return;
+ }
+ if (url.pathname === "/regression/slow-load") {
+ const delayMs = boundedDelay(url.searchParams.get("ms"), 1200);
+ res.writeHead(200, {
+ "content-type": "text/html",
+ "cache-control": "no-store",
+ });
+ res.end(`<!doctype html>
+<html>
+ <head><meta charset="utf-8"><title>slow load regression</title></head>
+ <body>
+ <h1>Slow load regression</h1>
+ <img alt="delayed asset" src="/regression/delayed-asset.svg?ms=${delayMs}&nonce=${Date.now()}">
+ </body>
+</html>`);
+ return;
+ }
if (url.pathname === "/api/bytes") {
const n = Math.max(0, Number(url.searchParams.get("n") || 0));
res.writeHead(200, {
@@ -178,6 +207,11 @@ export async function startFixtureServer(taskName) {
};
}
+function boundedDelay(raw, fallback) {
+ const value = Number(raw ?? fallback);
+ return Number.isFinite(value) ? Math.max(0, Math.min(value, 5000)) : fallback;
+}
+
function pageHtml(kind) {
const title =
kind === "nav-target"
diff --git a/package/ego-browser/src/driver/waits.test.mjs b/package/ego-browser/src/driver/waits.test.mjs
index 6655d34..9373c1d 100644
--- a/package/ego-browser/src/driver/waits.test.mjs
+++ b/package/ego-browser/src/driver/waits.test.mjs
@@ -103,13 +103,94 @@ test("waitForURL supports Playwright-style glob strings", async () => {
},
});
try {
- assert.equal(await waitForURL("**/target", { timeout: 500 }), true);
+ assert.equal(
+ await waitForURL("**/target", {
+ timeout: 500,
+ waitUntil: "commit",
+ }),
+ true,
+ );
} finally {
restore();
}
assert.deepEqual(sleeps, [100]);
});
+test("waitForURL predicates receive URL objects and wait for load by default", async () => {
+ const calls = [];
+ let observedUrl;
+ const restore = setOverrides({
+ cdpOverride: async (method, params) => {
+ calls.push({ method, params });
+ if (method === "Page.getFrameTree") {
+ return {
+ frameTree: {
+ frame: { url: "https://example.com/target" },
+ },
+ };
+ }
+ if (method === "Runtime.evaluate") {
+ if (params.expression === "location.href") {
+ return {
+ result: { value: "https://example.com/target" },
+ };
+ }
+ if (params.expression === "document.readyState") {
+ return { result: { value: "complete" } };
+ }
+ }
+ throw new Error(`unexpected CDP call: ${method}`);
+ },
+ });
+ try {
+ assert.equal(
+ await waitForURL((url) => {
+ observedUrl = url;
+ return url.pathname === "/target";
+ }),
+ true,
+ );
+ } finally {
+ restore();
+ }
+
+ assert(observedUrl instanceof URL);
+ assert.equal(observedUrl.href, "https://example.com/target");
+ assert.ok(
+ calls.some(
+ ({ method, params }) =>
+ method === "Runtime.evaluate" &&
+ params.expression === "document.readyState",
+ ),
+ "default waitForURL waits for the load state after the URL matches",
+ );
+});
+
+test("waitForURL waitUntil commit returns without waiting for load", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride: async (method, params) => {
+ calls.push({ method, params });
+ return { result: { value: "https://example.com/target" } };
+ },
+ });
+ try {
+ assert.equal(
+ await waitForURL("https://example.com/target", {
+ waitUntil: "commit",
+ }),
+ true,
+ );
+ } finally {
+ restore();
+ }
+
+ assert.equal(
+ calls.some(({ method }) => method === "Page.getFrameTree"),
+ false,
+ );
+});
+
test("waitForRequest matches exact URL and returns a request facade", async () => {
const calls = installAutoEgo();
try {
diff --git a/package/ego-browser/src/driver/waits.ts b/package/ego-browser/src/driver/waits.ts
index a4c9782..c588d8d 100644
--- a/package/ego-browser/src/driver/waits.ts
+++ b/package/ego-browser/src/driver/waits.ts
@@ -83,8 +83,9 @@ export async function waitForFunction(
/**
* Wait for the current page URL to match a string, glob, RegExp, or predicate.
- * @param {string|RegExp|Function} url URL matcher. Strings with * are treated as globs; other strings are exact.
+ * @param {string|RegExp|Function} url URL matcher. Predicate functions receive a URL object. Strings with * are treated as globs; other strings are exact.
* @param {{timeout?: number, waitUntil?: "load"|"domcontentloaded"|"networkidle"|"commit"}} [options]
+ * `waitUntil` defaults to `"load"`.
* @returns {Promise<boolean>} True when matched before timeout.
*/
export async function waitForURL(url, options: WaitForURLOptions = {}) {
@@ -100,12 +101,12 @@ export async function waitForURL(url, options: WaitForURLOptions = {}) {
"location.href",
);
if (urlMatches(current, url)) {
- if (options.waitUntil && options.waitUntil !== "commit") {
- return waitForLoadState(options.waitUntil, {
- timeout: Math.max(0, deadline - state.now()),
- });
- }
- return true;
+ const waitUntil = options.waitUntil ?? "load";
+ return waitUntil === "commit"
+ ? true
+ : waitForLoadState(waitUntil, {
+ timeout: Math.max(0, deadline - state.now()),
+ });
}
await state.sleep(100);
}
diff --git a/package/ego-browser/src/format.test.mjs b/package/ego-browser/src/format.test.mjs
index 2b0f05c..9b636b4 100644
--- a/package/ego-browser/src/format.test.mjs
+++ b/package/ego-browser/src/format.test.mjs
@@ -58,6 +58,22 @@ test("formatCliLogValue documents ego.learnings as the site facade alias", () =>
assert.match(parsed.learnings.runTool.example, /learnings\.runTool/);
});
+test("formatCliLogValue documents the waitForURL matcher and default", () => {
+ const formatted = formatCliLogValue({
+ helpers: { page: { waitForURL() {} } },
+ });
+
+ const parsed = JSON.parse(formatted);
+ assert.equal(
+ parsed.helpers.page.waitForURL.signature,
+ "page.waitForURL(url, options?) => Promise<boolean>",
+ );
+ assert.match(
+ parsed.helpers.page.waitForURL.description,
+ /predicate receiving a URL object.*load by default/,
+ );
+});
+
test("formatCliLogValue handles nested bigint and circular references", () => {
const value = { id: 1n, child: {} };
value.child.self = value;
diff --git a/package/ego-browser/src/format.ts b/package/ego-browser/src/format.ts
index 46996d8..7a779b5 100644
--- a/package/ego-browser/src/format.ts
+++ b/package/ego-browser/src/format.ts
@@ -288,24 +288,27 @@ const FUNCTION_DOCS: Record<string, FunctionDoc> = {
"await page.waitForFunction(() => document.readyState === 'complete')",
},
"page.waitForURL": {
- signature: "page.waitForURL(url, options?) => Promise<void>",
+ signature: "page.waitForURL(url, options?) => Promise<boolean>",
description:
- "Wait until the current URL matches a string, glob, regex, or predicate.",
+ "Wait until the current URL matches a string, glob, regex, or predicate receiving a URL object, then wait for load by default.",
params: [
{
name: "url",
type: "string | RegExp | Function",
required: true,
- description: "URL matcher.",
+ description:
+ "URL matcher. Predicate functions receive a URL object; use url.href, url.pathname, or url.searchParams.",
},
{
name: "options",
- type: "{ timeout?: number }",
- description: "Timeout in milliseconds.",
+ type: "{ timeout?: number, waitUntil?: 'load' | 'domcontentloaded' | 'networkidle' | 'commit' }",
+ description:
+ "Timeout in milliseconds and completion state; waitUntil defaults to 'load'.",
},
],
- returns: "Promise<void>",
- example: "await page.waitForURL(/docs\\.google\\.com/)",
+ returns: "Promise<boolean>",
+ example:
+ "await page.waitForURL(url => url.pathname === '/done', { timeout: 10000 })",
},
"page.waitForRequest": {
signature:
diff --git a/skills/ego-browser/SKILL.md b/skills/ego-browser/SKILL.md
index c84d9b5..d7e120b 100644
--- a/skills/ego-browser/SKILL.md
+++ b/skills/ego-browser/SKILL.md
@@ -53,6 +53,7 @@ Notes:
- `await browser.closeTab(target?)` — closes the given target id / tab object, or the current tab when omitted.
- `await page.drainEvents()` — consumes and returns the async event queue produced by the page (navigation events, network events, etc.).
- `page.waitForRequest(...)` / `page.waitForResponse(...)` — start the wait before the click or form action that triggers the network call.
+- `page.waitForURL(...)` predicate functions receive a `URL` object, not a string. Use `url.href.includes(...)`, `url.pathname`, or `url.searchParams`; do not call `url.includes(...)`. Its `waitUntil` defaults to `"load"`; pass `{ waitUntil: "commit" }` only when intentionally continuing while the document is still loading.
- Hard rule: during exploration, do not call `page.waitForTimeout(...)` with a value greater than `2000`. A fixed sleep is only for brief visual settling after a non-network UI action.
- For navigation, search, filtering, sorting, form submit, modal transition, or any action that changes page data, do not use fixed sleeps as the primary wait. Start `page.waitForResponse(...)`, `page.waitForRequest(...)`, `page.waitForURL(...)`, or a concrete state assertion before the triggering action, then verify the expected state.
@@ -63,6 +64,11 @@ const responsePromise = page.waitForResponse(
);
await page.getByRole('button', { name: /search/i }).click();
await responsePromise;
+
+await page.waitForURL(
+ (url) => url.pathname === '/orders/complete' && url.searchParams.has('id'),
+ { timeout: 15000 },
+);
```
- Hard rule: never reuse `@N` refs across heredoc rounds. Use `@N` only immediately after a fresh `page.snapshot()` in the same script. Across rounds, use `loc=css:...`, `loc=role:...`, `page.getByRole(...)`, `page.getByText(...)`, `page.getByLabel(...)`, or take a new snapshot and act on the new refs immediately.