Skip to content

Commit 2082641

Browse files
authored
refactor: simplify snapshot generation (#369)
the only difference is that checked/pressed are only included if the element was checked/pressed. Refs: #363
1 parent 477eef4 commit 2082641

File tree

3 files changed

+18
-55
lines changed

3 files changed

+18
-55
lines changed

src/formatters/snapshotFormatter.ts

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,69 +28,32 @@ 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-
}
31+
const excluded = new Set(['id', 'role', 'name', 'elementHandle', 'children']);
5432

55-
// Boolean properties that also have an 'able' attribute
56-
const booleanPropertyMap = {
33+
const booleanPropertyMap: Record<string, string> = {
5734
disabled: 'disableable',
5835
expanded: 'expandable',
5936
focused: 'focusable',
6037
selected: 'selectable',
6138
};
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-
}
7039

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);
40+
for (const attr of Object.keys(serializedAXNodeRoot).sort()) {
41+
if (excluded.has(attr)) {
42+
continue;
8243
}
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]}"`);
44+
const value = (serializedAXNodeRoot as unknown as Record<string, unknown>)[
45+
attr
46+
];
47+
if (typeof value === 'boolean') {
48+
if (booleanPropertyMap[attr]) {
49+
attributes.push(booleanPropertyMap[attr]);
9150
}
51+
if (value) {
52+
attributes.push(attr);
53+
}
54+
} else if (typeof value === 'string' || typeof value === 'number') {
55+
attributes.push(`${attr}="${value}"`);
9256
}
9357
}
94-
9558
return attributes;
9659
}

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)