diff --git a/e2e/lib/cdp.mjs b/e2e/lib/cdp.mjs index 0b5742a9..fa5689a0 100644 --- a/e2e/lib/cdp.mjs +++ b/e2e/lib/cdp.mjs @@ -33,7 +33,7 @@ export class CdpClient { }); } - static async connect(port, { timeoutMs = 30_000 } = {}) { + static async connect(port, { timeoutMs = 30_000, match = () => true } = {}) { assert.equal( typeof WebSocket, "function", @@ -49,7 +49,8 @@ export class CdpClient { if (!response.ok) throw new Error(`HTTP ${response.status}`); const targets = await response.json(); const target = targets.find( - (item) => item.type === "page" && item.webSocketDebuggerUrl, + (item) => + item.type === "page" && item.webSocketDebuggerUrl && match(item), ); if (!target) throw new Error("no debuggable page target"); const socket = new WebSocket(target.webSocketDebuggerUrl); diff --git a/e2e/tests/browser.test.mjs b/e2e/tests/browser.test.mjs index 1a6b9187..479fba92 100644 --- a/e2e/tests/browser.test.mjs +++ b/e2e/tests/browser.test.mjs @@ -1177,6 +1177,19 @@ test("real Wayfern fingerprinting, terms, API automation, CDP, cookies, and proc }, { timeoutMs: 20_000, description: "API-opened Wayfern target" }, ); + // The tab's own first load can start before the capture attaches to it; + // what the tab sends once it is open is captured. + const openedTab = await CdpClient.connect( + launched.value.remote_debugging_port, + { match: (target) => target.url.includes("/opened-via-api") }, + ); + try { + await openedTab.evaluate( + "fetch('/api/echo?from=opened-via-api').then((response) => response.text())", + ); + } finally { + openedTab.close(); + } const capturedRequest = await app.waitFor( async () => { const listed = await app.invoke("list_network_requests", {