Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Nightwatchjs Nightwatch Element Interaction And Assertion

From Leeroopedia
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:

  1. Accessible roles: findByRole (most preferred)
  2. Label text: findByLabelText
  3. Placeholder text: findByPlaceholderText
  4. Visible text: findByText
  5. Alt text: findByAltText
  6. CSS selectors: find (least preferred)

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment