Cucumber Parameter Tutorial

Now that we have setup an IDE we can start to look at passing Cucumber parameters from a Feature file and into a step definition class. One of the features of a Feature file is the ability to pass a Cucumber Parameter to a step definition class. We can use this ability to help write better step definition methods which can cater for multiple test steps in a Feature file. What this means is that a number of test steps can now reference the same step definition method. Lets have a look at how we can achieve this.

How does a Test Step find a Step Definition

We have talked about how a Feature file actually finds a step definition class (via the glue parameter in Cucumber) but we have not talked about how a test step in a Feature file actually finds a matching step definition in a step definition class. When a Feature file has located a step definition class it uses Regex to match a test step in a Feature file to a step definition method in a step definition class. For the following test step ‘Given I navigate to TheTestRoom.com’ the text after the Gherkin keyword i.e. ‘I navigate to TheTestRoom.com’ is used as the String to match the Regex pattern in a step definition annotation. For instance:

Feature File:
Scenario: Check Cucumber Tutorial page has loaded
Given I navigate to thetestroom.com

And now the step definition:

@Given("^I navigate to thetestroom\\.com$")
public void navigateToTheTestRoom() {
// code here
}

If look at the text for the test step in the Feature file and the expected Regex pattern in the step definition, then you can see that they match. It is the line of text (or String) in the Feature file which is used by Cucumber to actually match a test step to a step definition method.

Regex Patterns to understand

Before we go into detail about how we actually match the text of a test step to a step definition, we need to gain a small understand of Regex patterns used by Cucumber. First of all what is Regex? Regex is a pattern tool which is used to match Strings across any text based documents. We can use Regex to define patterns which we can then use to scan entire documents to help find matching strings. Regex has a number of ways to match Strings but the functions which we are most interested in are listed below:

^ - This marks the beginning of the string
$ - This marks the end of the string
\ - This is used to allow using special characters in a Regex pattern such as " and .
* - This is used to mean either zero, one of more
( ) - The text in these brackets are used to identify grouped characters
[ ] - This text is in these square brackets are used to match a single character using an or expression
. - This text will be used represent any character

Using the above we can combine some of the syntax to get certain types of results. For instance:

^Navigate to thetestroom$ - This will look for an exact match of the String 'Navigate to thetestroom'
^I navigate to thetestroom\\.com$ - This will look for an exact match of the String 'Navigate to thetestroom.com'
^I .* to thetestroom\\.com$- This will look for an exact match of the String 'Navigate to thetestroom.com'

Regex has many more features and methods which allow someone to match Strings but the functions above are enough to get us started with Cucumber. We will be using the above Regex functions to help us match the text in a test step against a step definition method.

Passing Cucumber Parameter

Now that we have gained a simple understand of Regex, lets have a look at how we can pass a Cucumber Parameter from a Feature file and into a step definition class.

We can capture specific sections in a test step to pass as a Cucumber parameter in a a Feature file. I usually do this using double quotation marks in a test step to make it easier for me to capture the parameter which I am about to send. This is also good practice as Cucumber Feature files highlight this as a parameter:

When I click on "Cucumber Java Tutorial"

And in the step definition method we can use Regex to capture all the text in the double quotation marks which we pass from the Feature file like so and capture in a step definition method:

@When("^When I click on \"([^\"]*)\"$")
public void iClickOn(String link){
//code here
}

If you have a look at the above step definition method then you should be able to see that we are able to capture any parameter that we pass from a Feature file test step and into a Step Definition class step definition method. We can then use these parameters to create step definitions which we can use for multiple test steps. This means we can now do the following:

When I click on "Cucumber Java Tutorial"
When I click on "WebDriver Java Tutorial"
When I click on "AngularJS Java Tutorial"

All three above steps can now be used to refer to the same step definition as all the text in each test step now matches the regex pattern in the same step definition method.

And that’s it folks. In this tutorial we have looked at how to pass a Cucumber Parameter from a Feature file and into a step definition class. Using this information we can now easily pass a Cucumber Parameter and really start to take advantage of the power of Cucumber.

Video Tutorial on YouTube

cucumber parameter

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook - Google Plus

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to upload videos on my YouTube channel, write blogs about Software Testing and generally contribute back to the Software Testing world.

Leave a Reply