Chrome DevTools for coding agents
fix(snapshot): omit uid for accessibility nodes without backendNodeId (#3187)
Twot9054351be25b
diff --git a/src/TextSnapshot.ts b/src/TextSnapshot.tsindex 6be61c61..841f75b8 100644--- a/src/TextSnapshot.ts+++ b/src/TextSnapshot.ts@@ -73,20 +73,22 @@ export class TextSnapshot {const assignIds = (node: SerializedAXNode): TextSnapshotNode => {let id = '';// @ts-expect-error untyped backendNodeId.- const backendNodeId: number = node.backendNodeId;- // @ts-expect-error untyped loaderId.- const uniqueBackendId = `${node.loaderId}_${backendNodeId}`;- const existingMcpId = uniqueBackendNodeIdToMcpId.get(uniqueBackendId);- if (existingMcpId !== undefined) {- // Re-use MCP exposed ID if the uniqueId is the same.- id = existingMcpId;- } else {- // Only generate a new ID if we have not seen the node before.- id = `${snapshotId}_${idCounter++}`;- uniqueBackendNodeIdToMcpId.set(uniqueBackendId, id);+ const backendNodeId: number | undefined = node.backendNodeId;+ if (backendNodeId !== undefined) {+ // @ts-expect-error untyped loaderId.+ const uniqueBackendId = `${node.loaderId}_${backendNodeId}`;+ const existingMcpId = uniqueBackendNodeIdToMcpId.get(uniqueBackendId);+ if (existingMcpId !== undefined) {+ // Re-use MCP exposed ID if the uniqueId is the same.+ id = existingMcpId;+ } else {+ // Only generate a new ID if we have not seen the node before.+ id = `${snapshotId}_${idCounter++}`;+ uniqueBackendNodeIdToMcpId.set(uniqueBackendId, id);+ }+ seenUniqueIds.add(uniqueBackendId);+ seenBackendNodeIds.add(backendNodeId);}- seenUniqueIds.add(uniqueBackendId);- seenBackendNodeIds.add(backendNodeId);const nodeWithId: TextSnapshotNode = {...node,@@ -105,7 +107,9 @@ export class TextSnapshot {}}- idToNode.set(nodeWithId.id, nodeWithId);+ if (id) {+ idToNode.set(nodeWithId.id, nodeWithId);+ }return nodeWithId;};diff --git a/src/formatters/SnapshotFormatter.ts b/src/formatters/SnapshotFormatter.tsindex 14661c55..4a3bd131 100644--- a/src/formatters/SnapshotFormatter.ts+++ b/src/formatters/SnapshotFormatter.ts@@ -42,7 +42,7 @@ Get a verbose snapshot to include all elements if you are interested in the seleconst line =' '.repeat(depth * 2) +attributes.join(' ') +- (node.id === this.#snapshot.selectedElementUid+ (node.id && node.id === this.#snapshot.selectedElementUid? ' [selected in the DevTools Elements panel]': '') +'\n';@@ -85,7 +85,10 @@ Get a verbose snapshot to include all elements if you are interested in the sele}#getAttributes(serializedAXNodeRoot: TextSnapshotNode): string[] {- const attributes = [`uid=${serializedAXNodeRoot.id}`];+ const attributes: string[] = [];+ if (serializedAXNodeRoot.id) {+ attributes.push(`uid=${serializedAXNodeRoot.id}`);+ }if (serializedAXNodeRoot.role &&@@ -141,7 +144,9 @@ Get a verbose snapshot to include all elements if you are interested in the sele): Record<string, unknown> {const result: Record<string, unknown> = {};if (!excludeSpecial) {- result.id = node.id;+ if (node.id) {+ result.id = node.id;+ }if (node.role) {result.role = node.role;}diff --git a/src/third_party/index.ts b/src/third_party/index.tsindex 29103c48..bab2515e 100644--- a/src/third_party/index.ts+++ b/src/third_party/index.ts@@ -46,6 +46,7 @@ export type ShapeOutput<T extends zod.ZodRawShape> = zod.output<export {default as ajv} from 'ajv';export {+ Accessibility,Dialog,ElementHandle,Locator,diff --git a/tests/McpResponse.test.js.snapshot b/tests/McpResponse.test.js.snapshotindex 7508f604..1064424b 100644--- a/tests/McpResponse.test.js.snapshot+++ b/tests/McpResponse.test.js.snapshot@@ -405,7 +405,7 @@ uid=1_0 RootWebArea "My test page" url="about:blank"uid=1_2 ignoreduid=1_3 complementaryuid=1_4 "test"- uid=1_5 InlineTextBox "test"+ InlineTextBox "test"`;@@ -435,7 +435,6 @@ exports[`McpResponse > returns verbose snapshot and structured content 2`] = `"name": "test","children": [{- "id": "1_5","role": "InlineTextBox","name": "test"}@@ -468,7 +467,7 @@ uid=1_0 RootWebArea "My test page" url="about:blank"uid=1_2 ignoreduid=1_3 complementaryuid=1_4 "test"- uid=1_5 InlineTextBox "test"+ InlineTextBox "test"`;diff --git a/tests/TextSnapshot.test.ts b/tests/TextSnapshot.test.tsindex 7627ea9c..4096b990 100644--- a/tests/TextSnapshot.test.ts+++ b/tests/TextSnapshot.test.ts@@ -12,6 +12,7 @@ import sinon from 'sinon';import {TextSnapshot} from '../src/TextSnapshot.js';import type {TextSnapshotNode} from '../src/types.js';+import {createMockAXNode, createMockMcpPage} from './mocks.js';import {html, withMcpContext} from './utils.js';describe('TextSnapshot', () => {@@ -20,6 +21,67 @@ describe('TextSnapshot', () => {TextSnapshot.resetCounter();});+ it('does not assign uids to nodes without backendNodeId', async () => {+ const page = createMockMcpPage();+ page.uniqueBackendNodeIdToMcpId = new Map();+ page.extraHandles = [];+ const mockRoot = createMockAXNode('RootWebArea', {+ loaderId: '1',+ backendNodeId: 1,+ children: [+ createMockAXNode('paragraph', {+ loaderId: '1',+ backendNodeId: 2,+ children: [+ createMockAXNode('InlineTextBox', {+ name: 'alpha',+ loaderId: '1',+ }),+ ],+ }),+ createMockAXNode('paragraph', {+ loaderId: '1',+ backendNodeId: 3,+ children: [+ createMockAXNode('InlineTextBox', {+ name: 'beta',+ loaderId: '1',+ }),+ ],+ }),+ ],+ });+ page.pptrPage.accessibility.snapshot.resolves(mockRoot);++ const snapshot = await TextSnapshot.create(page, {verbose: true});+ const inlineNodes = [...snapshot.idToNode.values()].filter(+ node => node.role === 'InlineTextBox',+ );++ assert.strictEqual(inlineNodes.length, 0);++ const paragraph1 = snapshot.idToNode.get('1_1');+ assert.strictEqual(paragraph1?.role, 'paragraph');+ assert.strictEqual(paragraph1?.children[0]?.role, 'InlineTextBox');+ assert.strictEqual(paragraph1?.children[0]?.id, '');+ assert.strictEqual(paragraph1?.children[0]?.name, 'alpha');++ const paragraph2 = snapshot.idToNode.get('1_2');+ assert.strictEqual(paragraph2?.role, 'paragraph');+ assert.strictEqual(paragraph2?.children[0]?.role, 'InlineTextBox');+ assert.strictEqual(paragraph2?.children[0]?.id, '');+ assert.strictEqual(paragraph2?.children[0]?.name, 'beta');++ assert.strictEqual(+ page.uniqueBackendNodeIdToMcpId.has('1_undefined'),+ false,+ );+ assert.strictEqual(+ page.uniqueBackendNodeIdToMcpId.has('undefined_undefined'),+ false,+ );+ });+it('creates a snapshot', async () => {await withMcpContext(async (_response, context) => {const page = context.getSelectedMcpPage();diff --git a/tests/formatters/snapshotFormatter.test.ts b/tests/formatters/snapshotFormatter.test.tsindex 84600175..21fb0316 100644--- a/tests/formatters/snapshotFormatter.test.ts+++ b/tests/formatters/snapshotFormatter.test.ts@@ -51,6 +51,26 @@ describe('snapshotFormatter', () => {});});+ it('omits uid= when node id is empty', () => {+ const node = createNode('1_1', 'paragraph', 'alpha', [+ createNode('', 'InlineTextBox', 'alpha'),+ ]);++ const formatter = new SnapshotFormatter({root: node} as TextSnapshot);+ assert.strictEqual(+ formatter.toString(),+ `uid=1_1 paragraph "alpha"+ InlineTextBox "alpha"+`,+ );+ assert.deepStrictEqual(formatter.toJSON(), {+ id: '1_1',+ role: 'paragraph',+ name: 'alpha',+ children: [{role: 'InlineTextBox', name: 'alpha'}],+ });+ });+it('skips the only StaticText child duplicating the parent name', () => {const node = createNode('1_1', 'link', 'Privacy Policy', [createNode('1_2', 'StaticText', 'Privacy Policy'),diff --git a/tests/mocks.ts b/tests/mocks.tsindex ff9bc4fd..59a6d679 100644--- a/tests/mocks.ts+++ b/tests/mocks.ts@@ -24,7 +24,7 @@import './setupDevTools.js';-import type {Frame, Mouse} from 'puppeteer-core';+import type {Frame, Mouse, SerializedAXNode} from 'puppeteer-core';import sinon from 'sinon';import {@@ -46,6 +46,7 @@ import type {HeapSnapshotDiffObject,} from '../src/processors/HeapSnapshotManager.js';import {+ Accessibility,CdpBrowser,CdpExtension,CdpFrame,@@ -79,6 +80,7 @@ export type MockPuppeteerPage = sinon.SinonStubbedInstance<Page> & {webmcp: sinon.SinonStubbedInstance<WebMCP>;tracing: sinon.SinonStubbedInstance<Tracing>;mouse: sinon.SinonStubbedInstance<Mouse>;+ accessibility: sinon.SinonStubbedInstance<Accessibility>;};export type MockMcpPage = sinon.SinonStubbedInstance<McpPage> & {@@ -87,6 +89,32 @@ export type MockMcpPage = sinon.SinonStubbedInstance<McpPage> & {};export type MockMcpContext = sinon.SinonStubbedInstance<McpContext>;export type MockMcpResponse = sinon.SinonStubbedInstance<McpResponse>;++export interface ProtocolAXNode extends SerializedAXNode {+ loaderId?: string;+ backendNodeId?: number;+ children?: ProtocolAXNode[];+ elementHandle: () => Promise<ElementHandle<Element> | null>;+}++export function createMockAXNode(+ role: string,+ options: {+ name?: string;+ loaderId?: string;+ backendNodeId?: number;+ children?: ProtocolAXNode[];+ } = {},+): ProtocolAXNode {+ return {+ role,+ name: options.name,+ loaderId: options.loaderId,+ backendNodeId: options.backendNodeId,+ children: options.children,+ elementHandle: async () => null,+ };+}export type MockDOMNode = sinon.SinonStubbedInstance<DevTools.DOMModel.DOMNode>;export type MockCSSProperty =sinon.SinonStubbedInstance<DevTools.CSSProperty.CSSProperty>;@@ -308,10 +336,17 @@ export function createMockPuppeteerPage(): MockPuppeteerPage {configurable: true,});+ const accessibility = sinon.createStubInstance(Accessibility);+ Object.defineProperty(page, 'accessibility', {+ value: accessibility,+ writable: true,+ configurable: true,+ });+page.emulateFocusedPage.resolves();page.evaluateHandle.resolves(createMockElementHandle().handle);- return Object.assign(page, {webmcp, tracing, mouse});+ return Object.assign(page, {webmcp, tracing, mouse, accessibility});}export function createMockWebMCPTool(options: {