Repository navigation
Expand file tree
/
Copy pathsnapshot.ts
More file actions
122 lines (110 loc) · 3.62 KB
/
Copy pathsnapshot.ts
File metadata and controls
122 lines (110 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { RawSnapshotNode } from '@agent-device/kernel/snapshot';
type LimrunSelector = { key: 'id' | 'label' | 'text' | 'value'; value: string };
export type IosTreeNode = {
elementType?: string;
type?: string;
label?: string;
AXLabel?: string | null;
identifier?: string;
AXUniqueId?: string | null;
value?: string;
AXValue?: string | null;
frame?: { x?: number; y?: number; width?: number; height?: number };
rect?: { x?: number; y?: number; width?: number; height?: number };
enabled?: boolean;
role?: string;
selected?: boolean;
checked?: boolean;
password?: boolean;
editable?: boolean;
heading?: boolean;
roleDescription?: string;
role_description?: string;
hittable?: boolean;
traits?: string[];
pid?: number;
children?: IosTreeNode[];
nodes?: IosTreeNode[];
elements?: IosTreeNode[];
};
export function flattenIosTree(input: IosTreeNode | IosTreeNode[]): RawSnapshotNode[] {
const roots = Array.isArray(input) ? input : [input];
const nodes: RawSnapshotNode[] = [];
const visit = (node: IosTreeNode, depth: number, parentIndex?: number) => {
const index = nodes.length;
nodes.push(mapIosNode(node, { index, depth, parentIndex }));
for (const child of readIosNodeChildren(node)) {
visit(child, depth + 1, index);
}
};
for (const root of roots) visit(root, 0);
return nodes;
}
function mapIosNode(
node: IosTreeNode,
options: { index: number; depth: number; parentIndex?: number },
): RawSnapshotNode {
return {
index: options.index,
type: readIosNodeType(node),
role: readIosNodeRole(node),
label: readIosNodeLabel(node),
value: readIosNodeValue(node),
identifier: readIosNodeIdentifier(node),
rect: readIosNodeRect(node),
enabled: node.enabled,
selected: node.selected,
checked: node.checked,
password: node.password,
editable: node.editable,
heading: node.heading,
roleDescription: node.roleDescription ?? node.role_description,
hittable: node.hittable,
depth: options.depth,
parentIndex: options.parentIndex,
};
}
function readIosNodeType(node: IosTreeNode): string | undefined {
return node.elementType ?? node.type;
}
function readIosNodeRole(node: IosTreeNode): string | undefined {
return node.role ?? readIosNodeType(node);
}
function readIosNodeLabel(node: IosTreeNode): string | undefined {
return node.label ?? node.AXLabel ?? undefined;
}
function readIosNodeValue(node: IosTreeNode): string | undefined {
return node.value ?? node.AXValue ?? undefined;
}
function readIosNodeIdentifier(node: IosTreeNode): string | undefined {
return node.identifier ?? node.AXUniqueId ?? undefined;
}
/** The node's screen-space rect, or undefined when the provider omitted a complete one. */
export function readIosNodeRect(node: IosTreeNode): RawSnapshotNode['rect'] {
const rect = node.rect ?? node.frame;
if (
!rect ||
typeof rect.x !== 'number' ||
typeof rect.y !== 'number' ||
typeof rect.width !== 'number' ||
typeof rect.height !== 'number'
) {
return undefined;
}
return {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
};
}
export function readIosNodeChildren(node: IosTreeNode): IosTreeNode[] {
return node.children ?? node.nodes ?? node.elements ?? [];
}
export function toIosSelector(selector: LimrunSelector) {
if (selector.key === 'id') return { AXUniqueId: selector.value };
if (selector.key === 'value') return { AXValue: selector.value };
// The Limrun iOS tree exposes visible text through AXLabel, so both
// agent-device label and text selectors target the provider's label field.
return { AXLabel: selector.value };
}