Selectors

For AI agents: this page is Markdown at https://squiggle.sh/docs/flows/selectors.md. Every docs page is listed at https://squiggle.sh/llms.txt, and all of them are one file at https://squiggle.sh/llms-full.txt.

A selector tells a step which control to act on. Pick one that names what the control is, and the flow keeps finding it when the copy, the language or the layout changes.

Which selector to use

Pick something that happens to apps all the time, and see which selectors still find the Add to cart button:

The screen the flow was written against.

  1. testID{ testID: add-to-cart }Still found: finds the button
  2. text{ text: Add to cart }Still found: finds the button
  3. nth{ role: button, nth: 2 }Still found: the second button is Add to cart
  4. point{ point: "50%, 78%" }Still found: the point is on the button

Use a testID wherever you can. It is the only selector that survives a reword, a translation and a redesign. Text is the quickest to write and the first to break.

Selector fields

A selector must match exactly one control. From strongest to weakest:

FieldMatches
testIDThe control’s testID. The strongest choice.
sourceWhere the component is defined, such as LoginForm.tsx:42. Comes from the Babel plugin, or, in a development build without it, from your dev server, as in a React web app.
componentThe component’s name, such as CartRow.
roleThe accessibility role, such as button or tab. The role describe shows for a native control works too, both ways: AXButton also matches your app’s buttons, and button also matches the buttons iOS draws, such as an alert’s. AXImage and image, AXSwitch and switch, and AXSlider and slider pair the same way. So role: AXButton beside a label two of your buttons share matches both, and the step fails as ambiguous: narrow it with within or nth.
labelThe accessibility label.
textText on screen, or an accessibility label when nothing your app draws matches (see Combine fields).
callSiteThe exact line that renders this element, as describe reports it. For checking a line you edited; it never heals.
pointA position on the screen. Use it only when nothing else works.

On a React web app, testID matches a data-testid attribute, and label and role match aria-label and the element’s role. See React web apps.

Selectors mean the same in a flow, an intent program and the MCP tools: tap: { label: Checkout } taps the control labelled Checkout in all three. The MCP tools take testID, text, label, role, component, callSite, point, nth, the states checked, selected and expanded, the text filters textMatches and textNumber, and a ref from your agent’s last look; source, within and index work only in flows and intent programs. To name a step in the report, use note (see options on any step).

Tap a position

A point is written one of two ways, the same in a flow, an intent program and the MCP tools:

FormMeans
"120, 640"Logical points from the top-left corner of the screen.
"50%, 40%"Percentages of the screen’s width and height.
[0.5, 0.4]Fractions of the screen, from 0 to 1. The same place as "50%, 40%".

You can mix points and percentages in a string, such as "50%, 640". A list is always fractions: a value above 1, such as [120, 640], is refused, and the error tells you to write "120, 640" for points. Each axis is a plain number, and a percentage goes up to 100%. Any other string, such as "50%" or "120px, 640", is refused before the flow runs, and the error shows the forms above.

Combine fields

Fields in one selector all have to match. A testID with text beside it names the control and checks what it says, so this passes only while the counter reads exactly 3 items:

YAML
- assert: { visible: { testID: cart-count, text: "3 items" } }

In your app, text has to match a whole text, not part of it. A styled run inside a sentence counts as a text of its own, so text: fix is in place finds the bold words in The fix is in place and the test passes. When nothing in your app matches, Squiggle looks in the accessibility tree, comparing with each control’s accessibility label and ignoring case. One label often holds several pieces of text, so there a label of up to 80 characters that contains the text matches too, if no label equals it: text: sync-log finds the file picker’s sync-log, Text file, 78 bytes, a paragraph a native markdown view draws matches a phrase inside it, and text: Photo matches an icon button labelled Attach photo. This stops while text your React components show on the screen holds the text inside a longer string: then only a whole label matches. So notVisible: { text: Needs attention } passes while a card reads 1 needs attention, and tap does not tap that card either. Checks, waits, conditions and taps all match this way. A label always has to match the whole accessibility label, in any case.

Match what a text says

When the exact text changes from run to run, such as a frame counter or an order number, check what it says instead of the whole text. textNumber compares the number a text shows, and textMatches takes a regular expression:

YAML
- assert: { visible: { testID: skia-frames, textNumber: { gt: 0 } } }- assert: { visible: { testID: build-line, textMatches: "^Build \\d+$" } }- tap: { component: OrderRow, textMatches: "Order #10[0-9]{2}" }

Both narrow a control another field names, so put them beside a testID or another field; on their own, the flow is refused before it runs. A control matches when its text or its accessibility label passes the filter.

FilterMatches when
textNumberThe text holds exactly one number and it passes the comparison: eq, gt, gte, lt, lte, or in with a list. Skia frames 660 is 660, 12.5 ms is 12.5, and 1,234 rows is 1234. A number inside a word or a version, such as M0 or v2.1, does not count. A text with no number, or with several (3 of 10), matches nothing.
textMatchesThe regular expression is found anywhere in the text, in JavaScript syntax and case-sensitive. Add ^ and $ to match the whole text. In YAML double quotes, double each backslash: "\\d+"; in single quotes, write it once: '\d+'.

A visible check that fails says what the control reads, such as node c:12 reads "Skia frames 0". Prefer visible with the value you expect over notVisible with the one you don’t: notVisible: { testID: skia-frames, textNumber: { eq: 0 } } also passes when the counter is not on the screen at all. A waitFor on visible waits until the text says it, so it can wait for a counter to go above zero. A selector with a text filter is never healed. When it matches several controls, it follows the rules in the next section.

When a selector matches several controls

Narrow it with within, which looks only inside another control, or pick one by position with nth (counting from 1) or index (counting from 0):

YAML
- tap: { text: Remove, within: { testID: cart-row-2 } }- tap: { role: button, nth: 2 }

Prefer within. A position points at a different control as soon as one is added above it.

A step whose selector matches several controls fails and lists the matches, and is never healed onto one of them. Your agent’s tap refuses it the same way, and its refusal offers each match as the same selector with its nth, and its own ref, so your agent can pick one.

Match a control in one state

In a React web app, a selector can also say what state the control is in: checked for a checkbox, radio button or switch (true, false, or mixed for one that is partly checked), selected for an option, tab or row, and expanded for a section that opens and closes. Each matches the element’s ARIA state, or a checkbox’s own:

YAML
- tap: { testID: remember-me, checked: false }- assert: { visible: { testID: remember-me, checked: true } }

A tap with a state only taps the control while it is in that state, and a check that fails says which state the control is in. These states aren’t read from React Native components, so on iOS a selector with one matches nothing.

What a selector can see

Squiggle looks in two places: the components React rendered, and the accessibility tree. The second covers what React doesn’t draw, such as native tab bars, so a selector for a native tab works too. It also covers what iOS draws over your app, such as the photo picker and permission prompts: see Pickers, sheets and system alerts. A check like visible reads the current text, so a counter that changed since the screen opened still matches.

A selector also reads a control’s current name. When a button changes its testID, label, role or hint, or gains or loses its onPress, without being remounted, selectors see the new values from the next render. You don’t need a key to force a new control: a Next button that turns into Get started is found by its new name, and its old name no longer matches.

A control in a navigation header or an iOS toolbar is found where iOS draws it, by any selector. Expo Router’s Stack.Toolbar.Button takes no testID, so select it by its text or label.

In a react-native-pager-view pager (react-native-tab-view uses one too), only the page that shows is on screen. A control on another page isn’t visible, describe leaves it out, and a tap on it fails as not found. While the pager turns, both pages show until it settles, so after a tap that turns it, wait for the old page with waitFor: { notVisible: … } instead of asserting straight away.

A search field iOS draws for Stack.SearchBar is named by its placeholder while it is empty. Stack.SearchBar itself draws nothing, so component: StackSearchBar has nothing to tap: select the field instead. An icon inside a control, such as the field’s magnifier, counts as part of that control. Tap the toolbar’s search button to open the field, then type into the field by its placeholder. role: AXTextField is the role describe lists for the field, and keeps the step off the placeholder text inside it:

YAML
- tap: { label: Search }- type:    into: { label: Search tasks, role: AXTextField }    value: milk

Name a screen

route takes a screen’s full route, the name your agent’s status and the Screen map show, or its own name alone. Both must match exactly:

YAML
- assert: { route: shop/cart/detail }   # the full route, as status shows it- assert: { route: detail }             # the screen's own name

cart/detail doesn’t match shop/cart/detail. Use the full route when two screens could share a name. A bare index names your home screen only, never the first screen of a nested stack such as settings/index. Expo Router groups like (tabs) are part of the full route.

On a React web app, a route matches the page’s URL path. An Expo Router group never appears in a URL, so (tabs)/index names the page at /.

Make controls easy to select

A Pressable that wraps an icon and some text shows up as two separate elements, and neither one is the button. Give the control a role, a label and a testID, and it becomes one named thing:

SectionToggle.tsx
<Pressable  accessibilityRole="button"  accessibilityLabel={title}  testID="section-toggle">  <Chevron />  <Text>{title}</Text></Pressable>

squiggle legibility scores the screens your app has shown out of 100 and lists the controls that have no good selector, with their file and line. It starts with the edits that raise the score most, one per line of JSX, each with the score it leaves you at. squiggle legibility --fix adds the missing testIDs for you to review. In CI, pass --min-score 80 to fail below that score, and raise the number as you fix things.

To check that a VoiceOver user can reach every control a flow uses, run it with --ax-only, which finds each control through the accessibility tree alone. See Run settings. For a control that must not be pressed by an agent, add agentHint (see the SDK reference).