项目作者: stevenmhunt

项目描述 :
Additional tools, utilities, and capabilities for Cucumber.js.
高级语言: JavaScript
项目地址: git://github.com/stevenmhunt/cucumber-extra.git
创建时间: 2020-09-19T17:36:27Z
项目社区:https://github.com/stevenmhunt/cucumber-extra

开源协议:MIT License

下载


:cucumber: Cucumber.js Extra

Additional tools, utilities, and capabilities for Cucumber.js.

  • Manage all of your profiles with a YAML file (no more annoying CLI arguments!)
  • Additional hooks including BeforeStep, AfterStep, and BeforeValue.
  • Configurable delays and retry handling for steps.
  • Enhanced type handling for step definition arguments.
  • Template engine support for step definition arguments.

My friend: Can you not be extra for 10 minutes? Me 11 minutes later (bird in a vegetable costume)

Installation and Setup

1) Install cucumber and cucumber-extra:

  1. npm install cucumber cucumber-extra --save

Note: Cucumber.js 5.0.0 and above is supported.

2) Create a ./cucumber.js file in your project and set it to the following:

  1. module.exports = require('cucumber-extra/init');

If you already have a ./cucumber.js file: Don’t panic! You won’t be needing it anymore :sunglasses:

3) Create a ./cucumber-extra.yaml file in your project. You can configure all the things here (including those profiles you used to keep in the ./cucumber.js file).

Congratulations on being extra! :tada:

Simple Profile Management

This package provides a convinient way of managing one or more profiles in YAML, making it simple to keep track of step definition libraries, languages, formatters, and other configurable options in your test project.

Here is an example of configuring all possible command-line options through profiles. You can omit this section entirely or only add the parameters you wish to change:

  1. profiles:
  2. default:
  3. backtrace: false
  4. dryRun: false
  5. exit: false
  6. failFast: false
  7. format:
  8. - progress
  9. - summary
  10. formatOptions:
  11. option1: test
  12. language: ISO 639-1
  13. name:
  14. noStrict: true
  15. order: defined
  16. parallel: 3
  17. require:
  18. - some/file.js
  19. - some/directory
  20. - some/glob/pattern/**/*.js
  21. requireModule:
  22. - module1
  23. - module2
  24. retry: 3
  25. retryTagFilter: "@retry"
  26. tags: "not @ignore"
  27. worldParameters:
  28. param1: test
  29. profile2:
  30. ...

You can define one or more profiles here, and then use the --profile <name> flag to specify it when you run ./node_modules/.bin/cucumber-js. For more information on command-line parameters, review the Cucumber.js CLI documentation.

Hooks

This package implements a step definition wrapper using setDefinitionFunctionWrapper in order to provide step-level hooks and the ability to modify step definition arguments at runtime. This wrapper is compatible with synchronous and asynchronous step definitions, and supports both callbacks and promises.

  1. const { BeforeStep, AfterStep, BeforeValue } = require('cucumber-extra');
  2. // runs before every step.
  3. BeforeStep(function({ pickle, args }) {
  4. // use `this` to reference the current scenario context here.
  5. });
  6. // runs after every step.
  7. AfterStep(function({ pickle, args, err, result }) {
  8. // use `this` to reference the current scenario context here.
  9. });
  10. // runs for every argument and table header and value.
  11. // Example: make all step definition arguments upper case strings.
  12. BeforeValue(value => `${value}`.toUpperCase());

Step Delays and Retries

Add the following section to your cucumber-extra.yaml file:

  1. steps:
  2. delay:
  3. # (default: 0) the number of milliseconds to wait before a step is executed.
  4. before: 1000
  5. # (default: 0) the number of milliseconds to wait after a step is executed.
  6. after: 1000
  7. retry:
  8. # (default: 0) the number of times to attempt to retry a step or hook if it fails.
  9. count: 3
  10. # (default: 0) the number of milliseconds to wait before retrying a step.
  11. delay: 1000
  12. # (default: 0) the number of milliseconds to add to the delay on each retry attempt.
  13. backoff: 2000

Type Handling

Add the following section to your cucumber-extra.yaml file:

  1. types:
  2. # (default: true) whether or not type handling is enabled.
  3. enabled: true
  4. # the supported types.
  5. supported:
  6. # (default: true) convert number-like strings into numbers.
  7. numbers: true
  8. # (default: true) parse JSON strings into objects or arrays.
  9. json: true
  10. # (default: true) convert known keywords (null, true, false) into their literal values.
  11. keywords: true
  12. # (default: true) support for wrapping strings in single or double quotes.
  13. # Note: this allows you to specify whitespace strings inside of gherkin tables!
  14. literals: true
  15. # (default: true) remove any hidden or zero-width spaces.
  16. stripHiddenSpaces: true

Templates

This package allows for all step definition parameters including tables to be processed through a templating engine, which allows for the scenario context and any other relevant sources of data to be referenced directly from the feature file. Common uses for this functionality include:

  • Handling identifiers which change for every test
  • Updating information based on data generated earlier in the scenario
  • Creating more flexible and reusable step definitions

Supported Engines

Add the following section to your cucumber-extra.yaml file:

  1. templates:
  2. # (default: true) whether or not to enable templates.
  3. enabled: true
  4. # (default: handlebars) which template engine to use.
  5. engine: handlebars

Using templating in a scenario:

  1. Scenario: creating and then modifying an object
  2. Given a "user" object is created in the system:
  3. | name |
  4. | Rob |
  5. When object "{{lastObject.id}}" is updated:
  6. | name |
  7. | {{lastObject.name}}ert |

Note: the scenario context this is automatically added to the template context.

Adding additional objects to the templating context:

  1. const { addContext } = require('cucumber-extra');
  2. // load your config into the context to reuse commonly needed values
  3. addContext(require('config'));

Processing a value with the type and templating system:

  1. const { processValue } = require('cucumber-extra');
  2. contexts = []; // additional context objects to add.
  3. const value = getValue("{{someValue}}", ...contexts);

Adding a custom templating engine:

  1. const { addEngine } = require('cucumber-extra');
  2. addEngine('custom-engine', (value, context) => {
  3. let result = value;
  4. // ... do something with the value and the context.
  5. return result;
  6. });

Template engines such as lodash templates and ejs are not supported out-of-the-box because they encourage the use of embedded javascript, which should be avoided if possible.