Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Nightwatchjs Nightwatch Custom Command Creation

From Leeroopedia
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:

  1. Encapsulation: Multi-step operations wrapped in a single method
  2. Reusability: Available across all test files without import
  3. Composition: Commands can call other commands (built-in or custom)
  4. Naming: File name determines API surface (browser.fileName())

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment