Implementation:Nightwatchjs Nightwatch Cucumber Step Definitions
| Knowledge Sources | |
|---|---|
| Domains | Testing, BDD, Step_Definitions |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Wrapper implementation for Cucumber.js step definitions using the browser global for Nightwatch command execution within BDD scenarios.
Description
Step definitions import Given, When, Then, and Before from @cucumber/cucumber and register pattern-callback pairs. The browser global is automatically injected by Nightwatch's Cucumber integration, providing the full Nightwatch API. Step callbacks can be synchronous or async; returning a browser.command() promise enables proper async chaining.
Usage
Create step definition files in the src_folders path. Import Given/When/Then from @cucumber/cucumber. Use the browser global for all Nightwatch commands. Return browser command calls for proper async behavior.
Code Reference
Source Location
- Repository: nightwatch
- File: examples/cucumber-js/features/step_definitions/nightwatch.js (lines 1-33)
Signature
const { Given, When, Then, Before } = require('@cucumber/cucumber');
Given(pattern: RegExp | string, callback: Function) -> void
When(pattern: RegExp | string, callback: Function) -> void
Then(pattern: RegExp | string, callback: Function) -> void
Before(callback: Function) -> void
// In callback body:
// browser (global) - Nightwatch API instance
// Capture groups from regex pattern become function arguments
Import
const { Given, When, Then, Before, After } = require('@cucumber/cucumber');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pattern | RegExp or string | Yes | Pattern matching Gherkin step text (capture groups become args) |
| callback | Function | Yes | Async function executing browser commands |
| browser (global) | NightwatchAPI | Automatic | Injected by Nightwatch Cucumber integration |
Outputs
| Name | Type | Description |
|---|---|---|
| Step execution | Promise | Browser commands executed; return promise for async chaining |
Usage Examples
Step Definitions with Browser Commands
const { Given, Then, When } = require('@cucumber/cucumber');
Given(/^I open the Rijksmuseum page$/, function() {
return browser.navigateTo('https://www.rijksmuseum.nl/en');
});
Given(/^I dismiss the cookie dialog$/, async function() {
const visible = await browser.isVisible({
selector: '.cookie-consent-bar-wrap',
suppressNotFoundErrors: true
});
if (visible) {
return browser.click('.cookie-consent-bar-wrap button.link');
}
});
When(/^I search "([^"]*)"$/, async function(searchTerm) {
await browser.pause(1000).click('a[aria-label="Search"]');
return browser
.waitForElementVisible('#rijksmuseum-app')
.setValue('input.search-bar-input[type=text]', [
searchTerm,
browser.Keys.ENTER
])
.pause(1000);
});
Then(/^the title is "([^"]*)"$/, function(title) {
return browser.assert.titleEquals(title);
});
Then(/^Body contains "([^"]*)"$/, function(contains) {
return browser.assert.textContains('.search-results', contains);
});