CodeceptJS Cucumber BDD Framework
codeceptjs-cucumber is an E2E testing framework that integrates CodeceptJS with Cucumber, enabling Behavior-Driven Development (BDD) workflows. It facilitates running tests across multiple browsers and offers out-of-the-box integration for Sauce Labs, allowing for efficient parallel test execution in the cloud. The current stable version is 5.0.0, released in late 2023. This package typically follows a major release cadence of approximately one to two years, aligning with updates in its core dependencies like CodeceptJS and Cucumber. It leverages the Should.js assertion library by default. As an opinionated framework, its key differentiator lies in simplifying the setup and configuration required to combine these powerful testing tools, particularly for complex cloud-based, cross-browser test automation scenarios, reducing the boilerplate often associated with such integrations.
Common errors
-
TypeError: I.amOnPage is not a function
cause The CodeceptJS helper (e.g., Playwright, WebDriver) is not correctly initialized or the `I` object is not properly injected into your step definitions.fixEnsure your `codecept.conf.js` has a helper defined (e.g., `Playwright` in the `helpers` section) and that `I` is correctly imported via `const { I } = inject();` in your steps file. Also, verify that `include.I` is set in the `codecept.conf.js`. -
Undefined step: I am on the Google homepage
cause Cucumber cannot find a matching step definition for a Gherkin step defined in your feature file.fixVerify that your step definition files are correctly linked in `codecept.conf.js` (check `gherkin.steps` and `Cucumber.require` paths). Ensure the regular expression in your step definition exactly matches the Gherkin step text. -
Error: Cannot find module '@cucumber/cucumber'
cause The `@cucumber/cucumber` package is either not installed, or Node.js cannot resolve its path.fixRun `npm install @cucumber/cucumber` or `yarn add @cucumber/cucumber`. Check your `package.json` for the correct dependency and ensure your `node_modules` directory is up-to-date. -
Error: Cannot find module 'codeceptjs'
cause The `codeceptjs` package is not installed or incorrectly resolved by your system.fixRun `npm install codeceptjs` or `yarn add codeceptjs`. Confirm it's listed in your `package.json` and `node_modules`.
Warnings
- breaking codeceptjs-cucumber v4.0.0 introduced breaking changes by upgrading to CodeceptJS v3 and Cucumber v8. Subsequent versions, including v5.0.0, continue to rely on these newer versions and `@cucumber/cucumber` v10. This requires updates to helper configurations, step definition syntaxes, and potentially project structure.
- gotcha Parallel execution with Sauce Labs requires specific configurations in `codecept.conf.js`, including setting up `sauceConnect: true` and defining appropriate capabilities. Incorrect setup will lead to tests running locally or failing to connect to Sauce Labs, often without clear error messages.
- gotcha Misconfigured `gherkin.steps` or `Cucumber.require` paths in `codecept.conf.js` will result in 'Undefined step' errors during test execution, even if step definitions exist.
- deprecated Older versions of Cucumber (below v8) are deprecated and no longer fully compatible with `codeceptjs-cucumber` v4 and v5. Using an outdated Cucumber version can lead to unexpected behavior, API mismatches, or missing features.
Install
-
npm install codeceptjs-cucumber -
yarn add codeceptjs-cucumber -
pnpm add codeceptjs-cucumber
Imports
- Cucumber Helper Configuration
CucumberHelper: { /* ... */ } // Incorrect helper name // Missing Cucumber helper configuration in helpers sectionCucumber: { require: './step_definitions', features: './features/*.feature' } - Step Definition File Path Configuration
require: './steps.js' // Pointing directly to a file instead of a directory // require: './features' // Incorrect path for step definitionsrequire: './step_definitions'
- Feature File Path Configuration
features: './feature.feature' // Only points to one file, misses glob pattern // features: './steps' // Incorrect path for feature filesfeatures: './features/*.feature'
Quickstart
/* package.json */
{
"name": "codeceptjs-cucumber-example",
"version": "1.0.0",
"description": "Example usage of codeceptjs-cucumber",
"scripts": {
"test": "codeceptjs run --steps"
},
"devDependencies": {
"codeceptjs": "^3.0.0",
"@cucumber/cucumber": "^10.0.0",
"codeceptjs-cucumber": "^5.0.0",
"playwright": "^1.0.0",
"should": "^13.0.0"
}
}
/* codecept.conf.js */
const { setWorldConstructor } = require('@cucumber/cucumber');
exports.config = {
output: './output',
helpers: {
Playwright: {
url: 'https://www.google.com',
show: true,
browser: 'chromium'
},
Cucumber: {
require: './step_definitions',
features: './features/*.feature'
}
},
include: {
I: './steps_file.js'
},
gherkin: {
features: './features/*.feature',
steps: ['./step_definitions/steps.js']
},
plugins: {
allure: {
enabled: true
},
retryFailedStep: {
enabled: true
},
screenshotOnFail: {
enabled: true
}
},
bootstrap: null,
teardown: null,
mocha: {},
name: 'codeceptjs-cucumber-example'
};
/* features/search.feature */
Feature: Google Search Functionality
As a user, I want to search on Google
So that I can find information
Scenario: Search for a specific term
Given I am on the Google homepage
When I search for "CodeceptJS Cucumber"
Then I should see "CodeceptJS Cucumber" in the search results
/* step_definitions/steps.js */
const { I } = inject();
const { Given, When, Then } = require('@cucumber/cucumber');
Given('I am on the Google homepage', () => {
I.amOnPage('/');
});
When('I search for "{string}"', async (searchTerm) => {
I.fillField('textarea[name="q"]', searchTerm);
I.pressKey('Enter');
});
Then('I should see "{string}" in the search results', async (expectedText) => {
I.see(expectedText);
});