Principle:Nightwatchjs Nightwatch Custom Command Creation
| Knowledge Sources | |
|---|---|
| Domains | Testing, Extensibility, Custom_Commands |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
An extensibility pattern for creating reusable, domain-specific browser commands that encapsulate multi-step interactions into a single API call.
Description
Custom Command Creation allows users to extend the Nightwatch API with domain-specific operations. Each custom command is a Node.js module that exports a command method. The this context within the command is bound to the Nightwatch command context, providing access to all built-in browser commands. Custom commands solve the reuse problem across tests: instead of duplicating interaction sequences, a single command encapsulates the pattern and is available across all test files.
Two implementation styles are supported: object-based (simple, synchronous) and class-based (ES6 class, supports async/await).
Usage
Create custom commands for any multi-step browser interaction that is reused across multiple test files. The command filename becomes the command name on the browser object.
Theoretical Basis
Custom commands follow the Command design pattern:
- Encapsulation: Multi-step operations wrapped in a single method
- Reusability: Available across all test files without import
- Composition: Commands can call other commands (built-in or custom)
- Naming: File name determines API surface (browser.fileName())