Implementation:Nightwatchjs Nightwatch Page Commands Interface
| Knowledge Sources | |
|---|---|
| Domains | Testing, Design_Patterns, Page_Object_Model |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Interface specification for defining page-specific commands as object literals or ES6 classes in Nightwatch.js page objects.
Description
Page commands are defined in the commands property of a page object module. Two styles are supported: object-based (an array of objects with method definitions) and class-based (an ES6 class). In both cases, this is bound to the page object instance, providing access to all Nightwatch commands, element aliases via @, and the this.api property for the full Nightwatch API.
Usage
Use object-based commands for simple method collections. Use class-based commands when you need inheritance, private methods, or more structured organization.
Code Reference
Source Location
- Repository: nightwatch
- File: examples/pages/google/search.js (lines 1-10)
- File: examples/pages/google/consent.js (lines 1-31)
- File: examples/pages/nightwatchFeatures.js (lines 1-17)
Signature
// Object-based commands (array of objects)
commands: [{
methodName(args) {
this.waitForElementVisible('@element');
this.click('@element');
return this; // Enable chaining
}
}]
// Class-based commands
commands: class PageCommands {
methodName(args) {
this.page.section.sectionName.click('@element');
return this;
}
}
Import
// Defined within a page object module - no separate import
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| this | PageObject | Automatic | Bound to the page object instance with all Nightwatch commands |
| this.api | NightwatchAPI | Automatic | Full Nightwatch API for advanced operations |
| @elementName | string | N/A | Element alias prefix for referencing named elements |
Outputs
| Name | Type | Description |
|---|---|---|
| this | PageObject | Commands should return this for method chaining |
Usage Examples
Object-Based Commands
const searchCommands = {
submit() {
this.waitForElementVisible('@submitButton', 1000)
.click('@submitButton');
this.pause(1000);
return this;
}
};
module.exports = {
url: 'https://google.no',
commands: [searchCommands],
elements: {
searchBar: { selector: 'textarea[name=q]' },
submitButton: { selector: 'input[value="Google Search"]' }
}
};
Class-Based Commands
class ConsentCommand {
turnOffSearchCustomization() {
this.page.section.customizeSearch.click('@turnOffButton');
return this;
}
confirm() {
this.page.section.consentForm.click('@submitButton');
return this;
}
turnOffEverything() {
return this.turnOffSearchCustomization()
.confirm();
}
}
module.exports = {
url: 'http://google.com',
commands: ConsentCommand,
sections: { /* ... */ }
};