Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/command-surface/src/interaction/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/is-predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const IS_PREDICATES = [
'absent',
'editable',
'selected',
'checked',
'focused',
'text',
] as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const SELECTOR_KEY_VALUE_KINDS = {
hidden: 'boolean',
editable: 'boolean',
selected: 'boolean',
checked: 'boolean',
focused: 'boolean',
enabled: 'boolean',
hittable: 'boolean',
Expand Down
93 changes: 92 additions & 1 deletion packages/selectors/src/internal/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 },
Expand Down
6 changes: 6 additions & 0 deletions packages/selectors/src/internal/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
10 changes: 10 additions & 0 deletions packages/selectors/src/internal/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/selectors/src/internal/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type SelectorKey =
| 'hidden'
| 'editable'
| 'selected'
| 'checked'
| 'focused'
| 'enabled'
| 'hittable';
Expand Down Expand Up @@ -45,6 +46,7 @@ const BOOLEAN_KEYS = new Set<SelectorKey>([
'hidden',
'editable',
'selected',
'checked',
'focused',
'enabled',
'hittable',
Expand Down
38 changes: 38 additions & 0 deletions packages/selectors/src/internal/predicates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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' });
});
10 changes: 8 additions & 2 deletions packages/selectors/src/internal/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export function checkIsPredicate(
}

export const IS_PREDICATE_USAGE_HINT =
'Use "is <predicate> <selector>" or "is <selector> <predicate>". 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 <predicate> <selector>" or "is <selector> <predicate>". 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 <selector> <predicate>`) 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
Expand Down Expand Up @@ -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);
Expand All @@ -92,6 +94,9 @@ export function evaluateIsPredicate(params: {
case 'selected':
pass = selected;
break;
case 'checked':
pass = checked;
break;
case 'focused':
pass = focused;
break;
Expand All @@ -108,6 +113,7 @@ export function evaluateIsPredicate(params: {
visible,
editable,
selected,
checked,
focused,
})}`;
return { pass, actualText, details };
Expand Down
1 change: 1 addition & 0 deletions scripts/fuzz/arbitraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const BOOLEAN_SELECTOR_KEYS = [
'hidden',
'editable',
'selected',
'checked',
'focused',
'enabled',
'hittable',
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/cli-grammar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <selector> <predicate>/);
assert.match(err.details?.hint ?? '', /visible=true/);
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/is-argument-surface-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const EXPECTED_IS_PREDICATES = [
'absent',
'editable',
'selected',
'checked',
'focused',
'text',
] as const;
Expand Down
4 changes: 3 additions & 1 deletion website/docs/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <selector> <value>` 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.
Expand Down
11 changes: 10 additions & 1 deletion website/docs/docs/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="<tag>"` 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.
Expand Down
Loading