Learn how to use Parameters in our How-To-Guide on applying Cucumber to lead your agile testing team down the path to automation.
Blog six of a series.
As the number of Scenarios in your project increases, so does the need to make them easy to maintain.
Brittle Scenarios, those which have hardcoded values, require multiple steps to accomplish the same task. Fortunately, Cucumber has provided an elegant solution for agile testing teams.
How to use Parameters in BDD Gherkin Script
Let’s work with the following BDD Gherkin Script:
To demonstrate how parameters work, I am going to write this script out in an imperative format rather than declarative. This will expose the individual steps:
You will notice the steps are highlighted in yellow. If you recall, that is Cucumber telling us it cannot find corresponding step definitions for each. So we need to create the corresponding step definitions (you can add these to the existing step definitions file).
If you ran the .feature file and copy and pasted the four new steps into the step definition file, you will notice a lot of red:
Hovering the cursor over the red tells you that “several step definitions with the same name were found.” And if you look at each step, you will see that Cucumber is right. Each step is identical. So it doesn’t know which one to use.
Working With Step Definitions and Parameters
As your Cucumber framework grows, these kind of error messages can be frustrating because the matching step definition might be hard to find. Fortunately for us, our Cucumber framework is still small. The solution is to simply remove three of the four duplicate steps.
Your step definition has two parameters:
- arg1, which takes the text entered
- arg2, which takes the field you wish to enter the text into.
The values of each are handled by Regular Expressions. We’re not going to get into Regular Expressions here because they are far too complicated.
Just know that when we use the parameter (.*?) it will allow any value to be passed to the step definition. The value being passed down will be treated as a String variable by Cucumber regardless of what you pass.
The values captured in arg1 and arg2 can now be manipulated and used by WebDriver. That means arg1 can be directly passed to WebDriver; arg2, however, needs some manipulation. To do this, I will use a simple if-then-else statement:
Go ahead and run your .feature file. It should be all green. You have just created a step definition that can be used to populate ANY text field you come across on the web.
Parameters can be an extremely powerful and useful tool for agile testing teams. They enable you to write similar steps in a specific way so the step definition can be reused. As a result, scripts can be generated very fast and automated equally as fast.
I would strongly encourage you to read more on Regular Expressions (regex.com) and read our blog on Advanced Cucumber Parameters.