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.

Workflow:Nightwatchjs Nightwatch Page Object Pattern

From Leeroopedia
Knowledge Sources
Domains E2E_Testing, Test_Architecture, Design_Patterns
Last Updated 2026-02-12 00:00 GMT

Overview

End-to-end process for structuring Nightwatch tests using the Page Object design pattern to encapsulate page-specific selectors and actions into reusable, maintainable modules.

Description

This workflow defines how to create and use Page Objects in Nightwatch to abstract UI structure away from test logic. A Page Object encapsulates element selectors, page sections, and custom commands for a specific page or component of the application under test. Tests reference page objects by name, calling high-level methods instead of embedding raw selectors. This reduces duplication, improves readability, and centralizes selector maintenance so that UI changes require updates in one place rather than across many test files.

Usage

Execute this workflow when your test suite grows beyond a few tests and you find yourself duplicating selectors across multiple test files. It is especially valuable for applications with complex UIs where multiple tests interact with the same pages, or when the UI changes frequently and selector maintenance becomes costly.

Execution Steps

Step 1: Define Page Object Structure

Create page object files in the configured page_objects_path directory. Each page object module exports an object containing a url property, an elements map (selector aliases), optional sections for nested component areas, and an optional commands array of reusable action methods.

Key considerations:

  • Each page object maps to one logical page or major UI component
  • Elements are defined as selector aliases with a selector property and optional locateStrategy
  • Sections allow nesting for modular page areas (e.g., header, sidebar, modal)
  • Commands encapsulate multi-step interactions as named methods

Step 2: Map Elements and Sections

Define element selectors using short, descriptive alias names. Group related elements into sections when a page has distinct UI regions. Sections can have their own elements, commands, and even nested sub-sections.

What happens:

  • Each element is defined with a selector string (CSS or XPath)
  • Elements within sections are scoped to the section's root element
  • Sections inherit available commands from the parent page object
  • Element aliases replace raw selectors throughout test code

Step 3: Implement Page Commands

Write custom command methods on the page object that encapsulate multi-step user interactions. Commands return the page object instance to support method chaining. They can access elements by alias using the @ prefix notation.

Key considerations:

  • Commands are defined as an array of plain objects or classes
  • Each command method receives the browser/page context via this
  • Commands should represent meaningful user actions (e.g., submitSearch, login)
  • Return this or the page object to enable fluent chaining

Step 4: Reference Page Objects in Tests

Access page objects in test files via browser.page.<name>() which returns an enhanced page object instance. Call navigate() to go to the page URL, interact with elements using their aliases, and use page-level commands for complex flows.

What happens:

  • browser.page.pageName() instantiates the page object
  • page.navigate() opens the URL defined in the page object
  • Element aliases are used with @ prefix in commands (e.g., @searchBox)
  • Section elements are accessed via page.section.sectionName

Step 5: Compose Multi-Page Flows

Chain multiple page objects together to test flows that span several pages. Each page transition creates a new page object instance. Assertions can be made on each page before navigating to the next.

Key considerations:

  • Instantiate each page object at the point of transition
  • Use expect or assert APIs through the page object for page-specific assertions
  • Page objects can be composed in before/after hooks for shared setup
  • Section commands and page commands can be mixed freely

Execution Diagram

GitHub URL

Workflow Repository