-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathselectors.test.ts
More file actions
292 lines (272 loc) · 8.74 KB
/
selectors.test.ts
File metadata and controls
292 lines (272 loc) · 8.74 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { test } from 'vitest';
import assert from 'node:assert/strict';
import type { SnapshotState } from '../../utils/snapshot.ts';
import {
findSelectorChainMatch,
parseSelectorChain,
resolveSelectorChain,
splitSelectorFromArgs,
} from '../selectors.ts';
const nodes: SnapshotState['nodes'] = [
{
ref: 'e1',
index: 0,
type: 'XCUIElementTypeTextField',
label: 'Email',
value: '',
identifier: 'login_email',
rect: { x: 0, y: 0, width: 200, height: 44 },
enabled: true,
hittable: true,
},
{
ref: 'e2',
index: 1,
type: 'XCUIElementTypeButton',
label: 'Continue',
identifier: 'auth_continue',
rect: { x: 0, y: 80, width: 200, height: 44 },
enabled: true,
hittable: true,
},
{
ref: 'e3',
index: 2,
type: 'XCUIElementTypeButton',
label: 'Continue',
identifier: 'secondary_continue',
rect: { x: 0, y: 140, width: 200, height: 44 },
enabled: true,
hittable: true,
},
];
test('parseSelectorChain parses fallback and boolean terms', () => {
const chain = parseSelectorChain('id=auth_continue || role=button label="Continue" visible=true');
assert.equal(chain.selectors.length, 2);
assert.equal(chain.selectors[0]!.terms[0]!.key, 'id');
assert.equal(chain.selectors[1]!.terms[2]!.key, 'visible');
});
test('resolveSelectorChain resolves unique match', () => {
const chain = parseSelectorChain('id=login_email');
const resolved = resolveSelectorChain(nodes, chain, {
platform: 'ios',
requireRect: true,
requireUnique: true,
});
assert.ok(resolved);
assert.equal(resolved.node.ref, 'e1');
});
test('resolveSelectorChain falls back when first selector is ambiguous', () => {
const chain = parseSelectorChain('label="Continue" || id=auth_continue');
const resolved = resolveSelectorChain(nodes, chain, {
platform: 'ios',
requireRect: true,
requireUnique: true,
});
assert.ok(resolved);
assert.equal(resolved.selectorIndex, 1);
assert.equal(resolved.node.ref, 'e2');
});
test('resolveSelectorChain keeps strict ambiguity behavior by default', () => {
const chain = parseSelectorChain('label="Continue"');
const resolved = resolveSelectorChain(nodes, chain, {
platform: 'ios',
requireRect: true,
requireUnique: true,
});
assert.equal(resolved, null);
});
test('resolveSelectorChain disambiguates to deeper/smaller matching node when enabled', () => {
const disambiguationNodes: SnapshotState['nodes'] = [
{
ref: 'e1',
index: 0,
type: 'Other',
label: 'Press me',
rect: { x: 0, y: 0, width: 300, height: 300 },
depth: 1,
enabled: true,
hittable: true,
},
{
ref: 'e2',
index: 1,
type: 'Other',
label: 'Press me',
rect: { x: 10, y: 10, width: 100, height: 20 },
depth: 2,
enabled: true,
hittable: true,
},
];
const chain = parseSelectorChain('role="other" label="Press me" || label="Press me"');
const resolved = resolveSelectorChain(disambiguationNodes, chain, {
platform: 'ios',
requireRect: true,
requireUnique: true,
disambiguateAmbiguous: true,
});
assert.ok(resolved);
assert.equal(resolved.node.ref, 'e2');
assert.equal(resolved.matches, 2);
});
test('resolveSelectorChain disambiguation tie falls back to next selector', () => {
const tieNodes: SnapshotState['nodes'] = [
{
ref: 'e1',
index: 0,
type: 'Other',
label: 'Press me',
rect: { x: 0, y: 0, width: 100, height: 20 },
depth: 2,
enabled: true,
hittable: true,
},
{
ref: 'e2',
index: 1,
type: 'Other',
label: 'Press me',
rect: { x: 0, y: 40, width: 100, height: 20 },
depth: 2,
enabled: true,
hittable: true,
},
{
ref: 'e3',
index: 2,
type: 'Other',
label: 'Press me',
identifier: 'press_me_unique',
rect: { x: 0, y: 80, width: 100, height: 20 },
depth: 2,
enabled: true,
hittable: true,
},
];
const chain = parseSelectorChain('label="Press me" || id="press_me_unique"');
const resolved = resolveSelectorChain(tieNodes, chain, {
platform: 'ios',
requireRect: true,
requireUnique: true,
disambiguateAmbiguous: true,
});
assert.ok(resolved);
assert.equal(resolved.selectorIndex, 1);
assert.equal(resolved.node.ref, 'e3');
});
test('findSelectorChainMatch returns first matching selector for existence checks', () => {
const chain = parseSelectorChain('label="Continue" || id=auth_continue');
const match = findSelectorChainMatch(nodes, chain, {
platform: 'ios',
});
assert.ok(match);
assert.equal(match.selectorIndex, 0);
assert.equal(match.matches, 2);
});
test('splitSelectorFromArgs extracts selector prefix and trailing value', () => {
const split = splitSelectorFromArgs(['id=login_email', 'editable=true', 'qa@example.com']);
assert.ok(split);
assert.equal(split.selectorExpression, 'id=login_email editable=true');
assert.deepEqual(split.rest, ['qa@example.com']);
});
test('splitSelectorFromArgs prefers trailing token for value when requested', () => {
const split = splitSelectorFromArgs(['label="Filter"', 'visible=true'], {
preferTrailingValue: true,
});
assert.ok(split);
assert.equal(split.selectorExpression, 'label="Filter"');
assert.deepEqual(split.rest, ['visible=true']);
});
test('splitSelectorFromArgs keeps full selector when trailing value preference is disabled', () => {
const split = splitSelectorFromArgs(['label="Filter"', 'visible=true']);
assert.ok(split);
assert.equal(split.selectorExpression, 'label="Filter" visible=true');
assert.deepEqual(split.rest, []);
});
test('parseSelectorChain rejects unknown keys and malformed quotes', () => {
assert.throws(() => parseSelectorChain('foo=bar'), /Unknown selector key/i);
assert.throws(() => parseSelectorChain('label="unclosed'), /Unclosed quote/i);
assert.throws(() => parseSelectorChain(''), /cannot be empty/i);
});
test('parseSelectorChain handles quoted values ending in escaped backslashes', () => {
const chain = parseSelectorChain('label="path\\\\" || id=auth_continue');
assert.equal(chain.selectors.length, 2);
});
test('text selector matches extractNodeText semantics (first non-empty field)', () => {
const chainByLabel = parseSelectorChain('text=Email');
const chainById = parseSelectorChain('text=login_email');
const resolvedLabel = resolveSelectorChain(nodes, chainByLabel, {
platform: 'ios',
requireUnique: true,
});
const resolvedId = resolveSelectorChain(nodes, chainById, {
platform: 'ios',
requireUnique: true,
});
assert.ok(resolvedLabel);
assert.equal(resolvedLabel.node.ref, 'e1');
assert.equal(resolvedId, null);
});
test('role selector normalization matches Android class names by leaf type', () => {
const androidNodes: SnapshotState['nodes'] = [
{
ref: 'a1',
index: 0,
type: 'android.widget.Button',
label: 'Continue',
identifier: 'auth_continue',
rect: { x: 0, y: 0, width: 120, height: 44 },
enabled: true,
hittable: true,
},
];
const chain = parseSelectorChain('role=button label="Continue"');
const resolved = resolveSelectorChain(androidNodes, chain, {
platform: 'android',
requireRect: true,
requireUnique: true,
});
assert.ok(resolved);
assert.equal(resolved.node.ref, 'a1');
});
// ── appName / windowTitle selectors ──────────────────────────────────────
test('appName selector matches nodes with appName field', () => {
const desktopNodes: SnapshotState['nodes'] = [
{
ref: 'd1',
index: 0,
type: 'Button',
label: 'OK',
appName: 'Calculator',
windowTitle: 'Main Window',
rect: { x: 0, y: 0, width: 80, height: 30 },
hittable: true,
},
{
ref: 'd2',
index: 1,
type: 'Button',
label: 'OK',
appName: 'TextEditor',
windowTitle: 'Untitled',
rect: { x: 0, y: 0, width: 80, height: 30 },
hittable: true,
},
];
// Match by appName — should disambiguate two OK buttons
const chain1 = parseSelectorChain('label=OK appname=Calculator');
const match1 = findSelectorChainMatch(desktopNodes, chain1, { platform: 'linux' });
assert.ok(match1);
assert.equal(match1.matches, 1);
// Match by windowTitle
const chain2 = parseSelectorChain('windowtitle=Untitled');
const match2 = findSelectorChainMatch(desktopNodes, chain2, { platform: 'linux' });
assert.ok(match2);
assert.equal(match2.matches, 1);
// Case-insensitive key (appName vs appname) and value
const chain3 = parseSelectorChain('appName=calculator');
const match3 = findSelectorChainMatch(desktopNodes, chain3, { platform: 'linux' });
assert.ok(match3);
assert.equal(match3.matches, 1);
});