Using CucumberJS with Nightwatch Suggest edits
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:
Configuration
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.
Running Tests
The easiest way to run Cucumber tests from examples is:
Cucumber spec files/step definition files can be provided in src_folders
in Nightwatch config or as a CLI argument.
With src_folders
defined:
Without src_folders
defined:
Running in Parallel
Parallel running using 2 workers:
Use other test runner options as usual:
Manually 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
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.
Run with extra setup
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:
or:
Example 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: