Implementation:Nightwatchjs Nightwatch Custom Command Type Definitions
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Testing, Type_System, Extensibility |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
TypeScript type definitions for the `NightwatchCustomCommandsModel` interface used to create custom commands, and the `NightwatchCustomCommands` augmentation interface.
Description
The custom-command.d.ts file defines:
- CustomCommandInstance — Extends `CommandInstance` from the core types, providing the base class for custom command instances.
- NightwatchCustomCommandsModel — The interface for custom command classes. Requires a `command(...args)` method that returns `unknown | Promise<unknown>`.
- NightwatchCustomCommands — Empty augmentation interface that users extend via module declaration to register custom commands on the `browser` object.
Usage
Implement `NightwatchCustomCommandsModel` when creating class-based custom commands. Augment `NightwatchCustomCommands` to add TypeScript types for custom commands accessible on `browser`.
Code Reference
Source Location
- Repository: Nightwatchjs_Nightwatch
- File: types/custom-command.d.ts
- Lines: 1-25
Signature
export interface CustomCommandInstance extends CommandInstance {}
export interface NightwatchCustomCommandsModel {
command: (...args: any[]) => unknown | Promise<unknown>;
}
export interface NightwatchCustomCommands {}
Import
import { NightwatchCustomCommandsModel } from 'nightwatch';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| command | function | Yes | The method implementing the custom command logic |
| args | any[] | No | Arguments passed when invoking the command |
Outputs
| Name | Type | Description |
|---|---|---|
| Return value | unknown / Promise | Command result, can be synchronous or async |
| Registered on browser | Method | Custom command available as `browser.customName()` |
Usage Examples
Class-Based Custom Command
import { NightwatchCustomCommandsModel } from 'nightwatch';
class LogMessage implements NightwatchCustomCommandsModel {
async command(message: string) {
console.log(`[Custom] ${message}`);
return Promise.resolve();
}
}
export default LogMessage;
Augmenting TypeScript Types
declare module 'nightwatch' {
interface NightwatchCustomCommands {
logMessage(message: string): this;
}
}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment