Implementation:Nightwatchjs Nightwatch Browser Page Factory
| Knowledge Sources | |
|---|---|
| Domains | Testing, Page_Object_Model |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete API for instantiating and using page objects within Nightwatch.js tests via the browser.page factory namespace.
Description
The browser.page object is a factory namespace that provides access to all page objects defined in page_objects_path. Page object files in subdirectories create nested namespaces (e.g., pages/google/search.js → browser.page.google.search()). Calling the factory returns an EnhancedPageObject instance with navigate(), setValue(), expect.element(), and all defined commands.
Usage
Call browser.page.namespace.pageName() to instantiate a page object, then use navigate() to open its URL, interact with elements using @ aliases, and call page commands.
Code Reference
Source Location
- Repository: nightwatch
- File: examples/tests/googlePageObject.js (lines 1-22)
- File: types/page-object.d.ts (lines 469-523)
Signature
// Factory instantiation
browser.page.namespace.pageName() -> EnhancedPageObject
// EnhancedPageObject methods
pageObject.navigate(url?: string) -> EnhancedPageObject
pageObject.setValue('@elementAlias', value: string) -> EnhancedPageObject
pageObject.expect.element('@elementAlias') -> ExpectElement
pageObject.section.sectionName -> SectionObject
Import
// No import required - page objects are accessed via browser.page
// Requires page_objects_path configured in nightwatch.conf.js
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| page_objects_path | string (config) | Yes | Directory containing page object files (in nightwatch.conf.js) |
| browser.page.namespace.pageName() | Function call | Yes | Factory invocation to create page object instance |
Outputs
| Name | Type | Description |
|---|---|---|
| EnhancedPageObject | Object | Page object instance with navigate(), commands, elements, sections, assert, expect |
Usage Examples
Using Page Objects in Tests
describe('Google search with page objects', function() {
const homePage = browser.page.google.search();
before(async () => homePage.navigate());
after(async (browser) => browser.quit());
it('should find nightwatch.js in results', function(browser) {
homePage.setValue('@searchBar', 'Nightwatch.js');
homePage.submit();
const resultsPage = browser.page.google.searchResults();
resultsPage.expect.element('@results').to.be.present;
resultsPage.expect.element('@results').text.to.contain('Nightwatch.js');
resultsPage.expect.section('@menu').to.be.visible;
const menuSection = resultsPage.section.menu;
menuSection.expect.element('@videos').to.be.visible;
});
});