--- title: Spec language reference --- # Spec language reference Lookup reference for everything importable from `@sanderling/spec`. For a worked example, read the [case study](../case-study/) first. ## Module structure A spec is a TypeScript module evaluated by the Go runner each step. It exports `properties` and `actionsRoot`, plus an optional `setup` and `generator`: ```ts import { ... } from "@sanderling/spec"; export const properties = { ... }; export const actionsRoot = weighted(...); export const setup = login; // optional export const generator = llm(...); // optional, see below ``` `setup` is an `ActionGenerator` the runner consults before `actionsRoot` each step. While it returns actions, they run; when it returns an empty list, the runner falls through to `actionsRoot`. Use it for preconditions like login and onboarding. If the app later regresses across the precondition (a logout mid-run), `setup` re-engages on its own. ## State Every extractor callback receives a `State`: ```ts interface State { ax: AccessibilityTree; snapshots: Record; lastAction: Action | null; logs: readonly LogEntry[]; exceptions: readonly ExceptionRecord[]; time: number; // ms since run start } ``` | Field | Description | |---|---| | `ax` | Live UI hierarchy for this step | | `snapshots` | Key-value data pushed by the app SDK (empty if SDK not integrated) | | `lastAction` | The action dispatched in the previous step, or `null` on the first step | | `logs` | Log entries collected since the previous step | | `exceptions` | Uncaught exceptions or `Sanderling.reportError()` calls since the previous step | | `time` | Milliseconds elapsed since the run started | ## Selectors Selectors are passed to `ax.find()`, `ax.findAll()`, and element-scoped `.find()` / `.findAll()`. ### String selectors | Form | Matches | |---|---| | `id:` | Exact match on resource-id, or element whose resource-id ends with `:id/` (Android) | | `idPrefix:` | Starts-with match on resource-id, matched against the whole id and against the local name after `:id/` (Android) | | `text:` | Substring match on text content | | `desc:` | Exact match on accessibility description; also matches when description starts with `, ` (iOS merged labels) | | `descPrefix:` | Starts-with match on accessibility description | | `:` | Substring match on any raw attribute by name | Boolean attributes (`"true"` / `"false"`) use exact match rather than substring. ### Object selectors Pass an object to apply multiple attribute filters with AND semantics: ```ts s.ax.find({ accessibilityText: "LoginScreen" }) s.ax.find({ testTag: "AccountCard", clickable: true }) ``` Every key-value pair must match. A key means the same thing here as in the string form: `id`, `desc`, `idPrefix` and `descPrefix` keep their matching rules, and every other key is an attribute name, with substring and boolean rules per attribute. Known attribute names are typed; you get autocomplete on `testTag`, `text`, `content-desc`, the boolean states (`clickable`, `enabled`, `focused`, `checked`, `selected`), and the cross-platform aliases (`identifier`, `accessibilityIdentifier`, `accessibilityText`, `accessibilityLabel`, `label`, `resource-id`, `class`, `elementType`, `package`, `placeholderValue`, `hintText`). Boolean state attributes accept a native `true` / `false`. Other attribute keys still type-check as a string-valued fallback so raw driver attributes remain reachable. A key that names neither an accepted selector key nor an attribute some element on screen carries fails the run, naming the key and the accepted list. Such a key can never match, and an empty result is indistinguishable from a screen with no matching element: the generator declines to act, the runner waits out the step, and the run ends clean having explored nothing. The string form keeps its open kind space, since `:` is the documented way to reach a raw driver attribute. ### Path selectors An array of object selectors matches a path: each segment is matched within the subtree of the previous match. Arrays work on the tree root and on element-scoped `.find`/`.findAll`. ```ts s.ax.find([{ testTag: "LoginScreen" }, { testTag: "LoginEmail" }]) s.ax.findAll([{ testTag: "HomeScreen" }, { testTag: "AccountCard" }]) ``` String selectors chain the same way with ` > `, but only on the tree root (`ax.find`, `ax.findAll`): ```ts s.ax.find("id:HomeScreen > descPrefix:account_card:") s.ax.find("id:LedgerScreen > desc:ledger_balance_display") ``` ### Cross-platform aliases These key aliases are resolved automatically so selectors work across platforms without changes: | Write this | Also checks | |---|---| | `content-desc` | `accessibilityText` | | `accessibilityText` | `content-desc` | | `label` | `accessibilityText` | | `accessibilityLabel` | `accessibilityText` | | `identifier` | `resource-id` | | `accessibilityIdentifier` | `resource-id` | ## AccessibilityElement fields Fields available on every element returned by `find` / `findAll`: | Field | Type | Description | |---|---|---| | `id` | `string` | resource-id (Android) or accessibility identifier (iOS) | | `text` | `string` | Visible text content | | `desc` | `string` | Accessibility description (`content-desc` / `accessibilityText`) | | `class` | `string` | View class (Android), element type (iOS), or HTML tag (web) | | `clickable` | `boolean` | Element is interactive | | `enabled` | `boolean` | Element is enabled | | `checked` | `boolean` | Checkbox or toggle state | | `focused` | `boolean` | Element has input focus | | `selected` | `boolean` | Selection state | | `bounds` | `{ left, top, right, bottom }` | Bounding box in device pixels | | `x` | `number` | Center X (derived from bounds) | | `y` | `number` | Center Y (derived from bounds) | | `attrs` | `Record` | All raw attributes from the driver | ## Platform notes ### Android - `id` maps to the Android resource-id (e.g., `com.example:id/button`). The `id:` selector matches by suffix after `:id/`, so `id:button` matches `com.example:id/button`. - `desc` maps to `content-desc`. - `class` is the Java view class name (e.g., `android.widget.TextView`). - `attrs` contains raw UIAutomator attributes: `package`, `scrollable`, `checkable`, etc. ### iOS - `id` maps to the `accessibilityIdentifier` set via `.accessibilityIdentifier` in SwiftUI/UIKit. - `desc` maps to `accessibilityText`, which the iOS sidecar builds by merging `accessibilityLabel` and the element's value (e.g., `"Close, icon description"`). The `desc:` selector handles this by also matching when the description starts with `, `. - `class` is the XCUITest element type (e.g., `XCUIElementTypeButton`). - `attrs` contains raw XCUITest attributes: `title`, `placeholderValue`, `hasFocus`, etc. ### Web (Chrome) - `id` maps to the HTML `id` attribute. - `desc` is derived from `aria-label`, `alt`, or `title`. - `class` is the lowercase HTML tag name (e.g., `button`, `input`). - `attrs` contains all HTML attributes available to CDP, keyed by the name the markup writes (`attrs["data-cents"]`, not `attrs.cents`). - `attrs.hintText` names an editable field the way a user reads it: its `aria-label`, the `