Cucumber Expressions

Profile picture for user devraj

The Cucumber defines its own set of Cucumber Expressions, which are an alternative to Regular expressions. Using Cucumber Expressions, you can use different parameters like int, float, string, etc., inside your step definition expression. You cannot combine Cucumber Expression syntax with Regular Expression syntax in the same expression.

Build-in Parameters type for Cucumber Expressions

Parameter TypeDescription
{int}Matches integers, for example 10 or -10. Converts to a 32-bit signed integer if the platform supports it.
{biginteger}Matches the same as {int}, but converts to a BigInteger if the platform supports it.
{long}Matches the same as {int}, but converts to a 64 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.
{byte}Matches the same as {int}, but converts to an 8 bit signed integer if the platform supports it.
{float}Matches floats, for example 10.5, .5 or -10.5. Converts to a 32 bit float if the platform supports it.
{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.
{word}Matches words without whitespace, for example "Hello" not "Hello World"
{string}Matches single-quoted or double-quoted strings, for example "Hello World" or 'Hello World' (but not Hello World). 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.
{} anonymousMatches anything (/.*/).

Using Cucumber Expression in Java Step Definitions

Using Integer

Then count of item is 10

Step Definition

@Then("count of item is {int}")
public void teststep(int n) {
}	

Using String

Then I should see "Hello World" text

Step Definition

@Then("I should see {string} text")
public void teststep1(String n) {
}

Video Tutorial: Cucumber Expressions