HtmlLocator unstable
Locates elements by ARIA role and accessible name, computed from tags and attributes, so a test finds elements the way assistive technology does.
The same lookups run over a scene's view value, a TestHost tree and a
real DOM tree. role, label and placeholder return a Locator, so
they combine with Locator.inside on the first two. On the DOM, click,
type, keydown and submit act as the user would, through real DOM
events, and refuse an inert element.
Hidden elements (the hidden attribute or property, aria-hidden="true",
<input type="hidden">) and their descendants are excluded from role
queries, as are inert elements. hidden: true includes both, but does not
make inert elements interactive.
Inertness comes from inert on an element or ancestor, or being outside
the topmost modal. The last open modal in document order is treated as
topmost. DOM modal detection uses :modal, including open shadow roots;
some test environments never match it. DOM inertness follows shadow hosts
and slots, but queries do not search inside shadow roots.
On a structural tree a match is typed Locator.Node, since it can be any
node of the tree. On TestHost, root.all(HtmlLocator.role(...)) keeps the
host's node type. On the DOM a match is an HTMLElement.
Example Finding a button by its name
import { HtmlLocator } from "effect/ui/html/testing"
const tree = { tag: "form", children: [ { tag: "label", attrs: { for: "new" }, children: [{ text: "New todo" }] }, { tag: "input", attrs: { id: "new" } }, { tag: "button", attrs: { type: "submit" }, children: [{ text: "Add" }] } ]}
console.log(HtmlLocator.getByRole(tree, "button", { name: "Add" }) === tree.children[2]) // trueconsole.log(HtmlLocator.nameOf(tree, HtmlLocator.getByRole(tree, "textbox"))) // "New todo"Actions
Clicks element as the user would: a button submits its form, and a
checkbox or radio button changes and fires input and change.
Throws on an inert element.
Signature
declare function click(element: Element): voidPresses key on element: fires keydown, then keyup. Throws on an
inert element.
Signature
declare function keydown(element: Element, key: string, modifiers?: { readonly alt?: boolean; readonly ctrl?: boolean; readonly meta?: boolean; readonly shift?: boolean;}): voidSubmits form, or the form element belongs to, as pressing Enter in it
would. Throws on an inert element.
Signature
declare function submit(element: Element): voidTypes into a text field as the user would: replaces its value with value
and fires input, then change. Throws on an inert element.
Signature
declare function type(element: Element, value: string): voidGetters
Reads whether a checkbox or radio button is checked: aria-checked when
the element sets it, else an input's checked property, else its
checked attribute. It reads a scene's view, a TestHost tree or a DOM
element, and returns undefined for an element that can't be checked.
When to use
Use to check a box's live state. .checked(on) sets a property, so the
attributes of a story's node don't hold it. It's the state the checked
option of role matches.
See
- value for a field's text
Signature
declare function checked(node: unknown): boolean | undefinedExample Checking a done box
import { HtmlLocator } from "effect/ui/html/testing"
declare const story: { get: (locator: unknown) => unknown }const done = story.get(HtmlLocator.role("checkbox", { name: "Done" }))console.log(HtmlLocator.checked(done)) // truedialogState
unstableWhether a dialog is closed, open, or open as a modal. It reads a story's
view or a TestHost tree, where .modal(true) sets open to "modal",
or a DOM dialog, which is a modal when it matches :modal. A DOM without
:modal, such as some test environments, reports a modal as "open".
Signature
declare function dialogState(node: unknown): "open" | "closed" | "modal"Example Checking a confirm dialog
import { HtmlLocator } from "effect/ui/html/testing"
declare const story: { get: (locator: unknown) => unknown }const dialog = story.get(HtmlLocator.role("dialog", { name: "Delete note?" }))console.log(HtmlLocator.dialogState(dialog)) // "modal"The accessible name of node, computed within root: from
aria-labelledby, aria-label, its labels, alt, a legend or caption,
its content for roles named by their content, then title and
placeholder.
Signature
declare const nameOf: { (root: Node, node: Node): string; (root: Node, node: Node): string;}popoverState
unstableWhether a popover is showing. It reads a scene's view or a TestHost
tree, where .popover(mode, open) sets popoverOpen, or a DOM element,
through :popover-open.
Signature
declare function popoverState(node: unknown): "open" | "closed"Example Checking a menu
import { HtmlLocator } from "effect/ui/html/testing"
declare const story: { get: (locator: unknown) => unknown }const menu = story.get(HtmlLocator.role("menu", { hidden: true }))console.log(HtmlLocator.popoverState(menu)) // "closed"The role of node: its role attribute, else the implicit role of its tag
and attributes, else undefined. root is the tree it's in, which decides
the role of header and footer.
Signature
declare const roleOf: { (root: Node, node: Node): string | undefined; (root: Node, node: Node): string | undefined;}Reads a form field's current value: its value property, else its
value attribute. It reads a scene's view, a TestHost tree or a DOM
element, and returns undefined for an element with neither.
When to use
Use to check what a field shows. .value(text) sets a property, so the
attributes of a story's node don't hold it.
See
- checked for a checkbox or radio button
Signature
declare function value(node: unknown): string | undefinedExample Checking a search field
import { HtmlLocator } from "effect/ui/html/testing"
declare const story: { get: (locator: unknown) => unknown }const field = story.get(HtmlLocator.role("searchbox", { name: "Search" }))console.log(HtmlLocator.value(field)) // "effect"Locators
The form fields labelled text: by a <label> around them or naming
their id in for, by aria-labelledby, or by aria-label. The label's
text is trimmed and must equal text, or contain it with
{ exact: false }.
Signature
declare function label(text: string, options?: { readonly exact?: boolean;}): LocatorExample A field by its label
import { HtmlLocator } from "effect/ui/html/testing"import { Locator } from "effect/ui/testing"
const tree = { tag: "form", children: [ { tag: "label", attrs: { for: "zip" }, children: [{ text: "Zip code" }] }, { tag: "input", attrs: { id: "zip" } } ]}
console.log(Locator.get(tree, HtmlLocator.label("Zip code")) === tree.children[1]) // trueplaceholder
unstableThe elements whose placeholder is text.
Signature
declare function placeholder(text: string): LocatorA Locator for elements with role role and matching options, to
combine with Locator.inside and the other locators.
Signature
declare function role(role: AriaRole, options?: RoleOptions): LocatorModels
What a role query matches besides the role.
Details
name matches the accessible name exactly (after collapsing whitespace),
or tests it with a RegExp. checked matches a checkbox or radio button's
live state, expanded matches an explicit aria-expanded attribute
("true" or "false"), and level a heading's. hidden: true includes
hidden and inert elements for inspection; simulated user actions still
refuse inert targets.
Signature
interface RoleOptions { readonly checked?: boolean; readonly expanded?: boolean; readonly hidden?: boolean; readonly level?: number; readonly name?: string | RegExp;}Queries
The one element under root with role role and matching options.
Throws when there's none or several, listing the roles and names there are.
Signature
declare const getByRole: { (root: Node, role: AriaRole, options?: RoleOptions): HTMLElement; (root: Node, role: AriaRole, options?: RoleOptions): Node;}queryAllByRole
unstableEvery element under root, root included, with role role and matching
options, in document order.
Signature
declare const queryAllByRole: { (root: Node, role: AriaRole, options?: RoleOptions): readonly Array<HTMLElement>; (root: Node, role: AriaRole, options?: RoleOptions): readonly Array<Node>;}