Skip to content

Chrome DevTools for coding agents

feat: list errors a page raises while a tool runs (#3431)

Thomas Bachemd5d30bd03956
diff --git a/src/McpPage.ts b/src/McpPage.ts
index 08ab96d1..2c25e95d 100644
--- a/src/McpPage.ts
+++ b/src/McpPage.ts
@@ -855,6 +855,19 @@ export class McpPage implements ContextPage {
return this.consoleCollector.getById(id);
}
+ getLatestConsoleMessageId(): number {
+ const latest = this.consoleCollector.getData(true).at(-1);
+ return latest ? this.consoleCollector.getIdForResource(latest) : 0;
+ }
+
+ getConsoleDataAfter(
+ id: number,
+ ): Array<ConsoleMessage | Error | DevTools.AggregatedIssue | UncaughtError> {
+ return this.consoleCollector
+ .getData(true)
+ .filter(item => this.consoleCollector.getIdForResource(item) > id);
+ }
+
getNetworkRequestById(reqid: number): HTTPRequest {
return this.networkCollector.getById(reqid);
}
diff --git a/src/McpResponse.ts b/src/McpResponse.ts
index a764afb2..2c6318eb 100644
--- a/src/McpResponse.ts
+++ b/src/McpResponse.ts
@@ -67,6 +67,7 @@ import type {
SnapshotParams,
WebAuthnAuthenticatorState,
} from './tools/ToolDefinition.js';
+import {LIST_CONSOLE_MESSAGES_TOOL_NAME} from './tools/toolNames.js';
import {
type InsightName,
type TraceResult,
@@ -82,6 +83,9 @@ import type {WaitForEventsResult} from './utils/WaitForHelper.js';
const {formatBytesToKb} = DevTools.I18n.ByteUtilities;
+/** How many errors raised during a call the response lists. */
+const MAX_CONSOLE_ERRORS = 10;
+
export type DataFormat = 'default' | 'toon' | 'gcf';
interface TraceInsightData {
@@ -156,6 +160,7 @@ export class McpResponse implements Response {
#page?: McpPage;
#error?: Error;
#attachedWaitForResult?: WaitForEventsResult;
+ #consoleErrorsFrom?: {page: McpPage; lastMessageId: number};
#reconnectNotice = false;
#devToolsComments?: CD4ACommentThread[];
#webAuthnFormatter?: WebAuthnFormatter;
@@ -277,6 +282,13 @@ export class McpResponse implements Response {
};
}
+ includeConsoleErrorsFrom(page: McpPage): void {
+ this.#consoleErrorsFrom = {
+ page,
+ lastMessageId: page.getLatestConsoleMessageId(),
+ };
+ }
+
setIncludeCssStyles(
matchedStyles: MatchedStyles,
options: CssFormatterOptions & PaginationOptions,
@@ -671,8 +683,42 @@ export class McpResponse implements Response {
);
}
- if (this.#consoleDataOptions.types?.length) {
- const normalizedTypes = new Set(this.#consoleDataOptions.types);
+ return await this.#formatConsoleData(
+ messages,
+ page,
+ this.#consoleDataOptions,
+ );
+ }
+
+ async #handleConsoleErrors(): Promise<
+ Array<ConsoleFormatter | IssueFormatter> | undefined
+ > {
+ const errorsFrom = this.#consoleErrorsFrom;
+ // A tool that lists or shows console messages itself already covers these.
+ if (
+ !errorsFrom ||
+ this.#consoleDataOptions?.include ||
+ this.#attachedConsoleMessageId !== undefined ||
+ errorsFrom.page.isClosed()
+ ) {
+ return undefined;
+ }
+ return await this.#formatConsoleData(
+ errorsFrom.page.getConsoleDataAfter(errorsFrom.lastMessageId),
+ errorsFrom.page,
+ {types: ['error']},
+ );
+ }
+
+ async #formatConsoleData(
+ messages: Array<
+ ConsoleMessage | Error | DevTools.AggregatedIssue | UncaughtError
+ >,
+ page: McpPage | undefined,
+ options: {types?: string[]; includeStackTraces?: boolean},
+ ): Promise<Array<ConsoleFormatter | IssueFormatter>> {
+ if (options.types?.length) {
+ const normalizedTypes = new Set(options.types);
messages = messages.filter(message => {
if ('type' in message) {
return normalizedTypes.has(message.type());
@@ -694,7 +740,7 @@ export class McpResponse implements Response {
return await ConsoleFormatter.from(consoleMessage, {
id: consoleMessageStableId,
fetchDetailedData: false,
- fetchStackTrace: this.#consoleDataOptions?.includeStackTraces,
+ fetchStackTrace: options.includeStackTraces,
devTools: page ? page.devtoolsUniverse : undefined,
});
}
@@ -808,6 +854,7 @@ export class McpResponse implements Response {
thirdPartyDeveloperTools,
webmcpTools,
consoleMessages,
+ consoleErrors,
networkRequests,
commentsResult,
] = await Promise.all([
@@ -817,6 +864,7 @@ export class McpResponse implements Response {
this.#handleThirdPartyDeveloperTools(),
this.#handleWebMCP(),
this.#handleConsoleList(context),
+ this.#handleConsoleErrors(),
this.#handleNetworkRequestList(context),
this.#handleComments(),
]);
@@ -835,6 +883,7 @@ export class McpResponse implements Response {
{
detailedConsoleMessage,
consoleMessages,
+ consoleErrors,
snapshot,
detailedNetworkRequest,
networkRequests,
@@ -868,6 +917,7 @@ export class McpResponse implements Response {
data: {
detailedConsoleMessage: ConsoleFormatter | IssueFormatter | undefined;
consoleMessages: Array<ConsoleFormatter | IssueFormatter> | undefined;
+ consoleErrors?: Array<ConsoleFormatter | IssueFormatter>;
snapshot: SnapshotFormatter | string | undefined;
detailedNetworkRequest?: NetworkFormatter;
networkRequests?: NetworkFormatter[];
@@ -894,6 +944,7 @@ export class McpResponse implements Response {
networkRequests?: object[];
consoleMessage?: object;
consoleMessages?: object[];
+ consoleErrors?: object[];
traceSummary?: string;
traceInsights?: Array<{
insightName: string;
@@ -1735,6 +1786,23 @@ Call ${handleDialog(this.#config.resolved).name} to handle it before continuing.
}
}
+ if (data.consoleErrors?.length) {
+ const grouped = ConsoleFormatter.groupConsecutive(data.consoleErrors);
+ const shown = grouped.slice(0, MAX_CONSOLE_ERRORS);
+ structuredContent.consoleErrors = shown.map(item => item.toJSON());
+ response.push('## Console errors during this call');
+ if (compactEncode) {
+ response.push(compactEncode(structuredContent.consoleErrors));
+ } else {
+ response.push(...shown.map(item => item.toString()));
+ }
+ if (grouped.length > shown.length) {
+ response.push(
+ `${grouped.length - shown.length} more not shown. Call ${LIST_CONSOLE_MESSAGES_TOOL_NAME} to list them.`,
+ );
+ }
+ }
+
if (data.comments) {
const commentsJson = data.comments.toJSON();
structuredContent.comments = commentsJson;
diff --git a/src/ToolHandler.ts b/src/ToolHandler.ts
index 52954e90..60d0caa9 100644
--- a/src/ToolHandler.ts
+++ b/src/ToolHandler.ts
@@ -419,6 +419,10 @@ export class ToolHandler {
await page?.init({emulateFocus: true});
signal.throwIfAborted();
response.setPage(page);
+ // A call that runs beside others can't tell its own errors apart.
+ if (tool.concurrency !== 'none') {
+ response.includeConsoleErrorsFrom(page);
+ }
if (tool.blockedByDialog) {
page.throwIfDialogOpen();
}
diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts
index 4f0dc081..8aafdb94 100644
--- a/src/tools/ToolDefinition.ts
+++ b/src/tools/ToolDefinition.ts
@@ -305,6 +305,11 @@ export interface Response {
serviceWorkerId?: string;
},
): void;
+ /**
+ * Lists the console errors and uncaught exceptions that the page raises from
+ * now until the response is handled.
+ */
+ includeConsoleErrorsFrom(page: ContextPage): void;
setIncludeCssStyles(
matchedStyles: MatchedStyles,
options: CssFormatterOptions & PaginationOptions,
diff --git a/src/tools/pages.ts b/src/tools/pages.ts
index 5a96cf30..79823428 100644
--- a/src/tools/pages.ts
+++ b/src/tools/pages.ts
@@ -150,6 +150,7 @@ export const newPage: ToolFactory = defineTool((args: ParsedArguments) => {
request.params.background,
request.params.isolatedContext,
);
+ response.includeConsoleErrorsFrom(page);
try {
await page.waitForEventsAfterAction(
diff --git a/src/tools/script.ts b/src/tools/script.ts
index 0cb6d755..0a43c863 100644
--- a/src/tools/script.ts
+++ b/src/tools/script.ts
@@ -197,6 +197,8 @@ Example with arguments: \`(el) => el.innerText\`
const evaluatable = await getPageOrFrame(page, frames);
+ // ToolHandler hands over the page of page-scoped tools only.
+ response.includeConsoleErrorsFrom(mcpPage);
const result = await mcpPage.waitForEventsAfterAction(
async () => {
await performEvaluation(
diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.ts
index a8144392..e961afa5 100644
--- a/tests/McpPage.test.ts
+++ b/tests/McpPage.test.ts
@@ -678,6 +678,55 @@ describe('McpPage', () => {
});
});
+ describe('getLatestConsoleMessageId() and getConsoleDataAfter()', () => {
+ function createUncaughtError(exceptionId: number): UncaughtError {
+ return new UncaughtError(
+ {
+ exceptionId,
+ text: `error-${exceptionId}`,
+ lineNumber: 0,
+ columnNumber: 0,
+ },
+ '<mock>',
+ );
+ }
+
+ it('getLatestConsoleMessageId() returns 0 and getConsoleDataAfter() nothing for an empty collector', async () => {
+ const {mcpPage} = await createMcpPage();
+
+ assert.strictEqual(mcpPage.getLatestConsoleMessageId(), 0);
+ assert.deepStrictEqual(mcpPage.getConsoleDataAfter(0), []);
+ });
+
+ it('getLatestConsoleMessageId() includes messages from an earlier navigation', async () => {
+ const {mcpPage, pptrPage} = await createMcpPage();
+ const error = createUncaughtError(1);
+ pptrPage.emit('uncaughtError', error);
+ pptrPage.emit('framenavigated', pptrPage.mainFrame());
+
+ const id = mcpPage.getLatestConsoleMessageId();
+
+ assert.strictEqual(id, mcpPage.consoleCollector.getIdForResource(error));
+ assert.deepStrictEqual(mcpPage.getConsoleDataAfter(id), []);
+ });
+
+ it('getConsoleDataAfter() returns the messages after the id, from an earlier navigation too', async () => {
+ const {mcpPage, pptrPage} = await createMcpPage();
+ pptrPage.emit('uncaughtError', createUncaughtError(1));
+ const id = mcpPage.getLatestConsoleMessageId();
+ const beforeNavigation = createUncaughtError(2);
+ pptrPage.emit('uncaughtError', beforeNavigation);
+ pptrPage.emit('framenavigated', pptrPage.mainFrame());
+ const afterNavigation = createUncaughtError(3);
+ pptrPage.emit('uncaughtError', afterNavigation);
+
+ assert.deepStrictEqual(mcpPage.getConsoleDataAfter(id), [
+ beforeNavigation,
+ afterNavigation,
+ ]);
+ });
+ });
+
describe('getTitle()', () => {
function createPage(url: string, title: string): McpPage {
const target = createMockPuppeteerTarget({url, title});
diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.ts
index 638e5761..862a562b 100644
--- a/tests/McpResponse.test.ts
+++ b/tests/McpResponse.test.ts
@@ -12,6 +12,8 @@ import {after, describe, it} from 'node:test';
import sinon from 'sinon';
import type {ParsedArguments} from '../src/config/mcp-options.js';
+import type {TargetUniverse} from '../src/devtools/DevtoolsUtils.js';
+import {ConsoleFormatter} from '../src/formatters/ConsoleFormatter.js';
import type {McpContext} from '../src/McpContext.js';
import {McpResponse} from '../src/McpResponse.js';
import {TextSnapshot} from '../src/TextSnapshot.js';
@@ -37,6 +39,7 @@ import {
createHandlerMocks,
createMockConsoleMessage,
createMockHeapSnapshotAggregate,
+ createMockCDPSession,
createMockCSSMatchedStyles,
createMockCSSProperty,
createMockCSSStyleDeclaration,
@@ -676,6 +679,223 @@ describe('McpResponse', () => {
});
});
+ describe('console errors of a handed over page', () => {
+ const heading = '## Console errors during this call';
+
+ function createHandedOverPage(
+ messages: Array<ReturnType<typeof createMockConsoleMessage>>,
+ universe?: TargetUniverse,
+ ) {
+ const page = createMockMcpPage();
+ page.getConsoleDataAfter.returns(messages);
+ sinon.stub(page, 'devtoolsUniverse').get(() => universe);
+ return {
+ page,
+ context: createMockMcpContext({selectedPage: page}),
+ response: new McpResponse(createMockServerConfig()),
+ };
+ }
+
+ function createErrors(...texts: string[]) {
+ return texts.map(text => createMockConsoleMessage({type: 'error', text}));
+ }
+
+ it('lists the errors after the message id read at the hand-over', async () => {
+ const {page, context, response} = createHandedOverPage([
+ createMockConsoleMessage({type: 'warn', text: 'warned after'}),
+ ...createErrors('logged after'),
+ ]);
+ page.getLatestConsoleMessageId.returns(7);
+ response.includeConsoleErrorsFrom(page);
+ page.getLatestConsoleMessageId.returns(9);
+
+ const {content} = await response.handle(context);
+
+ sinon.assert.calledOnceWithExactly(page.getConsoleDataAfter, 7);
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes(heading));
+ assert.ok(text.includes('logged after'));
+ assert.ok(!text.includes('warned after'));
+ });
+
+ it('lists the first errors and points to the rest', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors(
+ ...Array.from(
+ {length: 12},
+ (_, i) => `error ${String(i).padStart(2, '0')}`,
+ ),
+ ),
+ );
+ response.includeConsoleErrorsFrom(page);
+
+ const {content} = await response.handle(context);
+
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes('error 09'));
+ assert.ok(!text.includes('error 10'));
+ assert.ok(text.includes('2 more not shown.'));
+ });
+
+ it('lists ten errors without a pointer to the rest', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors(...Array.from({length: 10}, (_, i) => `error ${i}`)),
+ );
+ response.includeConsoleErrorsFrom(page);
+
+ const {content} = await response.handle(context);
+
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes('error 9'));
+ assert.ok(!text.includes('more not shown'));
+ });
+
+ it('counts a repeated error once toward the cap', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors(...Array.from({length: 12}, () => 'thrown in a loop')),
+ );
+ response.includeConsoleErrorsFrom(page);
+
+ const {content, structuredContent} = await response.handle(context);
+
+ assert.ok('consoleErrors' in structuredContent);
+ assert.ok(Array.isArray(structuredContent.consoleErrors));
+ assert.strictEqual(structuredContent.consoleErrors.length, 1);
+ assert.ok(!getTextContent(content[0]).includes('more not shown'));
+ });
+
+ it('leaves the section out when there are none', async () => {
+ const {page, context, response} = createHandedOverPage([]);
+ response.includeConsoleErrorsFrom(page);
+
+ const {content} = await response.handle(context);
+
+ assert.ok(!getTextContent(content[0]).includes(heading));
+ });
+
+ it('lists the errors next to the error of a tool that threw', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors('thrown in a timer'),
+ );
+ response.includeConsoleErrorsFrom(page);
+ response.setError(new Error('thrown by the function'));
+
+ const {content} = await response.handle(context);
+
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes('thrown by the function'));
+ assert.ok(text.includes(heading));
+ assert.ok(text.includes('thrown in a timer'));
+ });
+
+ it('formats the errors with the DevTools universe of the handed over page', async () => {
+ const universe: TargetUniverse = {
+ universe: sinon.createStubInstance(
+ DevTools.Foundation.Universe.Universe,
+ ),
+ target: sinon.createStubInstance(DevTools.Target),
+ session: createMockCDPSession(),
+ };
+ const [message] = createErrors('logged after');
+ const {page, context, response} = createHandedOverPage(
+ [message],
+ universe,
+ );
+ const formatter = await ConsoleFormatter.from(message, {
+ id: -1,
+ fetchDetailedData: false,
+ });
+ const from = sinon.stub(ConsoleFormatter, 'from').resolves(formatter);
+ response.includeConsoleErrorsFrom(page);
+
+ await response.handle(context);
+
+ sinon.assert.calledOnceWithExactly(from, message, {
+ id: -1,
+ fetchDetailedData: false,
+ fetchStackTrace: undefined,
+ devTools: universe,
+ });
+ });
+
+ it('keeps the listing of a tool that lists console messages', async () => {
+ const messages = createErrors('logged after');
+ const {page, context, response} = createHandedOverPage(messages);
+ page.getConsoleData.returns(messages);
+ response.setPage(page);
+ response.setIncludeConsoleData(true);
+ response.includeConsoleErrorsFrom(page);
+
+ const {content} = await response.handle(context);
+
+ sinon.assert.notCalled(page.getConsoleDataAfter);
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes('## Console messages'));
+ assert.ok(!text.includes(heading));
+ });
+
+ it('leaves the errors out next to a single attached message', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors('logged after'),
+ );
+ const [attached] = createErrors('logged before');
+ page.getConsoleMessageById.returns(attached);
+ response.setPage(page);
+ response.attachConsoleMessage(1);
+ response.includeConsoleErrorsFrom(page);
+
+ const {content} = await response.handle(context);
+
+ sinon.assert.notCalled(page.getConsoleDataAfter);
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes('logged before'));
+ assert.ok(!text.includes(heading));
+ });
+
+ it('keeps the pagination of the list the tool asked for', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors('logged after'),
+ );
+ page.getNetworkRequests.returns([
+ getMockRequest({stableId: 1}),
+ getMockRequest({stableId: 2}),
+ ]);
+ response.setPage(page);
+ response.setIncludeNetworkRequests(true, {pageSize: 1});
+ response.includeConsoleErrorsFrom(page);
+
+ const {content, structuredContent} = await response.handle(context);
+
+ assert.ok(getTextContent(content[0]).includes('logged after'));
+ assert.ok('consoleErrors' in structuredContent);
+ assert.ok(Array.isArray(structuredContent.consoleErrors));
+ assert.strictEqual(structuredContent.consoleErrors.length, 1);
+ assert.ok('pagination' in structuredContent);
+ assert.deepStrictEqual(structuredContent.pagination, {
+ currentPage: 0,
+ totalPages: 2,
+ hasNextPage: true,
+ hasPreviousPage: false,
+ startIndex: 0,
+ endIndex: 1,
+ invalidPage: false,
+ });
+ });
+
+ it('leaves out the errors of a page closed meanwhile', async () => {
+ const {page, context, response} = createHandedOverPage(
+ createErrors('logged before closing'),
+ );
+ response.includeConsoleErrorsFrom(page);
+ page.isClosed.callsFake(() => true);
+
+ const {content} = await response.handle(context);
+
+ sinon.assert.notCalled(page.getConsoleDataAfter);
+ assert.ok(!getTextContent(content[0]).includes(heading));
+ });
+ });
+
it("doesn't list the issue message if mapping returns null", async t => {
await withMcpContext(async (response, context) => {
const mockAggregatedIssue = getMockAggregatedIssue();
diff --git a/tests/ToolHandler.test.ts b/tests/ToolHandler.test.ts
index 047ba68a..f13f78f5 100644
--- a/tests/ToolHandler.test.ts
+++ b/tests/ToolHandler.test.ts
@@ -710,6 +710,7 @@ describe('ToolHandler', () => {
.stub(McpResponse.prototype, 'handle')
.resolves({content: [], structuredContent: {}});
const mockContext = createContext();
+ mockContext.getPageById.returns(createMockMcpPage());
const events: string[] = [];
const {promise: release, resolve: finishBlocking} =
Promise.withResolvers<void>();
@@ -2426,6 +2427,7 @@ describe('ToolHandler', () => {
mockContext.getDevToolsData.resolves({});
const mockPage = sinon.createStubInstance(McpPage);
mockPage.getDialog.returns(undefined);
+ mockPage.getConsoleDataAfter.returns([]);
sinon.stub(mockPage, 'networkConditions').get(() => undefined);
sinon.stub(mockPage, 'geolocation').get(() => undefined);
sinon.stub(mockPage, 'viewport').get(() => undefined);
@@ -2649,6 +2651,55 @@ describe('ToolHandler', () => {
assert.deepStrictEqual(focusStates, [false, true]);
});
+ async function handlePageToolCall(
+ concurrency: DefinedPageTool['concurrency'],
+ ) {
+ sinon
+ .stub(McpResponse.prototype, 'handle')
+ .resolves({content: [], structuredContent: {}});
+ const includeConsoleErrorsFrom = sinon.spy(
+ McpResponse.prototype,
+ 'includeConsoleErrorsFrom',
+ );
+ const config = parseConfig();
+ const mockContext = createMockMcpContext();
+ mockContext.ensureBrowser.resolves(
+ getMockBrowser({process: sinon.createStubInstance(ChildProcess)}),
+ );
+ const mockPage = createMockMcpPage();
+ mockContext.getPageById.returns(mockPage);
+ const tool = definePageTool(() => ({
+ name: 'page_tool',
+ description: 'A page scoped tool',
+ annotations: {category: ToolCategory.INPUT, readOnlyHint: false},
+ schema: {},
+ blockedByDialog: false,
+ concurrency,
+ verifyFilesSchema: {},
+ handler: async () => {},
+ }))(config.resolved);
+
+ await new ToolHandler(
+ tool,
+ config,
+ async () => mockContext,
+ sinon.spy(),
+ ).handle({pageId: 1});
+
+ return {includeConsoleErrorsFrom, mockPage};
+ }
+
+ it("hands the page of a page tool with concurrency 'exclusive' to the response", async () => {
+ const {includeConsoleErrorsFrom, mockPage} =
+ await handlePageToolCall('exclusive');
+ sinon.assert.calledOnceWithExactly(includeConsoleErrorsFrom, mockPage);
+ });
+
+ it("does not hand the page of a page tool with concurrency 'none' to the response", async () => {
+ const {includeConsoleErrorsFrom} = await handlePageToolCall('none');
+ sinon.assert.notCalled(includeConsoleErrorsFrom);
+ });
+
describe('browserless tools', () => {
function createBrowserlessTool(
handler: BrowserlessToolDefinition['handler'],
diff --git a/tests/mocks.ts b/tests/mocks.ts
index c648e902..197f960c 100644
--- a/tests/mocks.ts
+++ b/tests/mocks.ts
@@ -436,6 +436,8 @@ export function createMockMcpPage(
return {};
});
page.getWebMcpTools.returns([]);
+ page.getLatestConsoleMessageId.returns(0);
+ page.getConsoleDataAfter.returns([]);
page.discoverThirdPartyDeveloperTools.resolves([]);
page.getWebAuthnAuthenticator.returns(undefined);
Object.defineProperty(page, 'pptrPage', {
diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts
index 4366a1ef..aa938525 100644
--- a/tests/tools/pages.test.ts
+++ b/tests/tools/pages.test.ts
@@ -28,7 +28,12 @@ import {
createMockMcpPage,
createMockParsedArguments,
} from '../mocks.js';
-import {assertNoServiceWorkerReported, html, withMcpContext} from '../utils.js';
+import {
+ assertNoServiceWorkerReported,
+ getTextContent,
+ html,
+ withMcpContext,
+} from '../utils.js';
const EXTENSION_SW_PATH = path.resolve('tests/tools/fixtures/extension-sw');
const EXTENSION_PATH = path.resolve('tests/tools/fixtures/extension');
@@ -247,6 +252,14 @@ describe('pages', () => {
sinon.assert.calledOnceWithExactly(
response.setListThirdPartyDeveloperTools,
);
+ sinon.assert.calledOnceWithExactly(
+ response.includeConsoleErrorsFrom,
+ page,
+ );
+ sinon.assert.callOrder(
+ response.includeConsoleErrorsFrom,
+ page.pptrPage.goto,
+ );
});
it('closes the new tab and restores the selection when navigation fails', async () => {
const {
@@ -339,6 +352,22 @@ describe('pages', () => {
assert.ok(response.includePages);
});
});
+ it('lists the errors the page raises while it loads', async () => {
+ await withMcpContext(async (response, context, args) => {
+ await newPage(args).handler(
+ {
+ params: {
+ url: 'data:text/html,<script>console.error("on load")</script>',
+ },
+ },
+ response,
+ context,
+ );
+ const {content} = await response.handle(context);
+ // The page list shows the URL, so match the message line.
+ assert.ok(getTextContent(content[0]).includes('[error] on load'));
+ });
+ });
it('throws when navigating to a javascript URL and javascriptEvaluation is false', async () => {
const {context, response, args} = createHandlerMocks({
javascriptEvaluation: false,
diff --git a/tests/tools/script.test.ts b/tests/tools/script.test.ts
index 31d4e0a6..4ea171bd 100644
--- a/tests/tools/script.test.ts
+++ b/tests/tools/script.test.ts
@@ -24,6 +24,7 @@ import {
assertNoServiceWorkerReported,
createTempDir,
extractExtensionId,
+ getTextContent,
html,
withMcpContext,
} from '../utils.js';
@@ -169,6 +170,52 @@ describe('script', () => {
);
});
});
+ it('lists errors the page raised while the function ran', async () => {
+ await withMcpContext(async (response, context, args) => {
+ await evaluateScript(args).handler(
+ {
+ params: {
+ function: String(
+ () =>
+ new Promise(resolve => {
+ console.warn('warned in the function');
+ setTimeout(() => {
+ throw new Error('thrown in a timer');
+ }, 0);
+ setTimeout(() => resolve('done'), 50);
+ }),
+ ),
+ },
+ },
+ response,
+ context,
+ );
+ const {content} = await response.handle(context);
+ const text = getTextContent(content[0]);
+ assert.ok(text.includes('"done"'));
+ assert.ok(text.includes('## Console errors during this call'));
+ assert.ok(text.includes('thrown in a timer'));
+ assert.ok(!text.includes('warned in the function'));
+ });
+ });
+ it('hands its page to the response before the function runs', async () => {
+ const {page, response, context, args} = createHandlerMocks();
+ page.waitForEventsAfterAction.callsFake(async () => {
+ sinon.assert.calledOnceWithExactly(
+ response.includeConsoleErrorsFrom,
+ page,
+ );
+ return {};
+ });
+
+ await evaluateScript(args).handler(
+ {params: {function: String(() => 1)}},
+ response,
+ context,
+ );
+
+ sinon.assert.calledOnce(page.waitForEventsAfterAction);
+ });
it('loads function source from a local file', async () => {
const {page, response, context, args} = createHandlerMocks();
const sourcePath = path.join(os.tmpdir(), 'function.js');