Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(test): keep waiting through document transitions [C7]

Corey Olsonb6776950abbf
diff --git a/test/chrome-fixture.js b/test/chrome-fixture.js
index 45c6eb4c..f8e7645e 100644
--- a/test/chrome-fixture.js
+++ b/test/chrome-fixture.js
@@ -140,14 +140,21 @@ export async function startChrome({
async function navigate(nextUrl) {
await send('Page.navigate', { url: nextUrl }, 'navigate page');
}
- /** Wait until a page expression becomes truthy before its command deadline. */
+ /** Wait until a page expression becomes truthy, treating document-transition evaluation errors as pending. */
async function waitFor(expression, timeoutMs = commandTimeoutMs, label = 'wait for page condition') {
const waitDeadline = Date.now() + timeoutMs;
+ let lastEvaluationError = null;
while (Date.now() < waitDeadline) {
- if (await evaluate(expression, label)) return;
+ try {
+ if (await evaluate(expression, label)) return;
+ } catch (error) {
+ if (!(error instanceof Error) || !error.message.startsWith('Browser evaluation failed:')) throw error;
+ lastEvaluationError = error;
+ }
await pause(50);
}
- throw new Error(`Browser condition "${label}" did not arrive within ${timeoutMs}ms.`);
+ const exception = lastEvaluationError instanceof Error ? `; last evaluation failed: ${lastEvaluationError.message}` : '';
+ throw new Error(`Browser condition "${label}" did not arrive within ${timeoutMs}ms; expression: ${expression.slice(0, 200)}${exception}.`);
}
/** Start asynchronous page work without holding a CDP Runtime.evaluate command open. */
async function startTask(expression, label) {
diff --git a/test/chrome-fixture.test.js b/test/chrome-fixture.test.js
index 25ae0e5a..1f3dc136 100644
--- a/test/chrome-fixture.test.js
+++ b/test/chrome-fixture.test.js
@@ -1,6 +1,8 @@
/** The shared Chrome fixture bounds startup and removes profiles only after the owned process group exits [C7]. */
import assert from 'node:assert/strict';
+import { once } from 'node:events';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test } from 'node:test';
@@ -146,3 +148,66 @@ test('a failed browser evaluation names what the page threw [C7]', async () => {
await chrome.close();
}
});
+
+test('a wait that throws while the page changes keeps waiting [C7]', async (t) => {
+ let replacementResponse;
+ const server = createServer((request, response) => {
+ if (request.url === '/before') {
+ response.writeHead(200, { 'content-type': 'text/html' });
+ response.end('<!doctype html><body><script>window.goNext = () => location.replace("/next")</script>before</body>');
+ return;
+ }
+ if (request.url === '/next') {
+ response.writeHead(200, { 'content-type': 'text/html' });
+ response.write('<!doctype html><html><head><title>loading');
+ replacementResponse = response;
+ return;
+ }
+ if (request.url === '/observed-null') {
+ response.writeHead(204);
+ response.end();
+ server.emit('document-gap-observed');
+ replacementResponse?.end('</title></head><body>ready</body></html>');
+ return;
+ }
+ response.writeHead(404);
+ response.end();
+ });
+ server.listen(0, '127.0.0.1');
+ await once(server, 'listening');
+ const documentGapObserved = new Promise((resolveGap) => server.once('document-gap-observed', resolveGap));
+ t.after(() => {
+ server.closeAllConnections();
+ if (server.listening) server.close();
+ });
+
+ const chrome = await startChrome({ url: `http://127.0.0.1:${server.address().port}/before` });
+ try {
+ await chrome.waitFor("typeof window.goNext === 'function'", 5_000, 'initial document ready');
+ await chrome.evaluate('setTimeout(() => window.goNext(), 0); true');
+ const expression = "location.pathname === '/next' && document.body === null ? (() => { if (!window.__gapNotified) { window.__gapNotified = true; fetch('/observed-null'); } return document.body.textContent.includes('ready'); })() : document.body?.textContent.includes('ready') === true";
+ const waiting = chrome.waitFor(expression, 5_000, 'wait for the replacement document')
+ .then(() => ({ error: null }), (error) => ({ error }));
+ const first = await Promise.race([
+ documentGapObserved.then(() => ({ gapObserved: true })),
+ waiting.then((result) => ({ result })),
+ ]);
+ assert.equal(first.gapObserved, true, 'the real page expression observed its document without a body');
+ const waitResult = await waiting;
+ assert.equal(waitResult.error, null, 'the document-transition exception stays pending until the next page is ready');
+ assert.equal(await chrome.evaluate('document.body.textContent'), 'ready', 'the replacement document completed');
+
+ const alwaysThrows = '(() => { throw new Error("persistent evaluation failure"); })()';
+ const startedAt = Date.now();
+ await assert.rejects(chrome.waitFor(alwaysThrows, 350, 'always-throwing expression'), (error) => {
+ assert.match(error.message, /did not arrive within 350ms/u);
+ assert.match(error.message, /persistent evaluation failure/u);
+ assert.ok(error.message.includes(alwaysThrows), 'deadline diagnostic names the expression');
+ assert.ok(Date.now() - startedAt >= 350, 'the wait polls through its requested deadline');
+ return true;
+ });
+
+ } finally {
+ await chrome.close();
+ }
+});