Locator unstable
Locators over a minimal structural tree, so one locator works on a story's
view value and on TestHost's rendered tree alike.
A node is anything with some of tag, attrs, text and children:
an element has a tag, a text node has text. Locators match elements.
text matches the innermost elements whose text contains the string,
attr and className match by attribute, testId by a web
data-testid or a React Native testID, and inside narrows a locator
to the subtree of another. and and not combine locators, so
li.row:not(.selected) button is
inside(and(tag("li"), className("row"), not(className("selected"))), tag("button")).
Locators are pipeable, and inside and and have pipe forms.
Example Finding a row's button
import { Locator } from "effect/ui/testing"
const row = (id: string) => ({ tag: "li", attrs: { "data-testid": id }, children: [{ tag: "button", children: [{ text: "Save" }] }]})const tree = { tag: "ul", children: [row("row-1"), row("row-2")] }
const button = Locator.get(tree, Locator.inside(Locator.testId("row-2"), Locator.text("Save")))console.log(button === tree.children[1].children[0]) // true
const piped = Locator.text("Save").pipe(Locator.inside(Locator.testId("row-2")))console.log(Locator.get(tree, piped) === button) // trueCombinators
Elements every locator finds. self.pipe(Locator.and(that)) is
Locator.and(self, that).
Signature
declare const and: { (that: Locator): (self: Locator) => Locator; (self: Locator, that: Locator, ...rest: readonly Array<Locator>): Locator;}Elements under the root, the root included, that locator doesn't find.
Details
not(text("Save")) also finds the ancestors of the innermost match, since
text finds only that one.
Signature
declare function not(locator: Locator): LocatorConstructors
Events
Sends event with payload to node. The innermost element with a
handler for it, node or an ancestor, gets it.
Details
Payloads are plain data shaped by the vocabulary, such as { value } for
input. A scene's view value has no host, so scene tests use the
fire and click steps of Scene.Steps instead.
Signature
declare function fire(node: Firable, event: string, payload: unknown): voidGetters
Every element under root, root included, in document order.
Signature
declare function elements<N extends Node>(root: N): readonly Array<N>The text of a node and everything under it.
Signature
declare function textOf(node: Node): stringLocators
Elements with the attribute name, equal to or matching value when one
is given.
Details
A string matches the whole value, so attr("class", "row") skips
class="row selected"; use className for that. A prefix is /^prefix/.
Signature
declare function attr(name: string, value?: string | RegExp): LocatorElements whose class has the token name, split on ASCII whitespace as
in CSS.
Signature
declare function className(name: string): LocatorMatches of locator that are descendants of elements matched by container.
The container comes first in both forms: button.pipe(Locator.inside(form))
is Locator.inside(form, button).
Signature
declare const inside: { (container: Locator): (locator: Locator) => Locator; (container: Locator, locator: Locator): Locator;}Elements with the tag tag.
Signature
declare function tag(tag: string): LocatorElements whose data-testid attribute, or React Native testID prop,
equals id.
Signature
declare function testId(id: string): LocatorThe innermost elements whose text contains text, or equals it with
{ exact: true }.
Signature
declare function text(text: string, options?: { readonly exact?: boolean;}): LocatorModels
A node that can receive events, as TestHost's nodes can.
Signature
interface Firable extends Node { readonly fire: (event: string, payload: unknown) => void;}Finds elements under a root, in document order.
Signature
interface Locator extends Pipeable { readonly all: <N extends Node>(root: N) => readonly Array<N>; readonly description: string;}The structural tree every locator runs over.
Details
props holds the typed values set with .prop, such as a field's
value and checked, which aren't attributes. A scene's view value and a
TestHost tree both have them, and a vocabulary's readers, such as
HtmlLocator.value, read them.
Signature
interface Node { readonly attrs?: Readonly<Record<string, string | undefined>>; readonly children?: readonly Array<Node>; readonly props?: Readonly<Record<string, unknown>>; readonly tag?: string; readonly text?: string;}Queries
Every match, in document order.
Signature
declare function all<N extends Node>(root: N, locator: Locator): readonly Array<N>The one match. Throws when there's none or several.
Signature
declare function get<N extends Node>(root: N, locator: Locator): NThe one match, or undefined. Throws when there are several.
Signature
declare function query<N extends Node>(root: N, locator: Locator): N | undefined