From e6eb985d1d34ba6ac9f4faa894292edac0be7509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 19 Nov 2026 11:26:06 +0100 Subject: [PATCH] feat(selectors): assert checked state with is checked and checked= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds “checked” to the selector boolean keys and the is predicate vocabulary so flows can assert the checked state of switches, checkboxes, and radios that snapshot nodes already publish. checked= is three-valued: checked=true matches only node.checked === true, checked=false only node.checked === false; iOS “mixed” and an absent fact (a non-checkable Android node) match neither. is checked passes only on an explicitly checked node and reports checked in its details JSON. Usage hint, positional rotation, command metadata, CLI help (via IS_PREDICATES / SELECTOR_KEY_NAMES), fuzz and property arbitraries, and the website selector docs are updated, including a note that Android id= matches the full pkg:id/name resource-id, React Native testIDs arrive bare, and Compose testTags need testTagsAsResourceId = true. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- .../src/interaction/metadata.ts | 2 +- packages/contracts/src/is-predicate.ts | 1 + .../__tests__/property-arbitraries.ts | 1 + packages/selectors/src/internal/match.test.ts | 93 ++++++++++++++++++- packages/selectors/src/internal/match.ts | 6 ++ packages/selectors/src/internal/parse.test.ts | 10 ++ packages/selectors/src/internal/parse.ts | 2 + .../selectors/src/internal/predicates.test.ts | 38 ++++++++ packages/selectors/src/internal/predicates.ts | 10 +- scripts/fuzz/arbitraries.ts | 1 + src/__tests__/cli-grammar.test.ts | 2 +- .../is-argument-surface-parity.test.ts | 1 + website/docs/docs/commands.md | 4 +- website/docs/docs/selectors.md | 11 ++- 14 files changed, 175 insertions(+), 7 deletions(-) diff --git a/packages/command-surface/src/interaction/metadata.ts b/packages/command-surface/src/interaction/metadata.ts index f6c4471845..0e71c3593f 100644 --- a/packages/command-surface/src/interaction/metadata.ts +++ b/packages/command-surface/src/interaction/metadata.ts @@ -80,7 +80,7 @@ const interactionCommandDescriptions = { scroll: 'Scroll in a direction, or toward the top/bottom edge of scrollable content. The until input scrolls to an off-screen selector in one command. The optional amount is the finger-path fraction of the viewport axis, honored up to 0.8 of it; directional scrolls reduce release momentum, while app scroll physics determine the final content offset. A visible keyboard shortens the swiped band instead of being dismissed; when too little is left, the command refuses with scroll_keyboard_occludes_surface. A directional scroll also reports the movement it observed as movement: moved, at-edge, unchanged, or unobserved when the two reads could not back a claim either way; an unchanged surface inside a container that still hides content in that direction refuses with scroll_no_progress rather than repeating the requested distance. The movement field is absent where a tier verifies per pass (top/bottom, until), where the runtime cannot read a screen, where a settle observation or a replay already owns that observation, or where the operator opted out of it.', get: 'Read text or accessibility attributes from a snapshot ref or selector without changing the app. The text format returns visible content; attrs returns the element attribute map.', - is: 'Check whether a selector satisfies a UI predicate such as visible, hidden, exists, absent, editable, selected, focused, or text. `absent` passes only when one readable, complete, settled, unscoped, full-depth accessibility capture has zero matches. This command checks the current condition without polling.', + is: 'Check whether a selector satisfies a UI predicate such as visible, hidden, exists, absent, editable, selected, checked, focused, or text. `absent` passes only when one readable, complete, settled, unscoped, full-depth accessibility capture has zero matches. This command checks the current condition without polling.', find: 'Find by text/label/value/role/id and run action; the action defaults to click. The role locator matches snapshot kind vocabulary (button, text, text-field, switch, ...); pre-reconciliation leaf spellings (statictext, edittext, textview) stay accepted on the nodes that carried them during a deprecation window. The focus and type actions aim at a text field: the match or the field holding it, even beside other fields, or else the one enabled on-screen field inside the match. Several enabled on-screen fields inside the match refuse with AMBIGUOUS_MATCH and the field candidates, and a disabled target refuses with reason target_disabled. Web text boxes are not recognized as text fields.', gesture: 'Perform a structured pan, fling, swipe, pinch, rotate, transform, tap, or drag gesture. Each gesture kind has its own inputs. A tap touches 2 to 5 points at once; count and pauseMs control repetition.', diff --git a/packages/contracts/src/is-predicate.ts b/packages/contracts/src/is-predicate.ts index 217d960c3b..086b49f2bc 100644 --- a/packages/contracts/src/is-predicate.ts +++ b/packages/contracts/src/is-predicate.ts @@ -6,6 +6,7 @@ export const IS_PREDICATES = [ 'absent', 'editable', 'selected', + 'checked', 'focused', 'text', ] as const; diff --git a/packages/selectors/src/internal/__tests__/property-arbitraries.ts b/packages/selectors/src/internal/__tests__/property-arbitraries.ts index ac89f8eeee..d577966190 100644 --- a/packages/selectors/src/internal/__tests__/property-arbitraries.ts +++ b/packages/selectors/src/internal/__tests__/property-arbitraries.ts @@ -13,6 +13,7 @@ const SELECTOR_KEY_VALUE_KINDS = { hidden: 'boolean', editable: 'boolean', selected: 'boolean', + checked: 'boolean', focused: 'boolean', enabled: 'boolean', hittable: 'boolean', diff --git a/packages/selectors/src/internal/match.test.ts b/packages/selectors/src/internal/match.test.ts index 09d8e7233c..38a0a03531 100644 --- a/packages/selectors/src/internal/match.test.ts +++ b/packages/selectors/src/internal/match.test.ts @@ -3,7 +3,11 @@ import { test } from 'vitest'; import assert from 'node:assert/strict'; import type { SnapshotState } from '@agent-device/contracts/snapshot-model'; import { parseSelectorChain } from './parse.ts'; -import { findSelectorChainMatch, resolveSelectorChain } from './resolve.ts'; +import { + findSelectorChainMatch, + listSelectorChainMatches, + resolveSelectorChain, +} from './resolve.ts'; import { loginFormNodes } from './__tests__/login-form-nodes.ts'; import { formatRole } from '@agent-device/kernel/snapshot'; @@ -215,6 +219,93 @@ test('focused selector matches snapshot focus state', () => { assert.equal(resolved.node.ref, 'tv2'); }); +// ── checked= key (switches, checkboxes, radios) ──────────────────────────── + +// `checked` is published only on checkable controls, so an Android node without the fact is not +// checkable, and iOS can publish 'mixed' (ios-node-interpretation.ts). Neither matches +// checked=true or checked=false; only an explicit true/false matches the same value. +const CHECKED_NODES: SnapshotState['nodes'] = [ + { + ref: 'sw1', + index: 0, + type: 'android.widget.Switch', + kind: formatRole('android.widget.Switch'), + label: 'Wi-Fi', + checked: true, + }, + { + ref: 'sw2', + index: 1, + type: 'android.widget.Switch', + kind: formatRole('android.widget.Switch'), + label: 'Bluetooth', + checked: false, + }, + { + ref: 'cb1', + index: 2, + type: 'XCUIElementTypeButton', + kind: formatRole('XCUIElementTypeButton'), + label: 'Select all', + // iOS React Native value semantics can carry 'mixed' where the wire type narrows to boolean. + checked: 'mixed' as unknown as boolean, + }, + { + ref: 'btn1', + index: 3, + type: 'android.widget.Button', + kind: formatRole('android.widget.Button'), + label: 'Continue', + // Android publishes no `checked` fact on a non-checkable control. + }, +]; + +function resolveChecked(selector: string) { + return resolveSelectorChain(CHECKED_NODES, parseSelectorChain(selector), { + platform: 'android', + requireUnique: true, + }); +} + +test('checked=true matches only an explicitly checked node, and a bare checked means true', () => { + assert.equal(resolveChecked('checked=true')?.node.ref, 'sw1'); + assert.equal(resolveChecked('checked')?.node.ref, 'sw1'); +}); + +test('checked=false matches only an explicitly unchecked node', () => { + assert.equal(resolveChecked('checked=false')?.node.ref, 'sw2'); +}); + +test('mixed and absent checked facts match neither checked=true nor checked=false', () => { + // Nearest negatives: the iOS 'mixed' checkbox (cb1) and the Android button without a + // `checked` fact (btn1) must not appear under either value, on either platform. + for (const platform of ['android', 'ios'] as const) { + for (const selector of ['checked=true', 'checked=false']) { + const listed = listSelectorChainMatches(CHECKED_NODES, parseSelectorChain(selector), { + platform, + }); + const refs = (listed?.matchedNodes ?? []).map((node) => node.ref); + assert.ok(!refs.includes('cb1'), `${selector} on ${platform} must not match the mixed node`); + assert.ok( + !refs.includes('btn1'), + `${selector} on ${platform} must not match the fact-less node`, + ); + } + } + assert.deepEqual( + listSelectorChainMatches(CHECKED_NODES, parseSelectorChain('checked=true'), { + platform: 'ios', + })?.matchedNodes.map((node) => node.ref), + ['sw1'], + ); + assert.deepEqual( + listSelectorChainMatches(CHECKED_NODES, parseSelectorChain('checked=false'), { + platform: 'ios', + })?.matchedNodes.map((node) => node.ref), + ['sw2'], + ); +}); + test('editable selector follows a published editable fact and infers from type without one', () => { const nodes: SnapshotState['nodes'] = [ { ref: 'e1', index: 0, type: 'com.example.RichEditor', label: 'Custom', editable: true }, diff --git a/packages/selectors/src/internal/match.ts b/packages/selectors/src/internal/match.ts index fe8a3446d8..cf326e9130 100644 --- a/packages/selectors/src/internal/match.ts +++ b/packages/selectors/src/internal/match.ts @@ -41,6 +41,12 @@ function matchesTerm( return isNodeEditable(node, platform) === Boolean(term.value); case 'selected': return Boolean(node.selected === true) === Boolean(term.value); + // `checked` is three-valued: it is published only on checkable controls (switch, checkbox, + // radio), so an absent fact means "not checkable or unavailable", not "unchecked", and iOS + // can publish 'mixed'. Both match neither `checked=true` nor `checked=false`; only an + // explicit true/false matches the same value. + case 'checked': + return term.value === true ? node.checked === true : node.checked === false; case 'focused': return Boolean(node.focused === true) === Boolean(term.value); case 'enabled': diff --git a/packages/selectors/src/internal/parse.test.ts b/packages/selectors/src/internal/parse.test.ts index 8617be027d..f7eb85ce7e 100644 --- a/packages/selectors/src/internal/parse.test.ts +++ b/packages/selectors/src/internal/parse.test.ts @@ -46,6 +46,16 @@ test('parseSelectorChain parses fallback and boolean terms', () => { assert.equal(chain.selectors[1]!.terms[2]!.key, 'visible'); }); +test('parseSelectorChain parses the checked boolean key in bare and key=value forms', () => { + assert.deepEqual(chainTerms('role=switch checked')[0], [ + { key: 'role', value: 'switch' }, + { key: 'checked', value: true }, + ]); + assert.deepEqual(chainTerms('checked=true')[0], [{ key: 'checked', value: true }]); + assert.deepEqual(chainTerms('checked=false')[0], [{ key: 'checked', value: false }]); + assert.throws(() => parseSelectorChain('checked=maybe'), /Invalid boolean value for checked/); +}); + 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); diff --git a/packages/selectors/src/internal/parse.ts b/packages/selectors/src/internal/parse.ts index 84cd0a2897..98fd73af6c 100644 --- a/packages/selectors/src/internal/parse.ts +++ b/packages/selectors/src/internal/parse.ts @@ -12,6 +12,7 @@ export type SelectorKey = | 'hidden' | 'editable' | 'selected' + | 'checked' | 'focused' | 'enabled' | 'hittable'; @@ -45,6 +46,7 @@ const BOOLEAN_KEYS = new Set([ 'hidden', 'editable', 'selected', + 'checked', 'focused', 'enabled', 'hittable', diff --git a/packages/selectors/src/internal/predicates.test.ts b/packages/selectors/src/internal/predicates.test.ts index 9fa9d33bb4..aa6df315bb 100644 --- a/packages/selectors/src/internal/predicates.test.ts +++ b/packages/selectors/src/internal/predicates.test.ts @@ -347,6 +347,43 @@ test('the text predicate never consults the visibility index', () => { assert.deepEqual(materialized, { nodeMap: 0, viewportRects: 0 }); }); +test('checked predicate passes only on an explicitly checked node', () => { + const base: SnapshotNode = { + index: 0, + ref: 'e1', + type: 'android.widget.Switch', + kind: formatRole('android.widget.Switch'), + label: 'Wi-Fi', + }; + const evaluate = (node: SnapshotNode) => + evaluateIsPredicate({ + predicate: 'checked', + node, + visibility: createSnapshotVisibility([node]), + platform: 'android', + }); + + const on = evaluate({ ...base, checked: true }); + assert.equal(on.pass, true); + assert.match(on.details, /"checked":true/); + + // Closest negatives: an explicitly unchecked switch, the iOS 'mixed' state + // (ios-node-interpretation.ts), and an Android node without a `checked` fact + // (not checkable) all fail `is checked`. + assert.equal(evaluate({ ...base, checked: false }).pass, false); + assert.equal(evaluate({ ...base, checked: 'mixed' as unknown as boolean }).pass, false); + assert.equal(evaluate(base).pass, false); +}); + +test('normalizeIsPositionals rotates a trailing checked token into the predicate slot', () => { + assert.deepEqual(normalizeIsPositionals(['role=switch', 'checked']), ['checked', 'role=switch']); + // Inside the selector it stays a boolean filter term. + assert.deepEqual(normalizeIsPositionals(['checked', 'role=switch checked=false']), [ + 'checked', + 'role=switch checked=false', + ]); +}); + test('an unsupported is predicate always carries the usage hint', () => { const refused = checkIsPredicate('nope'); expect(refused).toEqual({ @@ -360,4 +397,5 @@ test('an unsupported is predicate always carries the usage hint', () => { test('is predicates are admitted case-insensitively, matching what the daemon executes', () => { expect(checkIsPredicate('TEXT')).toEqual({ ok: true, predicate: 'text' }); expect(checkIsPredicate('Visible')).toEqual({ ok: true, predicate: 'visible' }); + expect(checkIsPredicate('CHECKED')).toEqual({ ok: true, predicate: 'checked' }); }); diff --git a/packages/selectors/src/internal/predicates.ts b/packages/selectors/src/internal/predicates.ts index a85f48d177..20dca9167f 100644 --- a/packages/selectors/src/internal/predicates.ts +++ b/packages/selectors/src/internal/predicates.ts @@ -40,9 +40,9 @@ export function checkIsPredicate( } export const IS_PREDICATE_USAGE_HINT = - 'Use "is " or "is ". visible|hidden|editable|selected|focused double as selector keys: a bare predicate token after the selector is read as the predicate, so write key=true (e.g. visible=true) inside the selector to use it as a filter instead.'; + 'Use "is " or "is ". visible|hidden|editable|selected|checked|focused double as selector keys: a bare predicate token after the selector is read as the predicate, so write key=true (e.g. visible=true) inside the selector to use it as a filter instead.'; -// visible|hidden|editable|selected|focused double as selector boolean keys, so the selector-first +// visible|hidden|editable|selected|checked|focused double as selector boolean keys, so the selector-first // form (`is `) cannot survive greedy selector parsing: the trailing // predicate token would be swallowed as a boolean selector term. Reserve the first bare // predicate token that terminates a valid selector prefix and rotate the positionals into @@ -75,6 +75,8 @@ export function evaluateIsPredicate(params: { const actualText = extractNodeText(node); const editable = isNodeEditable(node, platform); const selected = node.selected === true; + // iOS can publish 'mixed' (ios-node-interpretation.ts); like an absent fact it is not checked. + const checked = node.checked === true; const focused = node.focused === true; const visible = predicate === 'text' ? isNodeVisible(node) : isAssertionVisible(node, visibility, platform); @@ -92,6 +94,9 @@ export function evaluateIsPredicate(params: { case 'selected': pass = selected; break; + case 'checked': + pass = checked; + break; case 'focused': pass = focused; break; @@ -108,6 +113,7 @@ export function evaluateIsPredicate(params: { visible, editable, selected, + checked, focused, })}`; return { pass, actualText, details }; diff --git a/scripts/fuzz/arbitraries.ts b/scripts/fuzz/arbitraries.ts index 39063a246f..0321d3fd4f 100644 --- a/scripts/fuzz/arbitraries.ts +++ b/scripts/fuzz/arbitraries.ts @@ -43,6 +43,7 @@ const BOOLEAN_SELECTOR_KEYS = [ 'hidden', 'editable', 'selected', + 'checked', 'focused', 'enabled', 'hittable', diff --git a/src/__tests__/cli-grammar.test.ts b/src/__tests__/cli-grammar.test.ts index 0b769de7d1..baafb62300 100644 --- a/src/__tests__/cli-grammar.test.ts +++ b/src/__tests__/cli-grammar.test.ts @@ -181,7 +181,7 @@ test('is grammar explains the predicate/selector-key collision on invalid predic assert.equal(err.code, 'INVALID_ARGS'); assert.match( err.message, - /is requires predicate: visible\|hidden\|exists\|absent\|editable\|selected\|focused\|text/, + /is requires predicate: visible\|hidden\|exists\|absent\|editable\|selected\|checked\|focused\|text/, ); assert.match(err.details?.hint ?? '', /is /); assert.match(err.details?.hint ?? '', /visible=true/); diff --git a/src/__tests__/is-argument-surface-parity.test.ts b/src/__tests__/is-argument-surface-parity.test.ts index 859eacf267..afe3a283e7 100644 --- a/src/__tests__/is-argument-surface-parity.test.ts +++ b/src/__tests__/is-argument-surface-parity.test.ts @@ -29,6 +29,7 @@ const EXPECTED_IS_PREDICATES = [ 'absent', 'editable', 'selected', + 'checked', 'focused', 'text', ] as const; diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 4ed3299301..f11e671c11 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -641,11 +641,13 @@ agent-device is absent 'label="Loading..."' agent-device is hidden 'text="Loading..."' agent-device is editable 'id="email"' agent-device is selected 'label="Wi-Fi"' +agent-device is checked 'role=switch label="Wi-Fi"' agent-device is text 'id="greeting"' "Welcome back" ``` - `is` evaluates UI predicates against a selector expression and exits non-zero on failure. It takes selectors, not refs like `@e3`, and the same snapshot flags as `click`, `fill`, `get`, and `wait`. -- Supported predicates are `visible`, `hidden`, `exists`, `absent`, `editable`, `selected`, `focused`, and `text`. +- Supported predicates are `visible`, `hidden`, `exists`, `absent`, `editable`, `selected`, `checked`, `focused`, and `text`. +- `is checked` passes only on a checkable control (switch, checkbox, radio) whose snapshot state is explicitly checked; an iOS `mixed` state or a control with no checked state fails it. - `is visible` passes when the element is in the visible viewport, or has no rect of its own but sits inside a visible ancestor. `is exists` only checks that the selector matches. - `is text ` compares the element's text with the value. `wait text` checks that text is present, not that it can be tapped. - `is absent` passes only when the selector has zero matches in one readable, complete, settled, unscoped, full-depth capture. It does not mean hidden: `--scope` and `--depth` are rejected, and sparse, unreadable, truncated, unsettled, or loading captures fail. diff --git a/website/docs/docs/selectors.md b/website/docs/docs/selectors.md index ebbde6e5b4..02e557bb47 100644 --- a/website/docs/docs/selectors.md +++ b/website/docs/docs/selectors.md @@ -97,9 +97,18 @@ Text keys: Text values match the whole field, ignoring case and extra whitespace: `label="sign in"` matches `Sign In` but not `Sign In Now`. For partial matches, use `find`. -Boolean keys: `visible`, `hidden`, `editable`, `selected`, `focused`, `enabled`, `hittable`. Write +On Android, `id=` matches the element's full resource-id, which keeps its `pkg:id/name` form for +native XML ids, while a React Native `testID` arrives bare (`id="login"`). A Jetpack Compose +`Modifier.testTag` is exposed to agent-device only when the app sets +`semantics { testTagsAsResourceId = true }`; then `id=""` matches it. + +Boolean keys: `visible`, `hidden`, `editable`, `selected`, `checked`, `focused`, `enabled`, `hittable`. Write `visible=true` or `visible=false`; a bare `visible` means `visible=true`. +`checked` (and `is checked`) reads the state of a checkable control (switch, checkbox, radio). +`checked=true` matches only an explicitly checked control and `checked=false` only an explicitly +unchecked one; an iOS `mixed` state and a control with no checked state match neither. + `editable` (and `is editable`) uses the node's native `editable` fact when the snapshot carries one, so a custom text view can match and a read-only field cannot. Without the fact it is inferred from the element type. A disabled element is never editable.