Implementation:Nightwatchjs Nightwatch Multi Page Flow Pattern
| Knowledge Sources | |
|---|---|
| Domains | Testing, Page_Object_Model, Test_Architecture |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Pattern for composing multiple Nightwatch.js page object instances in a single test to model cross-page user journeys.
Description
Multi-page flows instantiate multiple page objects via browser.page.*() within a single test, each representing a different page in the user journey. As the user navigates between pages, the test creates new page object instances and performs assertions on the destination page's elements and sections. Cross-page assertions use expect.element() and expect.section() on the appropriate page object.
Usage
Use this pattern when a test needs to verify behavior across multiple pages, such as search → results, login → dashboard, or checkout → confirmation flows.
Code Reference
Source Location
- Repository: nightwatch
- File: examples/tests/googlePageObject.js (lines 2-21)
Signature
// Multi-page flow pattern
const homePage = browser.page.namespace.firstPage();
homePage.navigate();
homePage.doAction();
const resultsPage = browser.page.namespace.secondPage();
resultsPage.expect.element('@element').to.be.present;
resultsPage.expect.section('@section').to.be.visible;
const sectionObj = resultsPage.section.sectionName;
sectionObj.expect.element('@childElement').to.be.visible;
Import
// No import required - uses browser.page namespace
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Multiple page object definitions | Page modules | Yes | Page objects for each page in the flow |
| browser.page.* | Factory calls | Yes | Instantiate each page as the flow progresses |
Outputs
| Name | Type | Description |
|---|---|---|
| Cross-page assertions | Pass/Fail | Assertions verified across different page objects |
| Section-level checks | Pass/Fail | Element assertions within page sections |
Usage Examples
Search to Results Flow
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) {
// Page 1: Search page
homePage.setValue('@searchBar', 'Nightwatch.js');
homePage.submit();
// Page 2: Results page (new page object)
const resultsPage = browser.page.google.searchResults();
resultsPage.expect.element('@results').to.be.present;
resultsPage.expect.element('@results').text.to.contain('Nightwatch.js');
// Section within results page
resultsPage.expect.section('@menu').to.be.visible;
const menuSection = resultsPage.section.menu;
menuSection.expect.element('@videos').to.be.visible;
});
});