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.

Implementation:Nightwatchjs Nightwatch Page Object Type Definitions

From Leeroopedia
Knowledge Sources
Domains Testing, Type_System, Page_Object_Pattern
Last Updated 2026-02-12 00:00 GMT

Overview

TypeScript type definitions for Nightwatch's Page Object Model, including page definitions, sections, elements, and enhanced page object instances.

Description

The page-object.d.ts file defines all TypeScript interfaces for building typed page objects. The key types are:

  • PageObjectModel — The interface users implement to define a page object module with `url`, `commands`, `elements`, `sections`, and `props`.
  • ElementProperties — Element definition with `selector`, `locateStrategy`, `index`, `timeout`, and `suppressNotFoundErrors`.
  • SectionProperties — Section definition with its own `selector`, `elements`, nested `sections`, `commands`, and `props`.
  • EnhancedPageObject — The runtime instance type with full access to commands, element queries, assert/verify/expect, and the `navigate()` method.
  • EnhancedSectionInstance — Runtime section type with element commands and client commands.
  • EnhancedElementInstance — Runtime element type with `name`, `selector`, `locateStrategy`, and `parent` reference.

Usage

Use these types when defining page objects in TypeScript. The `PageObjectModel` interface should be used with `satisfies` to get type checking while preserving type inference. The `EnhancedPageObject` generic type is used when declaring typed page object variables.

Code Reference

Source Location

Signature

export interface PageObjectModel {
  commands?: Partial<Record<string, (...args: any) => unknown>> | Partial<Record<string, (...args: any) => unknown>>[];
  elements?: Partial<{ [name: string]: string | ElementProperties }> | Partial<{ [name: string]: string | ElementProperties }>[];
  props?: Record<string, unknown> | (() => Record<string, unknown>);
  sections?: { [name: string]: SectionProperties };
  url?: string | ((...args: any) => string);
}

export type EnhancedPageObject<Commands, Elements, Sections, Props, URL> =
  PageObjectClientCommands & ElementCommands & NightwatchCustomCommands &
  EnhancedPageObjectSharedFields<...> & Required<MergeObjectsArray<Commands>> & {
    url: URL;
    navigate(url?: string, callback?: () => void): EnhancedPageObject<...>;
  };

export interface ElementProperties {
  selector: string;
  locateStrategy?: LocateStrategy;
  index?: number;
  abortOnFailure?: boolean;
  timeout?: number;
  retryInterval?: number;
  suppressNotFoundErrors?: boolean;
}

Import

import { PageObjectModel, EnhancedPageObject, ElementProperties, SectionProperties } from 'nightwatch';

I/O Contract

Inputs

Name Type Required Description
url string / function No Page URL or function returning URL
elements Object / Object[] No Named element definitions with selectors
sections Object No Named section definitions with nested elements
commands Object / Object[] No Page-specific command methods

Outputs

Name Type Description
EnhancedPageObject Object Runtime page instance with commands, elements, sections, assert, expect
EnhancedSectionInstance Object Runtime section with scoped element commands
EnhancedElementInstance Object Runtime element with name, selector, parent reference

Usage Examples

Defining a Typed Page Object

import { PageObjectModel } from 'nightwatch';

const loginPage = {
  url: 'https://example.com/login',

  elements: {
    usernameInput: { selector: '#username' },
    passwordInput: { selector: '#password' },
    submitBtn: { selector: 'button[type="submit"]' },
  },

  sections: {
    header: {
      selector: '.page-header',
      elements: {
        logo: { selector: '.logo' },
      },
    },
  },

  commands: [{
    login(username: string, password: string) {
      return this
        .setValue('@usernameInput', username)
        .setValue('@passwordInput', password)
        .click('@submitBtn');
    },
  }],
} satisfies PageObjectModel;

export default loginPage;

Using EnhancedPageObject in Tests

import { EnhancedPageObject } from 'nightwatch';

describe('Login test', function() {
  it('logs in successfully', async function(browser) {
    const login = browser.page.loginPage();
    login
      .navigate()
      .assert.visible('@usernameInput')
      .setValue('@usernameInput', 'testuser')
      .setValue('@passwordInput', 'secret')
      .click('@submitBtn');

    // Access sections
    login.section.header.assert.visible('@logo');
  });
});

Related Pages

Page Connections

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