Cucumber.js
Cucumber, the popular Behaviour-Driven Development tool, brought to your JavaScript stack.
It runs on both Node.js and modern web browsers.
Prerequesites
Cucumber.js is tested on:
- Node.js 4.x, 0.12, 0.10, and io.js (see CI builds)
- Google Chrome
- Firefox
- Safari
- Opera
Usage
Install
Cucumber.js is available as an npm module.
Install globally with:
$ npm install -g cucumber
Install as a development dependency of your application with:
$ npm install --save-dev cucumber
Features
Features are written with the Gherkin syntax
# features/myFeature.featureFeature: Example featureAs a user of cucumber.jsI want to have documentation on cucumberSo that I can concentrate on building awesome applicationsScenario: Reading documentationGiven I am on the Cucumber.js GitHub repositoryWhen I go to the README fileThen I should see "Usage" as the page title
Support Files
Support files let you setup the environment in which steps will be run, and define step definitions.
World
World is a constructor function with utility properties, destined to be used in step definitions:
// features/support/world.jsvar zombie = ; { thisbrowser = ; // this.browser will be available in step definitions this { thisbrowser; }; ; // tell Cucumber we're finished and to use 'this' as the world instance} module { thisWorld = World;};
It is possible to tell Cucumber to use another object instance than the constructor:
// features/support/world.jsvar zombie = ;{var browser = ;var world =browser: browser // this.browser will be available in step definitions{ // this.visit will be available in step definitionsthisbrowser;};; // tell Cucumber we're finished and to use our world object instead of 'this'}module {thisWorld = World;};
Step Definitions
Step definitions are the glue between features written in Gherkin and the actual SUT (system under test). They are written in JavaScript.
All step definitions will run with this
set to what is known as the World in Cucumber. It's an object exposing useful methods, helpers and variables to your step definitions. A new instance of World
is created before each scenario.
Step definitions are contained within one or more wrapper functions.
Those wrappers are run before executing the feature suite. this
is an object holding important properties like the Given()
, When()
and Then()
functions. Another notable property is World
; it contains a default World
constructor that can be either extended or replaced.
Step definitions are run when steps match their name. this
is an instance of World
.
// features/step_definitions/myStepDefinitions.jsmodule {this;this;this;};
Promises
Instead of Node.js-style callbacks, promises can be returned by step definitions:
this;
Simply omit the last callback
parameter and return the promise.
Synchronous step definitions
Often, asynchronous behaviour is not needed in step definitions. Simply omit the callback parameter, do not return anything and Cucumber will treat the step definition function as synchronous:
this;
Strings instead of regular expressions
It is also possible to use simple strings instead of regexps as step definition patterns:
this;
'I have $count "$string"'
would translate to /^I have (.*) "([^"]*)"$/
.
Timeouts
By default, asynchronous hooks and steps timeout after 5000 milliseconds. This can be modified globally with:
// features/support/env.js var { this;}; moduleexports = configure;
A specific step's timeout can be set with:
// features/step_definitions/my_steps.js var { this;}; moduleexports = mySteps;
Hooks
Hooks can be used to prepare and clean the environment before and after each scenario is executed. Hooks can use callbacks, return promises, or be synchronous. The first argument to hooks is always the current scenario. See Cucumber.Api.Scenario for more information.
Before hooks
To run something before every scenario, use before hooks:
// features/support/hooks.js (this path is just a suggestion)var {this;};moduleexports = myHooks;
After hooks
The before hook counterpart is the after hook. It's similar in shape but is executed, well, after every scenario:
// features/support/after_hooks.js var { this;}; moduleexports = myAfterHooks;
Around hooks
It's also possible to combine both before and after hooks in one single definition with the help of around hooks:
// features/support/advanced_hooks.js { this;}; moduleexports = myAroundHooks;
Tagged hooks
Hooks can be conditionally elected for execution based on the tags of the scenario.
// features/support/hooks.js (this path is just a suggestion)var {this;};moduleexports = myHooks;
Attachments
You can attach text, images and files to the Cucumber report using the scenario object:
this;
By default, text is saved with a MIME type of text/plain
. You can also specify
a different MIME type:
this;
Images and other binary data can be attached using a stream.Readable
this;
Images and binary data can also be attached using a Buffer
this;
Here is an example of saving a screenshot using WebDriver when a scenario fails
this;
After features event
The after features event is emitted once all features have been executed, just before the process exits. It can be used for tasks such as closing your browser after running automated browser tests with selenium or phantomjs.
note: There are "Before" and "After" events for each of the following: "Features", "Feature", "Scenario", "Step" as well as the standalone events "Background" and "StepResult". e.g. "BeforeScenario".
// features/support/after_hooks.jsvar { this;} moduleexports = myAfterHooks;
Transpilers
Step definitions and support files can be written in other languages that transpile to javascript. This done with the CLI option --compiler <file_extension>:<module_name>
.
CoffeeScript
Install the coffee-script NPM package and invoke Cucumber with --compiler coffee:coffee-script/register
.
TypeScript
Install the typescript-node NPM package and invoke Cucumber with --compiler ts:typescript-node/register
.
As usual, all your step definition and support files must export a function to be run by Cucumber. This is how it is done in TS:
declare ;module.exports =
PogoScript
Install the pogo NPM package and invoke Cucumber with --compiler pogo:pogo
.
Run cucumber
Cucumber.js includes a binary file to execute the features.
If you installed cucumber.js globally, you may run it with:
$ cucumber.js
You may specify the features to run:
$ cucumber.js features/my_feature.feature
And require specific step definitions and support code files with the --require option:
$ cucumber.js features/my_feature.feature --require features/step_definitions/my_step_definitions.js
If you installed Cucumber locally or with npm install --save-dev
, you'll need to specify the path to the binary:
$ ./node_modules/.bin/cucumber.js
Note to Windows users: invoke Cucumber.js with cucumber-js
instead of cucumber.js
. The latter is causing the operating system to invoke JScript instead of Node.js, because of the so-called file extension.
Examples
A few example apps are available for you to browse:
Contribute
See CONTRIBUTE.
Help & support
- Twitter: @cucumber_js
- IRC: #cucumber on Freenode
- Google Groups: cukes
- Website: cucumber.io