Principle:Nightwatchjs Nightwatch Page Object Usage In Tests
| Knowledge Sources | |
|---|---|
| Domains | Testing, Page_Object_Model |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A factory-based instantiation pattern for accessing page objects within test cases via a centralized namespace on the browser instance.
Description
Page Object Usage in Tests describes how test code instantiates and interacts with page objects. Rather than importing page object files directly, tests access them through the browser.page namespace, which acts as a factory. Calling browser.page.namespace.pageName() creates an instance of the page object with all its elements, sections, commands, and the navigate() method.
This factory pattern ensures page objects are properly initialized with the current browser context and provides consistent instantiation across all tests.
Usage
Use this pattern in every test that interacts with page objects. Instantiate page objects at the test suite level or within individual test cases as needed.
Theoretical Basis
The factory pattern provides:
- Lazy instantiation: Page objects are created only when needed
- Context binding: Each instance is bound to the current browser session
- Namespace isolation: Directory structure creates natural namespaces
- Navigation integration: The navigate() method uses the page object's URL