Principle:Nightwatchjs Nightwatch Page Commands
| Knowledge Sources | |
|---|---|
| Domains | Testing, Design_Patterns, Page_Object_Model |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A command encapsulation pattern that bundles multi-step page interactions into reusable, named methods on page objects.
Description
Page Commands are methods defined on page objects that encapsulate complex multi-step interactions into single, semantically named operations. Instead of tests repeating sequences like "wait for element, type text, click submit," a page command wraps this into page.submit(). Commands have access to all page object elements via the @ alias prefix and should return this for method chaining.
Commands can be defined as object literals (placed in an array) or as ES6 classes. The this context within commands is bound to the page object instance, providing access to all Nightwatch commands and the page's elements.
Usage
Define page commands for any multi-step interaction that is reused across multiple tests. Each command should represent a meaningful user action (e.g., "login", "search", "submit form") rather than a low-level browser operation.
Theoretical Basis
Page commands follow the Command pattern from object-oriented design:
- Each command encapsulates a sequence of operations
- Commands use the page object's element references (not raw selectors)
- Commands return this to enable fluent chaining
- Commands abstract implementation details from test logic