Principle:Nightwatchjs Nightwatch Step Definition Implementation
| Knowledge Sources | |
|---|---|
| Domains | Testing, BDD, Step_Definitions |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A pattern-matching implementation pattern that maps Gherkin natural language steps to executable browser automation code via regex or string patterns.
Description
Step Definition Implementation is the bridge between human-readable Gherkin scenarios and executable test code. Each step definition registers a pattern (regex or string) via Given(), When(), or Then() functions from @cucumber/cucumber. When the Cucumber runner encounters a Gherkin step matching a pattern, it executes the corresponding callback function.
In the Nightwatch integration, the browser global is automatically available in step definition callbacks, providing access to all Nightwatch commands (navigateTo, click, setValue, assert, etc.) without explicit initialization.
Usage
Create step definition files in the src_folders directory. Each file imports Given/When/Then from @cucumber/cucumber and defines patterns that match Gherkin steps. Use the browser global for all browser interactions.
Theoretical Basis
Step definitions follow the adapter pattern:
- Pattern registration: Given/When/Then functions register regex → callback mappings
- Pattern matching: Cucumber matches Gherkin step text against registered patterns
- Capture groups: Regex capture groups become callback arguments
- Browser integration: The global browser object provides Nightwatch commands