Skip to content

Chrome DevTools for coding agents

feat: reduce StaticText noise in text snapshots (#2895)

Alex Rudenko44413258bc44
diff --git a/src/formatters/SnapshotFormatter.ts b/src/formatters/SnapshotFormatter.ts
index 29488cf5..b7531fdc 100644
--- a/src/formatters/SnapshotFormatter.ts
+++ b/src/formatters/SnapshotFormatter.ts
@@ -49,11 +49,29 @@ Get a verbose snapshot to include all elements if you are interested in the sele
chunks.push(line);
for (const child of node.children) {
+ if (this.#isRedundantStaticText(node, child)) {
+ continue;
+ }
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
+ );
+ }
+
#nodeToJSON(node: TextSnapshotNode): object {
const rawAttrs = this.#getAttributesMap(node);
const children = node.children.map(child => this.#nodeToJSON(child));
@@ -67,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}`];
- if (serializedAXNodeRoot.role) {
+ if (
+ serializedAXNodeRoot.role &&
+ serializedAXNodeRoot.role !== 'StaticText'
+ ) {
attributes.push(
serializedAXNodeRoot.role === 'none'
? 'ignored'
diff --git a/tests/McpResponse.test.js.snapshot b/tests/McpResponse.test.js.snapshot
index a8e86f61..6af99afb 100644
--- a/tests/McpResponse.test.js.snapshot
+++ b/tests/McpResponse.test.js.snapshot
@@ -367,7 +367,7 @@ exports[`McpResponse > returns correctly formatted snapshot for a simple tree 2`
exports[`McpResponse > returns values for textboxes 1`] = `
## Latest page snapshot
uid=1_0 RootWebArea "My test page" url="about:blank"
- uid=1_1 StaticText "username"
+ uid=1_1 "username"
uid=1_2 textbox "username" focusable focused value="mcp"
`;
@@ -404,7 +404,7 @@ uid=1_0 RootWebArea "My test page" url="about:blank"
uid=1_1 ignored
uid=1_2 ignored
uid=1_3 complementary
- uid=1_4 StaticText "test"
+ uid=1_4 "test"
uid=1_5 InlineTextBox "test"
`;
@@ -467,7 +467,7 @@ uid=1_0 RootWebArea "My test page" url="about:blank"
uid=1_1 ignored
uid=1_2 ignored
uid=1_3 complementary
- uid=1_4 StaticText "test"
+ uid=1_4 "test"
uid=1_5 InlineTextBox "test"
`;
diff --git a/tests/formatters/snapshotFormatter.test.ts b/tests/formatters/snapshotFormatter.test.ts
index 64a3ffcc..928902d3 100644
--- a/tests/formatters/snapshotFormatter.test.ts
+++ b/tests/formatters/snapshotFormatter.test.ts
@@ -14,6 +14,91 @@ import type {TextSnapshot} from '../../src/TextSnapshot.js';
import type {TextSnapshotNode} from '../../src/types.js';
describe('snapshotFormatter', () => {
+ function createNode(
+ id: string,
+ role: string,
+ name: string,
+ children: TextSnapshotNode[] = [],
+ ): TextSnapshotNode {
+ return {
+ id,
+ role,
+ name,
+ children,
+ elementHandle: async (): Promise<ElementHandle<Element> | null> => {
+ return null;
+ },
+ };
+ }
+
+ it('omits the StaticText role in text output', () => {
+ const node = createNode('1_1', 'link', 'Privacy', [
+ createNode('1_2', 'StaticText', 'Privacy Policy'),
+ ]);
+
+ const formatter = new SnapshotFormatter({root: node} as TextSnapshot);
+ assert.strictEqual(
+ formatter.toString(),
+ `uid=1_1 link "Privacy"
+ uid=1_2 "Privacy Policy"
+`,
+ );
+ assert.deepStrictEqual(formatter.toJSON(), {
+ id: '1_1',
+ role: 'link',
+ name: 'Privacy',
+ children: [{id: '1_2', role: 'StaticText', name: 'Privacy Policy'}],
+ });
+ });
+
+ it('skips the only StaticText child duplicating the parent name', () => {
+ const node = createNode('1_1', 'link', 'Privacy Policy', [
+ createNode('1_2', 'StaticText', 'Privacy Policy'),
+ ]);
+
+ const formatter = new SnapshotFormatter({
+ root: node,
+ verbose: false,
+ } as TextSnapshot);
+ assert.strictEqual(formatter.toString(), `uid=1_1 link "Privacy Policy"\n`);
+ });
+
+ it('keeps the duplicate StaticText child in verbose mode', () => {
+ const node = createNode('1_1', 'link', 'Privacy Policy', [
+ createNode('1_2', 'StaticText', 'Privacy Policy'),
+ ]);
+
+ const formatter = new SnapshotFormatter({
+ root: node,
+ verbose: true,
+ } as TextSnapshot);
+ assert.strictEqual(
+ formatter.toString(),
+ `uid=1_1 link "Privacy Policy"
+ uid=1_2 "Privacy Policy"
+`,
+ );
+ });
+
+ it('keeps the duplicate StaticText child if it has siblings', () => {
+ const node = createNode('1_1', 'link', 'Privacy Policy', [
+ createNode('1_2', 'StaticText', 'Privacy Policy'),
+ createNode('1_3', 'image', 'icon'),
+ ]);
+
+ const formatter = new SnapshotFormatter({
+ root: node,
+ verbose: false,
+ } as TextSnapshot);
+ assert.strictEqual(
+ formatter.toString(),
+ `uid=1_1 link "Privacy Policy"
+ uid=1_2 "Privacy Policy"
+ uid=1_3 image "icon"
+`,
+ );
+ });
+
it('formats a snapshot with value properties', () => {
const node: TextSnapshotNode = {
id: '1_1',