top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Cucumber expression in Step Definition

Welcome to my blog!!


While working on project using Cucumber BDD approach, we have used cucumber expression in step definition file to reduce our code, Hope this help !!


In blog we will see,

  • What is Step Definition

  • What is Cucumber Expression

  • Example

What is Step Definition?

A Step Definition is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.


To illustrate how this works, look at the following Gherkin Scenario example1:

Scenario: The user is able to navigate to Arrays page

Given The user is on the Array page after logged in

The user is on the Array page after logged in (the text following the Given keyword) will match the following step definition:

java package com.example; import io.cucumber.java.en.Given;

public class StepDefinitions { @Given("The user is on the Array page after logged in") public void the_user_is_on_the_array_page_after_logged_in() { //To Do }

Now suppose you have similar step for Given but with slight difference example2 as

Scenario: The user is able to navigate to Stack page

Given The user is on the Stack page after logged in

It will match the following step definition:

java package com.example;

import io.cucumber.java.en.Given;

public class StepDefinitions { @Given("The user is on the Stack page after logged in") public void the_user_is_on_the_stack_page_after_logged_in() {

//To Do

}


For better reusability, We can use cucumber expression here. This means that it is not necessary to define a new step definition for each step that just differs slightly.


What is Cucumber Expression


Cucumber Expression is an expression type to specify step definitions. Cucumber Expressions is an alternative to Regular Expressions with a more intuitive syntax but you cannot mix Cucumber Expression syntax with Regular Expression syntax in the same expression.

Parameter Type

Description

{int}

Matches integers, for example 71 or -19. Converts to a 32-bit signed integer if the platform supports it.

{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.

For more parameters details : https://github.com/cucumber/cucumber-expressions#readme


As per the above table let's write a Cucumber Expression that matches the above example1 and example2 Gherkin step (the Given keyword has been removed here, as it's not part of the match).

The user is on the "Array page" after logged in

The user is on the "Stack page" after logged in

You can try in below link

https://cucumber.github.io/try-cucumber-expressions/?expression=+The+user+is+on+the+%7Bstring%7D+after+logged+in&parameters=%5B%5D&advanced=0&step=+The+user+is+on+the+%22Stack+page%22+after+logged+in


Now for both Given step only one step definition can be used as :

java package com.example;

import io.cucumber.java.en.Given;


@Given("The user is on the {string} after logged in")

public void the_user_is_on_the_after_logged_in(String pageName) {

String page_name = pageName.replaceAll("\\s+", "");

arrayPage.navigateTo(page_name);

Loggerload.info("The user is on the " + pageName + " after logged in");

}


In our project we have used Cucumber Expression for reusability and reducing code.


Still there is more to learn in cucumber expression , one can find more details on :

https://github.com/cucumber/cucumber-expressions#readme


https://cucumber.io/docs/cucumber/cucumber-expressions/


Thanks !! Happy Learning !! Please provide your valuable comments.





166 views0 comments

Recent Posts

See All
bottom of page