Skip to content

Commit f57febb

Browse files
committed
refactor: simplify snapshot generation
1 parent 5a6592d commit f57febb

File tree

3 files changed

+23
-56
lines changed

3 files changed

+23
-56
lines changed

src/formatters/snapshotFormatter.ts

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,69 +28,36 @@ function getAttributes(serializedAXNodeRoot: TextSnapshotNode): string[] {
2828
`"${serializedAXNodeRoot.name || ''}"`, // Corrected: Added quotes around name
2929
];
3030

31-
// Value properties
32-
const valueProperties = [
33-
'value',
34-
'valuetext',
35-
'valuemin',
36-
'valuemax',
37-
'level',
38-
'autocomplete',
39-
'haspopup',
40-
'invalid',
41-
'orientation',
42-
'description',
43-
'keyshortcuts',
44-
'roledescription',
45-
] as const;
46-
for (const property of valueProperties) {
47-
if (
48-
property in serializedAXNodeRoot &&
49-
serializedAXNodeRoot[property] !== undefined
50-
) {
51-
attributes.push(`${property}="${serializedAXNodeRoot[property]}"`);
52-
}
53-
}
54-
55-
// Boolean properties that also have an 'able' attribute
56-
const booleanPropertyMap = {
31+
const excluded = new Set([
32+
'id',
33+
'role',
34+
'name',
35+
'elementHandle',
36+
'children',
37+
]);
38+
39+
const booleanPropertyMap: {[key:string]: string} = {
5740
disabled: 'disableable',
5841
expanded: 'expandable',
5942
focused: 'focusable',
6043
selected: 'selectable',
6144
};
62-
for (const [property, ableAttribute] of Object.entries(booleanPropertyMap)) {
63-
if (property in serializedAXNodeRoot) {
64-
attributes.push(ableAttribute);
65-
if (serializedAXNodeRoot[property as keyof typeof booleanPropertyMap]) {
66-
attributes.push(property);
67-
}
68-
}
69-
}
7045

71-
const booleanProperties = [
72-
'modal',
73-
'multiline',
74-
'readonly',
75-
'required',
76-
'multiselectable',
77-
] as const;
78-
79-
for (const property of booleanProperties) {
80-
if (property in serializedAXNodeRoot && serializedAXNodeRoot[property]) {
81-
attributes.push(property);
46+
for (const attr of Object.keys(serializedAXNodeRoot).sort()) {
47+
if (excluded.has(attr)) {
48+
continue;
8249
}
83-
}
84-
85-
// Mixed boolean/string attributes
86-
for (const property of ['pressed', 'checked'] as const) {
87-
if (property in serializedAXNodeRoot) {
88-
attributes.push(property);
89-
if (serializedAXNodeRoot[property]) {
90-
attributes.push(`${property}="${serializedAXNodeRoot[property]}"`);
50+
const value = (serializedAXNodeRoot as unknown as {[key: string]: unknown})[attr];
51+
if (typeof value === 'boolean') {
52+
if (booleanPropertyMap[attr]) {
53+
attributes.push(booleanPropertyMap[attr]);
9154
}
55+
if (value) {
56+
attributes.push(attr);
57+
}
58+
} else if (typeof value === 'string') {
59+
attributes.push(`${attr}="${value}"`);
9260
}
9361
}
94-
9562
return attributes;
9663
}

tests/McpResponse.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ uid=1_0 RootWebArea ""
8989
## Page content
9090
uid=1_0 RootWebArea "My test page"
9191
uid=1_1 StaticText "username"
92-
uid=1_2 textbox "username" value="mcp" focusable focused
92+
uid=1_2 textbox "username" focusable focused value="mcp"
9393
`,
9494
);
9595
});

tests/formatters/snapshotFormatter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('snapshotFormatter', () => {
100100
const formatted = formatA11ySnapshot(snapshot);
101101
assert.strictEqual(
102102
formatted,
103-
`uid=1_1 checkbox "checkbox" checked checked="true"
103+
`uid=1_1 checkbox "checkbox" checked
104104
uid=1_2 statictext "text"
105105
`,
106106
);

0 commit comments

Comments
 (0)