liveBook · Manning

Cucumber expression

As explained in BDD in Action, Second Edition

A Cucumber expression is a pattern notation for matching Gherkin steps and converting captured text into parameters for step definitions.

A Cucumber expression is a pattern in a step definition annotation that matches a Gherkin step and passes parts of its text to automation code. It lets a readable scenario supply data—such as a train line, stations, and departure times—without putting every value into the step definition itself. That binding code is often called glue code. section 3.5.3

Reuse a step definition across scenarios

Instead of writing separate methods for Standard and Silver Frequent Flyer members, use `@Given("{word} is a {word} Frequent Flyer member")`. The captured name and status become method parameters. If the status parameter is an enum, `@Given("{word} is a {} Frequent Flyer member")` can use the anonymous `{}` type to convert the scenario text directly. section 8.4.1

Choose a parameter type for the data the scenario supplies:

  • `{int}` captures a whole number; `{float}` captures a floating-point number. section 8.4.1
  • `{word}` captures one word without whitespace; `{string}` captures text enclosed in double quotes. section 8.4.1
  • `{}` matches more general text and attempts conversion to the step method’s declared parameter type. section 8.4.1
Table 8.1 Built-in Cucumber Expression types (view table figure)

Parameter Type

Description

Example

Matches

{int}

A whole number

There are {int} remaining tasks

There are 5 remaining tasks

{float}

A floating-point number

The box weighs {float} kg

The box weighs 7.5 kg

{word}

A single word (without whitespace)

She buys a {word} jersey

She buys a blue jersey

{string}

A string enclosed in double-quotes

She flies to {string}

She flies to “New York”

{}

Matches any expression (works with enums and most basic types)

She flies to {}

She flies to New York

{biginteger} (only applies to step definitions written in a JVM language)

A BigInteger parameter

A population of {biginteger}

A population of 5,000,000,000

{bigdecimal}

A BigDecimal parameter

A total value of {bigdecimal}

A total value of 99.9876543

{byte}

A Byte parameter

There are {byte} apples

There are 500 apples

{short}

A Short parameter

There are {short} apples

There are 5,000 apples

{long}

A Long parameter

There are {long} apples

There are 50,000,000 apples

{double}

A Double parameter

The box weighs {double} kg

The box weighs 7.4987 kg

Compare the built-in parameter types with the scenario text each one matches before choosing a placeholder. section 8.4.1

Keep small wording differences from forcing duplicate definitions. Parentheses make text optional: `year(s)` matches both “year” and “years.” A slash allows alternative words: `member/customer` accepts either word. Slash alternatives work for words without whitespace; more sophisticated matching calls for regular expressions. section 8.4.2

Map a business step to a task

In Cucumber.js, `When('{actor} signs up using valid traveler details', ...)` matches a scenario step and supplies an actor to a step definition that runs Screenplay Pattern tasks. `{actor}` is not built in: a custom parameter type transforms the captured name into a Serenity/JS actor. A separate `{pronoun}` type can refer to the most recently used actor. section 14.3

How BDD in Action, Second Edition covers it

How deep it goes
Works through typed captures, anonymous conversion, optional text, and word alternatives; reserves more sophisticated matching for regular expressions.
Examples
Uses train timetable steps and Frequent Flyer membership statuses to show captured data and reusable definitions.
What it assumes
Assumes familiarity with Gherkin scenarios and step methods, while introducing Cucumber expression placeholders through examples.
What you can do after
Apply it via Cucumber to match scenario steps, pass captured values to step definitions, and reduce duplicated automation code.
How it connects
Connects Gherkin, step definitions, custom parameter types, and Screenplay Pattern tasks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.