Skip to main content

Cucumber Expressions

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 Type Description
{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.
{} anonymous Matches 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

Tags
Submitted by tgoswami on September 7, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.