npm install @cucumber/cucumber-expressions

Cucumber Expressions - a simpler alternative to Regular Expressions

About @cucumber/cucumber-expressions

The npm package "@cucumber/cucumber-expressions" offers a revolutionary approach to defining step definitions in behavior-driven development (BDD) frameworks like Cucumber. This node module serves as a simpler and more intuitive alternative to regular expressions, making it easier for developers to write and maintain tests. It specifically targets those who work with Gherkin-based test frameworks, providing tools that allow for more readable and expressive test cases. The main purpose of "@cucumber/cucumber-expressions" is to enhance the clarity and efficiency of test scripting, thereby reducing the potential for errors and improving collaboration across technical and non-technical team members. This package supports various programming languages, ensuring a wide applicability and integration into different development environments.

To start using "@cucumber/cucumber-expressions" in your project, you can easily add it by running the command `npm install @cucumber/cucumber-expressions` in your terminal. This command installs the package into your node project, making it readily available for importing into your test suites. Once installed, "@cucumber/cucumber-expressions" enables you to convert Cucumber step definitions into meaningful and human-readable expressions without the complexities of traditional regular expressions. This feature not only speeds up the development process but also enhances the maintainability of the test code. The package is continually updated to support the latest versions of Cucumber, ensuring compatibility and robustness in BDD testing scenarios.

"@cucumber/cucumber-expressions" is designed to improve the user experience for both developers and stakeholders involved in the development process. By simplifying the way parameters are expressed in Cucumber, this package helps in creating more understandable and concise test documentation. This clarity plays a crucial role in BDD methodologies, where clear communication and shared understanding are key to successful project outcomes. Moreover, the module's compatibility with multiple programming languages broadens its usability, making it an essential tool for developers looking to optimize their BDD strategy.

More from cucumber

cucumber npm packages

Find the best node modules for your project.

Search npm

@cucumber/cucumber-expressions

Cucumber Expressions - a simpler alternative to Regular...

Read more
,

@cucumber/tag-expressions

Cucumber Tag Expression...

Read more

Documentation

A README file for the @cucumber/cucumber-expressions code repository. View Code

test-go test-java test-javascript test-python test-ruby test-dotnet

Cucumber Expressions

Cucumber Expressions is an alternative to Regular Expressions with a more intuitive syntax.

Try Cucumber Expressions in your browser.

Cucumber supports both Cucumber Expressions and Regular Expressions for defining Step Definitions, but you cannot mix Cucumber Expression syntax with Regular Expression syntax in the same expression.

On platforms that don't have a literal syntax for regular expressions (such as Java), Cucumber will create a Cucumber Expression by default. To use Regular Expressions, add anchors (starting with ^ and ending with $) or forward slashes (/). For more information, see Cucumber Expression - Java Heuristics.

Introduction

Let's write a Cucumber Expression that matches the following Gherkin step (the Given keyword has been removed here, as it's not part of the match).

I have 42 cucumbers in my belly

The simplest Cucumber Expression that matches that text would be the text itself, but we can also write a more generic expression, with an int output parameter:

I have {int} cucumbers in my belly

When the text is matched against that expression, the number 42 is extracted from the {int} output parameter and passed as an argument to the step definition.

The following text would not match the expression:

I have 42.5 cucumbers in my belly

This is because 42.5 has a decimal part, and doesn't fit into an int. Let's change the output parameter to float instead:

I have {float} cucumbers in my belly

Now the expression will match the text, and the float 42.5 is extracted.

Parameter types

Text between curly braces reference a parameter type. Cucumber comes with the following built-in parameter types:

Parameter Type Description
{int} Matches integers, for example 71 or -19. Converts to a 32-bit signed integer if the platform supports it.
{float} Matches floats, for example 3.6, .8 or -9.2. Converts to a 32 bit float if the platform supports it.
{word} Matches words without whitespace, for example banana (but not banana split).
{string} Matches single-quoted or double-quoted strings, for example "banana split" or 'banana split' (but not banana split). Only the text between the quotes will be extracted. The quotes themselves are discarded. Empty pairs of quotes are valid and will be matched and passed to step code as empty strings.
{} anonymous Matches anything (/.*/).
{bigdecimal} Matches the same as {float}, but converts to a BigDecimal if the platform supports it.
{double} Matches the same as {float}, but converts to a 64 bit float if the platform supports it.
{biginteger} Matches the same as {int}, but converts to a BigInteger if the platform supports it.
{byte} Matches the same as {int}, but converts to an 8 bit signed integer if the platform supports it.
{short} Matches the same as {int}, but converts to a 16 bit signed integer if the platform supports it.
{long} Matches the same as {int}, but converts to a 64 bit signed integer if the platform supports it.

Java

The Anonymous Parameter

The anonymous parameter type will be converted to the parameter type of the step definition using an object mapper. Cucumber comes with a built-in object mapper that can handle all numeric types as well as. Enum.

To automatically convert to other types it is recommended to install an object mapper. See cucumber-java - Default Transformers to learn how.

Number formats

Java supports parsing localised numbers. I.e. in your English feature file you can format a-thousand-and-one-tenth as '1,000.1; while in French you would format it as '1.000,1'.

Parsing is facilitated by Javas DecimalFormat and includes support for the scientific notation. Unfortunately the default localisation include symbols that can not be easily written on a regular keyboard. So a few substitutions are made:

If support for your preferred language could be improved, please create an issue!

Custom Parameter types

Cucumber Expressions can be extended so they automatically convert output parameters to your own types. Consider this Cucumber Expression:

I have a {color} ball

If we want the {color} output parameter to be converted to a Color object, we can define a custom parameter type in Cucumber's configuration.

The table below explains the various arguments you can pass when defining a parameter type.

Argument Description
name The name the parameter type will be recognised by in output parameters.
regexp A regexp that will match the parameter. May include capture groups.
type The return type of the transformer.
transformer A function or method that transforms the match from the regexp. Must have arity 1 if the regexp doesn't have any capture groups. Otherwise the arity must match the number of capture groups in regexp.
useForSnippets / use_for_snippets Defaults to true. That means this parameter type will be used to generate snippets for undefined steps. If the regexp frequently matches text you don't intend to be used as arguments, disable its use for snippets with false.
preferForRegexpMatch / prefer_for_regexp_match Defaults to false. Set to true if you have step definitions that use regular expressions, and you want this parameter type to take precedence over others during a match.

Java

@ParameterType("red|blue|yellow")  // regexp
public Color color(String color){  // type, name (from method)
    return new Color(color);       // transformer function
}

Kotlin

@ParameterType("red|blue|yellow")   // regexp
fun color(color: String): Color {   // name (from method), type
    return Color(color)             // transformer function
}                                    

Scala

ParameterType("color", "red|blue|yellow") { color: String => // name, regexp
    Color(color)                                             // transformer function, type
}                                    

JavaScript / TypeScript

import { defineParameterType } from '@cucumber/cucumber'

defineParameterType({
    name: 'color',
    regexp: /red|blue|yellow/,
    transformer: s => new Color(s)
})

The transformer function may return a Promise.

Ruby

ParameterType(
  name:        'color',
  regexp:      /red|blue|yellow/,
  type:        Color,
  transformer: ->(s) { Color.new(s) }
)

.NET / SpecFlow

[StepArgumentTransformation("(red|blue|yellow)")]
public Color ConvertColor(string colorValue)
{
    return new Color(colorValue);
}

Note: Currently the parameter name cannot be customized, so the custom parameters can only be used with the type name, e.g. {Color}.

Python

ParameterType(
  name=        "color",
  regexp=      "red|blue|yellow",
  type=        Color,
  transformer= lambda s: Color(s),
)

Optional text

It's grammatically incorrect to say 1 cucumbers, so we should make the plural s optional. That can be done by surrounding the optional text with parentheses:

I have {int} cucumber(s) in my belly

That expression would match this text:

I have 1 cucumber in my belly

It would also match this text:

I have 42 cucumbers in my belly

In Regular Expressions, parentheses indicate a capture group, but in Cucumber Expressions they mean optional text.

Alternative text

Sometimes you want to relax your language, to make it flow better. For example:

I have {int} cucumber(s) in my belly/stomach

This would match either of those texts:

I have 1 cucumber in my belly
I have 1 cucumber in my stomach
I have 42 cucumbers in my stomach
I have 42 cucumbers in my belly

Alternative text only works when there is no whitespace between the alternative parts.

Escaping

If you ever need to match () or {} literally, you can escape the opening ( or { with a backslash:

I have {int} \{what} cucumber(s) in my belly \(amazing!)

This expression would match the following examples:

I have 1 {what} cucumber in my belly (amazing!)
I have 42 {what} cucumbers in my belly (amazing!)

You may have to escape the \ character itself with another \, depending on your programming language. For example, in Java, you have to use escape character \ with another backslash.

I have {int} \\{what} cucumber(s) in my belly \\(amazing!)

Then this expression would match the following example:

I have 1 \{what} cucumber in my belly \(amazing!)
I have 42 \{what} cucumbers in my belly \(amazing!)

The / character will always be interpreted as an alternative, unless escaped, such as with \/.

Architecture

See ARCHITECTURE.md

Acknowledgements

The Cucumber Expression syntax is inspired by similar expression syntaxes in other BDD tools, such as Turnip, Behat and Behave.

Big thanks to Jonas Nicklas, Konstantin Kudryashov and Jens Engel for implementing those libraries.

The Tiny-Compiler-Parser tutorial by Yehonathan Sharvit inspired the design of the Cucumber expression parser.