Skip to content

Locators

A locator describes how to find content on the terminal grid rather than storing a fixed coordinate. Every read, wait, or click re-evaluates the locator against the latest screen snapshot, so animations and layout shifts do not leave stale cell positions behind.

By default, getByText matches exact substrings on the visible viewport. Enable regex for pattern matching, whitespace: "normalize" to collapse runs of horizontal whitespace, or full: true to search retained scrollback alongside the live screen.

await terminal.getByText("Save").expect();
await terminal.getByText("item \\d+", { regex: true }).expect();
await terminal
.getByText("Name Value", { whitespace: "normalize" })
.expect();

Cells carry foreground and background colors, underline style and color, boolean SGR attributes (bold, dim, italic, inverse, hidden, strikethrough, blink), and OSC 8 hyperlink targets.

Root selectors (terminal.getByStyle(...) and terminal.getByLink(uri)) find contiguous matching cell runs within each row. Whole-match style filters (locator.withStyle(...), chained locator.getByStyle(...) with the default "within" direction, or CLI --fg and --bold) require every non-blank cell of a candidate match to satisfy the style. Hyperlink constraints (getByLink(uri) or CLI --link URI) check the OSC 8 target across every cell in the match, including spaces; pass "" to require unlinked cells.

await terminal
.getByText("Deployed")
.withStyle({ foreground: "green", bold: true })
.expect();
await terminal.getByLink("https://tui-test.dev/docs/install/").click();

When a screen repeats the same label in multiple panels, narrow the search region instead of hard-coding an index. Relative locators search "within" (default), "after", or "before" a parent locator (parent.getByText("Save", { direction: "after" }) or child.relativeTo(parent, "after")). Text scope anchors bound a text selector between after and before text anchors, each supporting regex and occurrence ("unique" by default, "any", "first", "last", or zero-based nth).

const save = terminal.getByText("Save", {
scope: {
after: { text: "Network", occurrence: "unique" },
before: { text: "Advanced" },
},
});
await save.unique().click();

Combine locators from the same session to express intersections, unions, and containment:

  • and (and_ in Python) keeps cells matched by both operands and merges adjacent cells on each row into contiguous runs.
  • or (or_ in Python) combines matched cells from either operand into contiguous per-row runs without duplicates.
  • filter({ has, hasNot }) (has / has_not in Python) keeps whole candidate matches from the receiver that contain has and do not contain hasNot.

Apply scope, relativeTo, and whole-match withStyle to leaf selectors before combining them with and, or, or filter. Selection order matters: a.first().and(b) intersects only a’s first match with b, whereas a.and(b).first() takes the first run of their intersection.

const link = terminal.getByLink("https://example.com");
const bold = terminal.getByStyle({ bold: true });
const boldLinkRuns = bold.and(link);
const rowWithLink = terminal.getByText("Docs", { regex: false }).filter({ has: link });
await boldLinkRuns.first().expect();
await rowWithLink.expect();

On the CLI, find|resolve|wait|expect|click|highlight query accepts --query JSON or --query-file PATH using the full nested LocatorQuery schema; see the CLI reference.

Select occurrences, inspect spans, and act

Section titled “Select occurrences, inspect spans, and act”

Narrow multiple matches with unique(), first(), last(), nth(index) (zero-based), or any().

locations() (find on the CLI) returns all selected TextMatch spans without waiting, count() returns their count, and all() returns a locator per current match. location() (resolve on the CLI) requires a single resolved match.

expect() and wait() retry until the locator matches, or until it disappears when not: true / state: "hidden" is set. click() retries for a single match on the live viewport and clicks its middle cell. highlight() and highlightMatches() outline matches in screenshots and the live monitor.