Locators
The four locator factories and how to build resilient target contracts.
Locators are how a target resolves to a DOM element at capture time. Democraft provides four factories in @democraft/core.
The four factories
byRole(role, options?)
The most resilient locator. Backed by ARIA role and accessible name — survives markup rewrites that preserve semantics.
| Prop | Type | Default |
|---|---|---|
name | | - |
type | | - |
byLabel(text)
Finds a form control by its associated <label> text. Resilient across input refactors.
byTestId(id)
Finds an element by its data-testid attribute. The reliable fallback for elements without a clear accessible name.
byText(text, options?)
Finds an element by visible text content. The most fragile locator — use only when no other option fits.
Grouping targets: defineTargets
Most demos declare all targets in one map. defineTargets is an identity function for type inference:
The keys are the names you pass to scene.click(...), scene.fill(...), etc.
Fallback chains: defineTarget
When a target has multiple plausible locators (e.g. mid-refactor), declare them in priority order with defineTarget:
During capture, Democraft tries each locator in order and uses the first that resolves. If none resolve, the capture fails with a diagnostic naming the target.
Single locator shorthand
For a target with one locator, the factory itself is enough — no need for defineTarget:
defineTargets accepts either a raw Locator (shorthand) or a full TargetDefinition from defineTarget.
Prefer byRole and byLabel — they encode intent. Reserve byTestId for elements without an accessible name, and byText for last-resort cases where the text is stable.