Using CucumberJS with Nightwatch
Overview
Nightwatch 2 brings integrated support for using Cucumber.js directly as an alternative test runner. No other plugins are necessary, other than the Cucumber library itself (version 7.3 or higher).
Simply run the following in the same project where Nightwatch is also installed:
npm i @cucumber/cucumber --save-devConfiguration
In order to use CucumberJS in Nightwatch you need to set the test_runner config property and set the type to cucumber. You will also need to set the path to where the feature files are located.
{
test_runner: {
// set cucumber as the runner
type: 'cucumber',
// define cucumber specific options
options: {
//set the feature path
feature_path: 'examples/cucumber-js/*/*.feature',
// start the webdriver session automatically (enabled by default)
auto_start_session: true,
// use parallel execution in Cucumber
// set number of workers to use (can also be defined in the cli as --parallel 2
parallel: 2
}
},
src_folders: ['examples/cucumber-js/features/step_definitions']
}Running Tests
The easiest way to run Cucumber tests from examples is:
npx nightwatch --env cucumber-jsCucumber spec files/step definition files can be provided in src_folders in Nightwatch config or as a CLI argument.
With src_folders defined:
npx nightwatchWithout src_folders defined:
npx nightwatch examples/cucumber-js/features/step_definitionRunning in Parallel
Parallel running using 2 workers:
nightwatch examples/cucumber-js/features/step_definitions --parallel 2Use other test runner options as usual:
npx nightwatch examples/cucumber-js/features/step_definitions --headlessManually Starting the WebDriver Session
You might need sometimes to not start the Webdriver session automatically after Nightwatch is instantiated. For this purpose, Nightwatch provides the instance available as this.client, which contains an launchBrowser() method.
Configuration
{
test_runner: {
type: 'cucumber',
options: {
feature_path: 'examples/cucumber-js/*/*.feature',
auto_start_session: false
}
}
}You can then use an extra setup file that you can pass as an extra --require to Nightwatch, which will be forwarded to Cucumber. In the extra setup file, you can add other operations needed to be executed before the session is started.
Example _extra_setup.js:
Remember to set the browser on this so it can be closed automatically by Nightwatch. Otherwise, remember to call .quit() in your own Cucumber After() hooks.
const {Before} = require('@cucumber/cucumber');
Before(async function(testCase) {
if (!this.client) {
console.error('Nightwatch instance was not created.');
return;
}
this.client.updateCapabilities({
testCap: 'testing'
});
this.browser = await this.client.launchBrowser();
});Run with extra setup
nightwatch examples/cucumber-js/features/step_definitions --require {/full/path/to/_extra_setup.js}Nightwatch setup file for Cucumber
You might also want to inspect the built-in setup file that Nightwatch uses for initializing the Cucumber runner. It is available in our project root folder at /cucumber-js/_setup_cucumber_runner.js.
Reporting
When using the integrated Cucumber test runner, you need to use the Cucumber formatters for generating output.
Nightwatch will forward --format and --format-options CLI arguments, if present, to Cucumber.
By default, the progress formatter is used.
For example:
npx nightwatch --env cucumber-js --format @cucumber/pretty-formatteror:
npx nightwatch --env cucumber-js --require cucumber.conf.js --format json:report/cucumber_report.jsonExample Output
Here's how the output looks like when running the example tests in Firefox. You can just run this in the project where Nightwatch is installed:
npx nightwatch examples/cucumber-js/features/step_definition