Principle:Nightwatchjs Nightwatch Element Interaction And Assertion
| Knowledge Sources | |
|---|---|
| Domains | Testing, Component_Testing, Accessibility |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A semantic element querying pattern that locates DOM elements by accessibility roles, text content, and labels rather than CSS selectors.
Description
Element Interaction and Assertion provides a set of query methods inspired by Testing Library that locate elements by their semantic properties. Methods like findByRole, findByText, findByPlaceholderText, findByLabelText, and findByAltText encourage tests to interact with the page the way users do—by visible text, labels, and roles rather than implementation-specific selectors.
This approach produces more resilient tests because they are less coupled to CSS class names or DOM structure, and they implicitly verify accessibility by requiring elements to have proper roles and labels.
Usage
Use semantic queries (findByRole, findByText, etc.) as the preferred method for locating elements in component tests. Fall back to CSS selectors only when semantic queries are not practical.
Theoretical Basis
The priority of queries follows Testing Library guidelines:
- Accessible roles: findByRole (most preferred)
- Label text: findByLabelText
- Placeholder text: findByPlaceholderText
- Visible text: findByText
- Alt text: findByAltText
- CSS selectors: find (least preferred)