|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { buildNodes } from '../__tests__/test-utils/snapshot-builders.ts'; |
| 4 | +import { |
| 5 | + findSelectorChainMatch, |
| 6 | + listSelectorChainMatches, |
| 7 | + parseSelectorChain, |
| 8 | + resolveSelectorChain, |
| 9 | +} from './selectors.ts'; |
| 10 | + |
| 11 | +// Public-entry contract (#3180), through `./selectors.ts` rather than a deep |
| 12 | +// import: every node the winning alternative matches, on the same snapshot a |
| 13 | +// consumer applies its own strictness to. |
| 14 | + |
| 15 | +const loginNodes = () => |
| 16 | + buildNodes([ |
| 17 | + { index: 0, type: 'Window' }, |
| 18 | + { |
| 19 | + index: 1, |
| 20 | + type: 'Button', |
| 21 | + label: 'Continue', |
| 22 | + identifier: 'auth_continue', |
| 23 | + rect: { x: 0, y: 80, width: 200, height: 44 }, |
| 24 | + hittable: true, |
| 25 | + }, |
| 26 | + { index: 2, type: 'Text', label: 'Skip', rect: { x: 0, y: 124, width: 200, height: 20 } }, |
| 27 | + { |
| 28 | + index: 3, |
| 29 | + type: 'Button', |
| 30 | + label: 'Continue', |
| 31 | + identifier: 'secondary_continue', |
| 32 | + rect: { x: 0, y: 140, width: 200, height: 44 }, |
| 33 | + hittable: true, |
| 34 | + }, |
| 35 | + // Matched by default; refused when the caller requires geometry. |
| 36 | + { index: 4, type: 'Button', label: 'Continue', hittable: true }, |
| 37 | + ]); |
| 38 | + |
| 39 | +test('listSelectorChainMatches returns every node of the winning alternative, in snapshot order (#3180)', () => { |
| 40 | + const nodes = loginNodes(); |
| 41 | + const match = listSelectorChainMatches(nodes, parseSelectorChain('label="Continue"'), { |
| 42 | + platform: 'ios', |
| 43 | + }); |
| 44 | + assert.ok(match); |
| 45 | + assert.equal(match.selectorIndex, 0); |
| 46 | + assert.equal(match.selector.raw, 'label="Continue"'); |
| 47 | + assert.deepEqual( |
| 48 | + match.matchedNodes.map((node) => node.ref), |
| 49 | + ['e2', 'e4', 'e5'], |
| 50 | + ); |
| 51 | + // Identity, not a copy: consumers compare against their own snapshot. |
| 52 | + assert.equal(match.matchedNodes[0], nodes[1]); |
| 53 | +}); |
| 54 | + |
| 55 | +test('listSelectorChainMatches reports several matches where resolveSelectorChain refuses, from the same chain', () => { |
| 56 | + const nodes = loginNodes(); |
| 57 | + const chain = parseSelectorChain('label="Continue"'); |
| 58 | + assert.equal( |
| 59 | + resolveSelectorChain(nodes, chain, { platform: 'ios', requireUnique: true }), |
| 60 | + null, |
| 61 | + 'uniqueness refusal must not remove the matched nodes from the list', |
| 62 | + ); |
| 63 | + const match = listSelectorChainMatches(nodes, chain, { platform: 'ios' }); |
| 64 | + assert.equal(match?.matchedNodes.length, 3); |
| 65 | +}); |
| 66 | + |
| 67 | +test('listSelectorChainMatches walks to the first alternative that matches, as find does and a uniqueness-refusing resolve does not', () => { |
| 68 | + const nodes = loginNodes(); |
| 69 | + const match = listSelectorChainMatches( |
| 70 | + nodes, |
| 71 | + parseSelectorChain('label="Absent" || id=auth_continue'), |
| 72 | + { platform: 'ios' }, |
| 73 | + ); |
| 74 | + assert.ok(match); |
| 75 | + assert.equal(match.selectorIndex, 1); |
| 76 | + assert.deepEqual( |
| 77 | + match.matchedNodes.map((node) => node.ref), |
| 78 | + ['e2'], |
| 79 | + ); |
| 80 | + // Both alternatives match: `find` shares this answer, a default |
| 81 | + // `resolveSelectorChain` refuses ambiguous alternative 0 and names 1. |
| 82 | + assert.equal( |
| 83 | + findSelectorChainMatch(nodes, parseSelectorChain('label="Continue" || id=auth_continue'), { |
| 84 | + platform: 'ios', |
| 85 | + })?.selectorIndex, |
| 86 | + 0, |
| 87 | + ); |
| 88 | + assert.equal( |
| 89 | + resolveSelectorChain(nodes, parseSelectorChain('label="Continue" || id=auth_continue'), { |
| 90 | + platform: 'ios', |
| 91 | + })?.selectorIndex, |
| 92 | + 1, |
| 93 | + ); |
| 94 | + const both = listSelectorChainMatches( |
| 95 | + nodes, |
| 96 | + parseSelectorChain('label="Continue" || id=auth_continue'), |
| 97 | + { platform: 'ios' }, |
| 98 | + ); |
| 99 | + assert.equal(both?.selectorIndex, 0); |
| 100 | + assert.deepEqual( |
| 101 | + both?.matchedNodes.map((node) => node.ref), |
| 102 | + ['e2', 'e4', 'e5'], |
| 103 | + ); |
| 104 | + assert.equal( |
| 105 | + listSelectorChainMatches(nodes, parseSelectorChain('label="Absent"'), { platform: 'ios' }), |
| 106 | + null, |
| 107 | + ); |
| 108 | +}); |
| 109 | + |
| 110 | +test('listSelectorChainMatches honors requireRect against the same geometry the resolver sees', () => { |
| 111 | + const nodes = loginNodes(); |
| 112 | + const match = listSelectorChainMatches(nodes, parseSelectorChain('label="Continue"'), { |
| 113 | + platform: 'ios', |
| 114 | + requireRect: true, |
| 115 | + }); |
| 116 | + assert.deepEqual( |
| 117 | + match?.matchedNodes.map((node) => node.ref), |
| 118 | + ['e2', 'e4'], |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +test('listSelectorChainMatches lets requireRect change WHICH alternative wins, not just its members', () => { |
| 123 | + // `requireRect` removes candidates BEFORE alternative selection, so the walk |
| 124 | + // can move to a later alternative instead of reporting an empty first one. |
| 125 | + const nodes = buildNodes([ |
| 126 | + { index: 0, type: 'Button', label: 'NoRect' }, |
| 127 | + { index: 1, type: 'Button', identifier: 'foo', rect: { x: 0, y: 10, width: 10, height: 10 } }, |
| 128 | + ]); |
| 129 | + const chain = parseSelectorChain('label="NoRect" || id=foo'); |
| 130 | + assert.equal( |
| 131 | + listSelectorChainMatches(nodes, chain, { platform: 'ios' })?.selectorIndex, |
| 132 | + 0, |
| 133 | + 'without the geometry requirement the first alternative wins', |
| 134 | + ); |
| 135 | + const required = listSelectorChainMatches(nodes, chain, { |
| 136 | + platform: 'ios', |
| 137 | + requireRect: true, |
| 138 | + }); |
| 139 | + assert.equal(required?.selectorIndex, 1); |
| 140 | + assert.deepEqual( |
| 141 | + required?.matchedNodes.map((node) => node.ref), |
| 142 | + ['e2'], |
| 143 | + ); |
| 144 | +}); |
| 145 | + |
| 146 | +test('listSelectorChainMatches returns null for an empty chain and for a total miss', () => { |
| 147 | + const nodes = loginNodes(); |
| 148 | + // No alternative to win, and an empty match set is not representable. |
| 149 | + assert.equal( |
| 150 | + listSelectorChainMatches(nodes, { raw: '', selectors: [] }, { platform: 'ios' }), |
| 151 | + null, |
| 152 | + ); |
| 153 | +}); |
| 154 | + |
| 155 | +test('listSelectorChainMatches returns nodes in snapshot-array order, not index order', () => { |
| 156 | + // Array order, not `index` order: the answer follows the snapshot passed in. |
| 157 | + const nodes = buildNodes([ |
| 158 | + { index: 5, type: 'Button', label: 'M' }, |
| 159 | + { index: 2, type: 'Button', label: 'M' }, |
| 160 | + ]); |
| 161 | + assert.deepEqual( |
| 162 | + nodes.map((node) => node.ref), |
| 163 | + ['e1', 'e2'], |
| 164 | + ); |
| 165 | + const match = listSelectorChainMatches(nodes, parseSelectorChain('label="M"'), { |
| 166 | + platform: 'ios', |
| 167 | + }); |
| 168 | + assert.deepEqual( |
| 169 | + match?.matchedNodes.map((node) => node.ref), |
| 170 | + ['e1', 'e2'], |
| 171 | + ); |
| 172 | + assert.deepEqual( |
| 173 | + match?.matchedNodes.map((node) => node.index), |
| 174 | + [5, 2], |
| 175 | + ); |
| 176 | +}); |
| 177 | + |
| 178 | +test('listSelectorChainMatches uses agent-device term semantics (#3180)', () => { |
| 179 | + // `hittable` requires an explicit true, so the non-hittable row cannot lead. |
| 180 | + const hittableNodes = buildNodes([ |
| 181 | + { index: 0, type: 'Button', label: 'Refresh' }, |
| 182 | + { |
| 183 | + index: 1, |
| 184 | + type: 'Button', |
| 185 | + label: 'Refresh', |
| 186 | + rect: { x: 0, y: 40, width: 100, height: 40 }, |
| 187 | + hittable: true, |
| 188 | + }, |
| 189 | + ]); |
| 190 | + const hittable = listSelectorChainMatches( |
| 191 | + hittableNodes, |
| 192 | + parseSelectorChain('hittable=true label="Refresh"'), |
| 193 | + { platform: 'ios' }, |
| 194 | + ); |
| 195 | + assert.deepEqual( |
| 196 | + hittable?.matchedNodes.map((node) => node.ref), |
| 197 | + ['e2'], |
| 198 | + ); |
| 199 | + |
| 200 | + // `text` is extractNodeText — the FIRST non-empty of label/value/identifier — |
| 201 | + // so e2, whose label wins with a different value, does not match. |
| 202 | + const textNodes = buildNodes([ |
| 203 | + { index: 0, type: 'Button', label: 'Sign in' }, |
| 204 | + { index: 1, type: 'Button', label: 'Other', value: 'Sign in' }, |
| 205 | + ]); |
| 206 | + const text = listSelectorChainMatches(textNodes, parseSelectorChain('text="Sign in"'), { |
| 207 | + platform: 'android', |
| 208 | + }); |
| 209 | + assert.deepEqual( |
| 210 | + text?.matchedNodes.map((node) => node.ref), |
| 211 | + ['e1'], |
| 212 | + ); |
| 213 | + |
| 214 | + // `role` matches the NORMALIZED native class, not the raw type string. |
| 215 | + const roleNodes = buildNodes([ |
| 216 | + { index: 0, type: 'XCUIElementTypeButton', label: 'OK' }, |
| 217 | + { index: 1, type: 'XCUIElementTypeSwitch', label: 'Wi-Fi' }, |
| 218 | + ]); |
| 219 | + const role = listSelectorChainMatches(roleNodes, parseSelectorChain('role="button"'), { |
| 220 | + platform: 'ios', |
| 221 | + }); |
| 222 | + assert.deepEqual( |
| 223 | + role?.matchedNodes.map((node) => node.ref), |
| 224 | + ['e1'], |
| 225 | + ); |
| 226 | + |
| 227 | + // `editable` is fillable type + enabled: a TextView role="textbox" is not |
| 228 | + // fillable, and a disabled EditText is not enabled. |
| 229 | + const editableNodes = buildNodes([ |
| 230 | + { index: 0, type: 'android.widget.EditText', label: 'Name', enabled: true }, |
| 231 | + { index: 1, type: 'android.widget.TextView', label: 'Note', role: 'textbox' }, |
| 232 | + { index: 2, type: 'android.widget.EditText', label: 'Disabled', enabled: false }, |
| 233 | + ]); |
| 234 | + const editable = listSelectorChainMatches(editableNodes, parseSelectorChain('editable=true'), { |
| 235 | + platform: 'android', |
| 236 | + }); |
| 237 | + assert.deepEqual( |
| 238 | + editable?.matchedNodes.map((node) => node.ref), |
| 239 | + ['e1'], |
| 240 | + ); |
| 241 | + |
| 242 | + // `visible` is isNodeVisible: hittable OR a non-empty rect. e1 exercises the |
| 243 | + // hittable arm with a zero-size rect, e2 the rect arm while non-hittable, and |
| 244 | + // e3 has neither, which is what `hidden=true` names. |
| 245 | + const visibleNodes = buildNodes([ |
| 246 | + { |
| 247 | + index: 0, |
| 248 | + type: 'Button', |
| 249 | + label: 'Row', |
| 250 | + hittable: true, |
| 251 | + rect: { x: 0, y: 0, width: 0, height: 0 }, |
| 252 | + }, |
| 253 | + { index: 1, type: 'Button', label: 'Row', rect: { x: 0, y: 20, width: 50, height: 50 } }, |
| 254 | + { index: 2, type: 'Button', label: 'Row' }, |
| 255 | + ]); |
| 256 | + const visible = listSelectorChainMatches(visibleNodes, parseSelectorChain('visible=true'), { |
| 257 | + platform: 'ios', |
| 258 | + }); |
| 259 | + assert.deepEqual( |
| 260 | + visible?.matchedNodes.map((node) => node.ref), |
| 261 | + ['e1', 'e2'], |
| 262 | + ); |
| 263 | + const hidden = listSelectorChainMatches(visibleNodes, parseSelectorChain('hidden=true'), { |
| 264 | + platform: 'ios', |
| 265 | + }); |
| 266 | + assert.deepEqual( |
| 267 | + hidden?.matchedNodes.map((node) => node.ref), |
| 268 | + ['e3'], |
| 269 | + ); |
| 270 | +}); |
0 commit comments