Implementation:Nightwatchjs Nightwatch Extension Invocation Pattern
| Knowledge Sources | |
|---|---|
| Domains | Testing, Extensibility |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Pattern for invoking custom commands and assertions in Nightwatch.js tests, demonstrating seamless integration with the built-in API.
Description
Custom commands are called via browser.commandName(args) for root-level commands, browser.namespace.commandName(args) for namespaced commands, and browser.assert.assertionName(args) for assertions. No import or setup is needed in test files; extensions are globally available once paths are configured.
Usage
Call custom commands and assertions in tests exactly like built-in commands. Ensure custom_commands_path and custom_assertions_path are configured in nightwatch.conf.js.
Code Reference
Source Location
- Repository: nightwatch
- File: test/apidemos/custom-commands/testNamespacedAliases.js (lines 1-17)
- File: test/apidemos/custom-commands/testUsingAutoInvokeCommand.js (lines 1-12)
Signature
// Custom command invocation
browser.strictClick(selector: string) -> browser
// Namespaced command invocation
browser.angular.getElementsInList(listName: string) -> Promise
// Custom assertion invocation
browser.assert.testCustomAssertion(selector, attribute, expected, msg?) -> browser
browser.verify.testCustomAssertion(selector, attribute, expected, msg?) -> browser
Import
// No import required - extensions are globally available
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Command arguments | any | Varies | As defined by the custom command's command() method |
| Assertion arguments | any | Varies | As defined by the custom assertion's exports.assertion function |
Outputs
| Name | Type | Description |
|---|---|---|
| browser (chaining) | NightwatchAPI | Commands return browser for chaining |
| Assertion result | Pass/Fail | Logged to console and reporter |
Usage Examples
Using Custom Commands and Assertions
describe('Test with custom extensions', function() {
it('uses custom strictClick command', function(browser) {
browser
.navigateTo('https://example.com')
.strictClick('#login-button') // Custom command
.setValue('#username', 'admin')
.strictClick('#submit'); // Reuse custom command
});
it('uses custom assertion', function(browser) {
browser
.navigateTo('https://example.com')
.assert.testCustomAssertion('#elem', 'data-role', 'admin')
.verify.testCustomAssertion('#other', 'data-type', 'button');
});
it('uses namespaced command', function(browser) {
browser
.navigateTo('https://example.com/angular-app')
.angular.getElementsInList('item in todoItems');
});
});