Implementation:Nightwatchjs Nightwatch Page Object Module Definition
| Knowledge Sources | |
|---|---|
| Domains | Testing, Design_Patterns, Page_Object_Model |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Module export pattern for defining Nightwatch.js page objects conforming to the PageObjectModel interface.
Description
A page object is defined as a Node.js module exporting an object with url, elements, sections, and commands properties. The module file is placed in the page_objects_path directory configured in nightwatch.conf.js. The filename (without extension) becomes the page object identifier on browser.page. Subdirectories create namespaces (e.g., pages/google/search.js becomes browser.page.google.search()).
Usage
Create a page object module for each significant page or component in your application. Place it in the configured page_objects_path directory.
Code Reference
Source Location
- Repository: nightwatch
- File: examples/pages/google/search.js (lines 1-40)
- File: types/page-object.d.ts (lines 370-458)
Signature
module.exports = {
// Page URL - string or function returning string
url: string | (() => string),
// Element mappings
elements: {
[name: string]: string | {
selector: string,
locateStrategy?: 'css selector' | 'xpath' | 'link text' | 'partial link text' | 'tag name',
index?: number
}
},
// Logical sections grouping elements
sections: {
[name: string]: {
selector: string,
locateStrategy?: string,
elements: Object,
sections?: Object,
commands?: Array
}
},
// Page-specific commands
commands: Array<Object> | Class
};
Import
// No import required - page objects are auto-loaded from page_objects_path
// File placement: pages/google/search.js
// Access: browser.page.google.search()
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | string or Function | No | Page URL for navigate() method |
| elements | Object | No | Named element selectors |
| sections | Object | No | Named section definitions with nested elements |
| commands | Array or Class | No | Page-specific reusable methods |
Outputs
| Name | Type | Description |
|---|---|---|
| Page object factory | Function | Registered on browser.page namespace; calling it returns an EnhancedPageObject |
Usage Examples
Complete Page Object
// pages/google/search.js
const searchCommands = {
submit() {
this.waitForElementVisible('@submitButton', 1000)
.click('@submitButton');
this.pause(1000);
return this; // Return page object for chaining
}
};
module.exports = {
url: 'https://google.no',
commands: [searchCommands],
sections: {
consentModal: {
selector: '[aria-modal="true"]',
elements: {
rejectAllButton: '.GzLjMd button:nth-child(1)'
}
}
},
elements: {
searchBar: {
selector: 'textarea[name=q]'
},
submitButton: {
selector: 'input[value="Google Search"]'
}
}
};