Principle:Webdriverio Webdriverio Cucumber Execution
Metadata
| Field | Value |
|---|---|
| Page ID | Cucumber_Execution |
| Page Type | Principle |
| Repository | webdriverio/webdriverio |
| Knowledge Sources | Repo (webdriverio/webdriverio) |
| Domains | Testing, BDD, Execution |
| Related Pages | implemented_by Implementation:Webdriverio_Webdriverio_CucumberAdapter_Class |
Overview
A mechanism for executing Cucumber BDD test scenarios within the WDIO testrunner lifecycle.
Description
Cucumber Execution bridges the WDIO testrunner with the Cucumber runtime. It adapts Cucumber's execution model to work within WDIO's worker process architecture, translating Cucumber events to WDIO reporter events, managing the Cucumber runtime lifecycle, and supporting WDIO-specific features like tag-based capability filtering and custom formatters.
The execution pipeline proceeds through several phases:
Phase 1: Initialization
The adapter is constructed with the worker's context (CID, config, spec files, capabilities, reporter). During construction it:
- Merges user
cucumberOptswithDEFAULT_OPTS - Initializes the Gherkin parser (
Gherkin.Parser,Gherkin.AstBuilder,Gherkin.GherkinClassicTokenMatcher) - Normalizes spec file paths (strips
file://protocol) - Appends the internal
CucumberFormatterto the format list - Injects WDIO reporter and event emitter references into format options
The init() method then:
- Generates dynamic skip tags from capabilities (for capability-based scenario filtering)
- Uses
loadSources()from@cucumber/cucumber/apito filter specs against tag expressions - Deduplicates the final spec list
Phase 2: Runtime Setup
The run() method orchestrates the actual execution:
- Registers required modules (
cucumberOpts.requireModule) for transpilation support - Resets the Cucumber
supportCodeLibraryBuilderwith the appropriate paths - Adds WDIO lifecycle hooks (
beforeFeature,beforeScenario,beforeStep,afterStep,afterScenario,afterFeature) to the Cucumber support code - Loads step definition files from
requireandimportpaths - Wraps step definitions with WDIO's before/after hook integration
- Sets the default step timeout
- Finalizes the support code library
Phase 3: Execution
The adapter calls runCucumber() from @cucumber/cucumber/api with the prepared configuration and support code library. During execution:
- The
CucumberFormatterlistens to Cucumber envelope events and translates them to WDIO reporter events (suite:start,test:start,test:pass,test:fail,suite:end, etc.) - Step results (pass, fail, undefined, ambiguous, pending, skipped) are translated to WDIO states
- Feature, scenario, and step metadata (titles, tags, URIs, durations) are propagated to reporters
Phase 4: Result Collection
After execution completes:
- The
CucumberFormatteremits the failed count via the event emitter - If
ignoreUndefinedDefinitionsis enabled, the adapter uses the formatter's failed count instead of Cucumber's overall success/failure - The WDIO
afterhook is executed with the result, capabilities, and specs - The method returns an exit code:
0for success,1for failure
Usage
Use Cucumber Execution when running BDD tests with npx wdio run wdio.conf.ts and framework: 'cucumber'. The adapter is automatically loaded and manages the Cucumber lifecycle within each worker process. No direct API usage is required -- the WDIO testrunner handles adapter instantiation.
// wdio.conf.ts - the adapter is loaded automatically
export const config: WebdriverIO.Config = {
framework: 'cucumber',
specs: ['./features/**/*.feature'],
cucumberOpts: {
require: ['./step-definitions/**/*.ts'],
tags: '@smoke',
timeout: 60000,
}
}
Run the tests:
// Command line
// npx wdio run wdio.conf.ts
Theoretical Basis
The execution model is based on the Adapter pattern: CucumberAdapter adapts Cucumber's internal API to WDIO's framework adapter interface. It implements init() for setup and run() for execution, translating between two different lifecycle models.
Key Design Patterns
- Adapter Pattern: The
CucumberAdapterclass bridges the WDIO framework adapter contract (init()+run()returning exit code) with Cucumber's API (loadConfiguration(),loadSources(),runCucumber()). - Observer Pattern: The
CucumberFormattersubscribes to Cucumber's envelope event stream and emits WDIO reporter events. It maintains internal state maps (gherkin documents, scenarios, test cases) to correlate events. - Factory Pattern: The exported
adapterFactory.init()function creates and initializesCucumberAdapterinstances. The WDIO testrunner uses this factory to create adapters in worker processes. - Mediator Pattern: An
EventEmitterinstance mediates communication between the adapter and the formatter, passing hook parameters, failed counts, and other runtime data.
Event Translation
The CucumberFormatter translates Cucumber events to WDIO events:
| Cucumber Event | WDIO Event |
|---|---|
testRunStarted |
suite:start (feature level)
|
testCaseStarted |
suite:start or test:start (scenario level, depending on scenarioLevelReporter)
|
testStepStarted |
test:start or hook:start
|
testStepFinished |
test:pass / test:fail / test:pending / hook:end
|
testCaseFinished |
suite:end (scenario level)
|
testRunFinished |
suite:end (feature level)
|
Error Handling
- Runtime errors during execution are caught, stored, and re-thrown after the WDIO
afterhook completes. - Undefined steps are handled based on the
ignoreUndefinedDefinitionsflag: either marked as pending (warning) or failed (error). - Ambiguous step matches are handled based on the
failAmbiguousDefinitionsflag. - Retried test cases (via the
retryoption) emitsuite:retryevents instead ofsuite:start.
Related Pages
Implementation:Webdriverio_Webdriverio_CucumberAdapter_Class
- implemented_by: Implementation:Webdriverio_Webdriverio_CucumberAdapter_Class