Element Selectors In Cucumber Protractor Javascript
Nightwatch 2 offers combined support for directly using Cucumber.js as an alternative test runner. Apart from the Cucumber library itself (version 7.3 or above), none else are required.
Just run the following within the same project where Nightwatch is also installed:
Configuration
Using CucumberJS in Nightwatch requires you to specify the type as cucumber and adjust the test_runner config value. You will also have to decide where the feature files reside.
{
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
Running Cucumber tests from examples is easiest done this way:
npx nightwatch --env cucumber-js
Source folders in Nightwatch settings or as a CLI parameter can contain cucumber spec files or step definition files.
Manually Starting the WebDriver Session
Sometimes you might have to stop the Webdriver session from starting automatically following Nightwatch instantiation. For this reason, Nightwatch offers the instance this.client has, which has launch Browser() capability.
{
test_runner: {
type: 'cucumber',
options: {
feature_path: 'examples/cucumber-js/*/*.feature',
auto_start_session: false
}
}
}
After that, you can forward an additional setup file you can pass to Nightwatch, which will be used as extra need. Cucumber Additional actions required before the session begins might be included to the extra setup file.
Example _extra_setup.js:
Make sure the browser is configured such that Nightwatch may automatically close it. Otherwise keep in mind to phone.quit() among 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
Additionally worth looking at is the built-in configuration file Nightwatch uses to start the Cucumber runner. Our project root folder contains it at /cucumber-js/_setup_cucumber_runner.js.
Reporting
The integrated Cucumber test runner requires you to create output using the Cucumber formers.