Principle:Nightwatchjs Nightwatch Custom Assertion Creation
| Knowledge Sources | |
|---|---|
| Domains | Testing, Extensibility, Assertions |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
An extensibility pattern for creating domain-specific test assertions that integrate with the framework's assertion and verification system.
Description
Custom Assertion Creation allows users to define assertions beyond the built-in set. Each custom assertion implements a protocol with five methods: expected() returns the expected value, pass(value) determines if the actual value passes, value(result) extracts the actual value from a command result, command(callback) executes the browser command that retrieves the value, and failure(result) detects error conditions. Custom assertions are available on both browser.assert (fail immediately) and browser.verify (log failure, continue).
Usage
Create custom assertions for domain-specific validation that is not covered by built-in assertions. Common uses include checking specific attributes, computed styles, or application state.
Theoretical Basis
The custom assertion protocol follows a retrieve-compare-report pattern:
- command(callback): Execute a browser command to retrieve a value
- value(result): Extract the actual value from the command result
- expected(): Return the expected value
- pass(value): Compare actual to expected, return boolean
- failure(result): Detect error conditions (element not found, etc.)
- message: Human-readable description of the assertion