Chrome DevTools for coding agents
feat: skip more redundant data in text snapshots (#2896)
Alex Rudenkob2f522c8ba0f
diff --git a/src/formatters/SnapshotFormatter.ts b/src/formatters/SnapshotFormatter.tsindex b7531fdc..14661c55 100644--- a/src/formatters/SnapshotFormatter.ts+++ b/src/formatters/SnapshotFormatter.ts@@ -48,28 +48,30 @@ Get a verbose snapshot to include all elements if you are interested in the sele'\n';chunks.push(line);- for (const child of node.children) {- if (this.#isRedundantStaticText(node, child)) {- continue;+ if (!this.#hasRedundantTextChildren(node)) {+ for (const child of node.children) {+ chunks.push(this.#formatNode(child, depth + 1));}- chunks.push(this.#formatNode(child, depth + 1));}return chunks.join('');}- #isRedundantStaticText(- parent: TextSnapshotNode,- child: TextSnapshotNode,- ): boolean {- return (- !this.#snapshot.verbose &&- parent.children.length === 1 &&- child.role === 'StaticText' &&- child.children.length === 0 &&- Boolean(parent.name) &&- child.name === parent.name &&- child.id !== this.#snapshot.selectedElementUid- );+ #hasRedundantTextChildren(node: TextSnapshotNode): boolean {+ if (this.#snapshot.verbose || !node.name || node.children.length === 0) {+ return false;+ }+ let text = '';+ for (const child of node.children) {+ if (+ child.role !== 'StaticText' ||+ child.children.length !== 0 ||+ child.id === this.#snapshot.selectedElementUid+ ) {+ return false;+ }+ text += child.name ?? '';+ }+ return stripWhitespace(text) === stripWhitespace(node.name);}#nodeToJSON(node: TextSnapshotNode): object {@@ -104,17 +106,25 @@ Get a verbose snapshot to include all elements if you are interested in the sele/* excludeSpecial */ true,);+ const isOption = serializedAXNodeRoot.role === 'option';for (const attr of Object.keys(serializedAXNodeRoot).sort()) {if (excludedAttributes.has(attr)) {continue;}const mapped = booleanPropertyMap[attr];- if (mapped && simpleAttrs[mapped]) {+ if (+ mapped &&+ simpleAttrs[mapped] &&+ !(isOption && mapped === 'selectable')+ ) {attributes.push(mapped);}const val = simpleAttrs[attr];+ if (isOption && attr === 'value' && val === serializedAXNodeRoot.name) {+ continue;+ }if (val === true) {attributes.push(attr);} else if (typeof val === 'string' || typeof val === 'number') {@@ -186,3 +196,7 @@ const excludedAttributes = new Set(['backendNodeId','loaderId',]);++function stripWhitespace(text: string): string {+ return text.replace(/\s+/g, '');+}diff --git a/tests/formatters/snapshotFormatter.test.ts b/tests/formatters/snapshotFormatter.test.tsindex 928902d3..84600175 100644--- a/tests/formatters/snapshotFormatter.test.ts+++ b/tests/formatters/snapshotFormatter.test.ts@@ -80,7 +80,7 @@ describe('snapshotFormatter', () => {);});- it('keeps the duplicate StaticText child if it has siblings', () => {+ it('keeps the duplicate StaticText child if it has non-text siblings', () => {const node = createNode('1_1', 'link', 'Privacy Policy', [createNode('1_2', 'StaticText', 'Privacy Policy'),createNode('1_3', 'image', 'icon'),@@ -99,6 +99,62 @@ describe('snapshotFormatter', () => {);});+ it('skips StaticText children whose combined text equals the parent name', () => {+ const node = createNode('1_1', 'link', 'English 7,245,000+ articles', [+ createNode('1_2', 'StaticText', 'English'),+ createNode('1_3', 'StaticText', '7,245,000+ '),+ createNode('1_4', 'StaticText', 'articles'),+ ]);++ const formatter = new SnapshotFormatter({+ root: node,+ verbose: false,+ } as TextSnapshot);+ assert.strictEqual(+ formatter.toString(),+ `uid=1_1 link "English 7,245,000+ articles"\n`,+ );+ });++ it('keeps StaticText children whose combined text differs from the parent name', () => {+ const node = createNode('1_1', 'link', 'English', [+ createNode('1_2', 'StaticText', 'English'),+ createNode('1_3', 'StaticText', 'articles'),+ ]);++ const formatter = new SnapshotFormatter({+ root: node,+ verbose: false,+ } as TextSnapshot);+ assert.strictEqual(+ formatter.toString(),+ `uid=1_1 link "English"+ uid=1_2 "English"+ uid=1_3 "articles"+`,+ );+ });++ it('omits redundant option attributes in text output', () => {+ const node = createNode('1_1', 'combobox', 'Language', [+ {...createNode('1_2', 'option', 'English'), value: 'English'},+ {+ ...createNode('1_3', 'option', 'Deutsch'),+ value: 'de',+ selected: true,+ },+ ]);++ const formatter = new SnapshotFormatter({root: node} as TextSnapshot);+ assert.strictEqual(+ formatter.toString(),+ `uid=1_1 combobox "Language"+ uid=1_2 option "English"+ uid=1_3 option "Deutsch" selected value="de"+`,+ );+ });+it('formats a snapshot with value properties', () => {const node: TextSnapshotNode = {id: '1_1',