From 88f2b2a59de25aa61052117fbc75cf50aec80b23 Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 18:24:42 +0530 Subject: [PATCH 1/2] fix(help): give every selector key a rule and list every selector command A term can be a bare boolean key, checked is three-valued, appname and windowtitle say which snapshots report them, and the command lists add gesture pinch and screenshot --crop-on (selectors.md also adds click, hover and gesture drag). --- .../schema/cli-help-first-screen.test.ts | 2 +- src/commands/schema/cli-help-selectors.ts | 8 ++++--- website/docs/docs/selectors.md | 23 ++++++++++++++----- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/commands/schema/cli-help-first-screen.test.ts b/src/commands/schema/cli-help-first-screen.test.ts index 7197ef9640..44214bf501 100644 --- a/src/commands/schema/cli-help-first-screen.test.ts +++ b/src/commands/schema/cli-help-first-screen.test.ts @@ -57,7 +57,7 @@ const FIRST_SCREEN_RULES: Record = { 'Rare iOS accessibility gap: a row shown disabled/hittable:false where press reports success but no UI change, or a collapsed composite control with no child refs -- run snapshot -i --json, compute the target center from rects, press x y, then diff snapshot -i. Coordinates are fallback-only; document why you used them.', ], selectors: [ - 'One or more key=value terms separated by spaces. An element must match every term', + 'One or more terms separated by spaces. An element must match every term', 'A role word is not a key: write role=button label="Save", not button="Save"', 'Text values match the whole field, ignoring case and extra whitespace', ], diff --git a/src/commands/schema/cli-help-selectors.ts b/src/commands/schema/cli-help-selectors.ts index b50c7c7196..ff462b31e9 100644 --- a/src/commands/schema/cli-help-selectors.ts +++ b/src/commands/schema/cli-help-selectors.ts @@ -4,10 +4,10 @@ export const selectorsHelpTopic = { summary: 'Selector syntax: keys, quoting, alternatives, and matching rules', body: `agent-device help selectors -Selector syntax for commands that take a target. press, click, fill, longpress, hover, get, is, wait, scroll --until, and gesture drag accept a selector wherever they accept an @ref; find accepts one as its query. +Selector syntax for commands that take a target. press, click, fill, longpress, hover, get, is, and wait take a selector as the target, gesture drag as its source and destination, and gesture pinch as the element to pinch. find takes one as its query, and scroll --until and screenshot --crop-on as the flag value. Shape: - One or more key=value terms separated by spaces. An element must match every term. Wrap the whole expression in single quotes so the shell passes it as one argument: + One or more terms separated by spaces. An element must match every term. A term is key=value, or a boolean key alone, which means key=true: checked is checked=true. Wrap the whole expression in single quotes so the shell passes it as one argument: agent-device press 'role=button label="Continue"' agent-device fill 'label="Email" editable=true' "qa@example.com" agent-device wait 'id="checkout-total"' 5000 @@ -19,8 +19,10 @@ Shape: Matching: id is the identifier (test ID, resource ID, accessibility identifier); label and value are the accessibility label and value; text is the first non-empty of label, value, and identifier; role is the role in brackets in snapshot output (button, text-field, switch). + appname and windowtitle are the owning app's name and window title. Linux snapshots and the macOS desktop, frontmost-app, and menubar surfaces report them; a macOS app session reports them only with AGENT_DEVICE_MACOS_APP_BACKEND=native. Elsewhere a non-empty value matches nothing. Text values match the whole field, ignoring case and extra whitespace: label="sign in" matches Sign In, not Sign In Now. For a partial match, use find text "Sign" click. - Boolean keys take true or false, and a bare key means true: visible=false, editable=true, checked. + Boolean keys take true or false: visible=false, editable=true. visible, hidden, editable, selected, focused, enabled, and hittable test the element state of that name. + checked tests a switch, checkbox, or radio. checked=true matches only an explicitly checked control and checked=false only an explicitly unchecked one; an iOS mixed state and an element with no checked state match neither. On Android, id= matches the full resource-id (pkg:id/name for native XML ids); a React Native testID arrives bare (id="login"). Matches in distinct subtrees make a mutating command fail with AMBIGUOUS_MATCH and a candidate list. Retry with one candidate ref, or narrow the selector with role, id, or longer text. diff --git a/website/docs/docs/selectors.md b/website/docs/docs/selectors.md index 0a9d28a675..fd5a49fba7 100644 --- a/website/docs/docs/selectors.md +++ b/website/docs/docs/selectors.md @@ -71,10 +71,17 @@ Tips: ## Selector syntax -A selector expression is one or more `key=value` terms separated by spaces. An element must match -every term. Commands that take a target, such as `press`, `fill`, `longpress`, `get`, `is`, `wait`, -and `scroll --until`, accept a selector wherever they accept an `@ref`. `find` accepts one as its -query. `agent-device help selectors` prints this syntax. +A selector expression is one or more terms separated by spaces, and an element must match every +term. A term is `key=value`, or a boolean key alone, which means `key=true`: `checked` is +`checked=true`. `agent-device help selectors` prints this syntax. + +These commands take a selector: + +- `press`, `click`, `fill`, `longpress`, `hover`, `get`, `is`, and `wait`, as the target +- `gesture drag`, as its source and destination +- `gesture pinch`, as the element to pinch +- `find`, as its query +- `scroll --until` and `screenshot --crop-on`, as the flag value ```bash agent-device press 'role=button label="Continue"' @@ -93,8 +100,12 @@ Text keys: | `value` | The accessibility value | | `text` | The first non-empty of label, value, and identifier | | `role` | The role shown in brackets in snapshot output (`button`, `text-field`, `switch`, …) | -| `appname` | The owning app's name, on desktop targets that report it | -| `windowtitle` | The owning window's title, on desktop targets that report it | +| `appname` | The owning app's name | +| `windowtitle` | The owning window's title | + +Linux snapshots and the macOS `desktop`, `frontmost-app`, and `menubar` surfaces report `appname` +and `windowtitle`. A macOS app session reports them only with `AGENT_DEVICE_MACOS_APP_BACKEND=native`. +Elsewhere a non-empty value matches nothing. 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`. From 23d94bfc7bfffaf12dcad8846271e2caa1fd2dae Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 18:24:42 +0530 Subject: [PATCH 2/2] test(help): run the selector help and docs examples through the parser --- .../schema/cli-help-selectors.test.ts | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/commands/schema/cli-help-selectors.test.ts diff --git a/src/commands/schema/cli-help-selectors.test.ts b/src/commands/schema/cli-help-selectors.test.ts new file mode 100644 index 0000000000..30daeeffdf --- /dev/null +++ b/src/commands/schema/cli-help-selectors.test.ts @@ -0,0 +1,178 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { test } from 'vitest'; +import { SELECTOR_KEY_NAMES } from '@agent-device/selectors'; +import type { SnapshotNode } from '@agent-device/kernel/snapshot'; +import { listSelectorChainMatches, parseSelectorChain } from '../../sdk/selectors.ts'; +import { selectorsHelpTopic } from './cli-help-selectors.ts'; + +// `help selectors` and selectors.md state the selector rules in hand-written prose. These tests pin +// the selectors those texts show: each command example parses, each rule example still appears in +// both texts and gets the result the test expects from the parser and matcher, and each parser key +// is the subject of a rule. They do not read the prose around an example, so a reworded rule that +// keeps its example is not caught. + +const HELP = selectorsHelpTopic.body; +const DOC = fs.readFileSync( + path.join(import.meta.dirname, '../../../website/docs/docs/selectors.md'), + 'utf8', +); +const SOURCES = { 'help selectors': HELP, 'selectors.md': DOC } as const; + +/** The single-quoted selector of each `agent-device ...` example line. */ +function commandExampleSelectors(text: string): string[] { + return text + .split('\n') + .map((line) => line.trim()) + .filter((line) => line.startsWith('agent-device ')) + .flatMap((line) => [...line.matchAll(/'([^']*=[^']*)'/g)].map(([, selector]) => selector!)); +} + +function labels(nodes: SnapshotNode[], expression: string): Array { + const match = listSelectorChainMatches(nodes, parseSelectorChain(expression), { + platform: 'ios', + }); + return match?.matchedNodes.map((node) => node.label) ?? []; +} + +function node(index: number, fields: Partial): SnapshotNode { + return { index, ref: `e${index + 1}`, ...fields }; +} + +test.each(Object.entries(SOURCES))('every command example selector in %s parses', (_, text) => { + const selectors = commandExampleSelectors(text); + assert.ok(selectors.length >= 4, `expected command examples, found ${selectors.length}`); + for (const selector of selectors) { + assert.doesNotThrow(() => parseSelectorChain(selector), selector); + } +}); + +/** The keys a rule clause is about: `id is ...`, `label and value are ...`, `a, b, and c test ...`. */ +function ruleSubjects(text: string): Set { + const clauses = text.split('\n').flatMap((line) => line.trim().split(/[;.] /)); + const subjects = clauses.flatMap( + (clause) => + /^([a-z]+(?:, [a-z]+)*(?:,? and [a-z]+)?) (?:is|are|tests?) /.exec(clause)?.[1] ?? [], + ); + return new Set(subjects.flatMap((subject) => subject.split(/,? and |, /))); +} + +// The Keys line is generated from the parser, so a new key shows up there with no rule. Each key +// must open a rule clause in the Matching section. +test('every selector key opens a rule clause in the help Matching section', () => { + const matching = HELP.slice(HELP.indexOf('Matching:')); + const subjects = ruleSubjects(matching); + for (const key of SELECTOR_KEY_NAMES) { + assert.ok(subjects.has(key), `no Matching clause starts with ${key}`); + } +}); + +// selectors.md gives a text key a table row and a boolean key a place on the Boolean keys line. +test('every selector key has a table row or a Boolean keys entry in selectors.md', () => { + const booleanLine = DOC.split('\n').find((line) => line.startsWith('Boolean keys:')) ?? ''; + for (const key of SELECTOR_KEY_NAMES) { + const row = DOC.split('\n').some((line) => line.startsWith(`| \`${key}\` `)); + assert.ok(row || booleanLine.includes(`\`${key}\``), `${key} has no rule in selectors.md`); + } +}); + +const RULE_EXAMPLES: ReadonlyArray<{ example: string; check: (example: string) => void }> = [ + { + // A bare boolean key is a term of its own. + example: 'checked', + check: (example) => + assert.deepEqual(parseSelectorChain(example).selectors[0]?.terms, [ + { key: 'checked', value: true }, + ]), + }, + { + example: 'visible=false', + check: (example) => + assert.deepEqual(parseSelectorChain(example).selectors[0]?.terms, [ + { key: 'visible', value: false }, + ]), + }, + { + example: 'role=button label="Save"', + check: (example) => + assert.deepEqual(parseSelectorChain(example).selectors[0]?.terms, [ + { key: 'role', value: 'button' }, + { key: 'label', value: 'Save' }, + ]), + }, + { + example: 'button="Save"', + check: (example) => assert.throws(() => parseSelectorChain(example), /Unknown selector key/), + }, + { + example: 'label="Say \\"hi\\""', + check: (example) => + assert.deepEqual(parseSelectorChain(example).selectors[0]?.terms, [ + { key: 'label', value: 'Say "hi"' }, + ]), + }, + { + example: 'label="sign in"', + check: (example) => + assert.deepEqual( + labels([node(0, { label: 'Sign In' }), node(1, { label: 'Sign In Now' })], example), + ['Sign In'], + ), + }, + { + // Neither an unchecked control nor one without a checked state matches checked=true. + example: 'checked=true', + check: (example) => + assert.deepEqual( + labels( + [ + node(0, { label: 'On', checked: true }), + node(1, { label: 'Off', checked: false }), + node(2, { label: 'Plain' }), + ], + example, + ), + ['On'], + ), + }, + { + example: 'checked=false', + check: (example) => + assert.deepEqual( + labels( + [ + node(0, { label: 'On', checked: true }), + node(1, { label: 'Off', checked: false }), + node(2, { label: 'Plain' }), + ], + example, + ), + ['Off'], + ), + }, + { + // The left alternative wins when both resolve; the right one is used when the left finds nothing. + example: 'id="submit" || label="Submit order"', + check: (example) => { + const both = [node(0, { label: 'Submit order' }), node(1, { identifier: 'submit' })]; + const chain = parseSelectorChain(example); + assert.equal(listSelectorChainMatches(both, chain, { platform: 'ios' })?.selectorIndex, 0); + const labelOnly = [node(0, { label: 'Submit order' })]; + assert.equal( + listSelectorChainMatches(labelOnly, chain, { platform: 'ios' })?.selectorIndex, + 1, + ); + }, + }, +]; + +test.each(RULE_EXAMPLES)( + '$example appears in both texts and parses or matches as expected', + ({ example, check }) => { + for (const [source, text] of Object.entries(SOURCES)) { + assert.ok(text.includes(example), `${source} no longer shows ${example}`); + } + check(example); + }, +);