Understanding Gherkins - The Language of Behavior Driven Development (BDD) Part 2
Having seen the basics of Gherkins in Part 1, let us look at the different ways to create Scenarios for Gherkins.
Writing Gherkin Scenarios
Let us write Gherkins for Registration page having both Positive & Negative testing scenarios.
Feature: Registration Page
Scenario: Verify Registration functionality with valid credentials
Given User is on Registration page
When User provides valid credentials at fields username, password and password confirmation
And clicks the register button
Then User should be redirected to home page
And user should see a registration confirmation message
Scenario: Verify Registration functionality with invalid credentials
Given User is on Registration page
When User provides invalid credentials at fields username, password and password confirmation
And clicks the register button
Then user should see validation error message for mandatory fields.
Now that we have written Scenarios for Registration feature, we would like to populate test data in these Scenarios and there are different ways to do so. Let us look at them one by one.
(i) Scenario Having Direct Parameters
(ii) Scenario With Data Table
(iii) Scenario Outline With Examples
(i) Scenario Having Direct Parameters: Here we will pass parameters/test data directly inside this step. The Step Definition should accept String arguments to handle the test data. For below example, the Step Definition should accept three string arguments for the three data sets. It is suited when one needs quick tests with one input set.
Feature: Registration Page
Scenario: Verify Registration functionality with valid credentials
Given User is on Registration page
When User provides valid credentials at fields “testuser”, “testpassword” and “testpassword”
And clicks the register button
Then User should be redirected to home page
And user should see a registration confirmation message
Scenario: Verify Registration functionality with invalid credentials
Given User is on Registration page
When User provides invalid text at fields “invaliduser”, “invalidpassword” , "invalidpassword"
And clicks the register button
Then user should see validation error message for mandatory fields.
Let us look at the Step Definition file for Scenario Having Direct Parameters

In Step Definitions, if the successive steps are same, then the step written earlier is utilized, there is no need not write the repeating step again.
e.g. The @ Given and @And has same action for both valid and invalid use cases so same step has been utilized rather than having two separate steps
(ii) Scenario With Data Table: Here we pass the parameters/test data using the data table provided by Cucumber. In this case, the whole Scenario is treated as a Single Test by the Test Runner.
To put it other way, the Scenario will run only once and to make use of the two rows of test data that we have provided, the Step Definition will have to loop over each row in the data table, ensuring that loop runs twice to cover both test data. This approach is suited when working with small datasets or grouped logic.
Feature: Registration Page
Scenario: Verify Registration functionality with valid credentials
Given User is on Registration page
When User provides valid credentials
| username | password | confirmPassword |
| testuser1 | testpassword1 | testpassword1 |
| testuser2 | testpassword2 | testpassword2 |
And clicks the register button
Then User should be able redirected to home page
And user should see a registration confirmation message
Scenario: Verify Registration functionality with invalid credentials
Given User is on Registration page
When User provides invalid credentials
| username | password | confirmPassword |
| invaliduser1 | invalidpassword1 | invalidpassword1 |
| invaliduser2 | invalidpassword2 | invalidpassword2 |
And clicks the register button
Then user should see validation error message for mandatory fields.
Let us look at the Step Definition file for the Scenario Having Data Table

In above Step Definition, at @When annotation, the data is coming in form of a dataTable. The data table is converted into a list of rows, where each row is a list of String. The for loop, loops through each row in the list of rows.
(iii) Scenario Outline With Examples: Here we pass the parameters/testdata using the Examples table provided by Cucumber. In this case, Cucumber automatically executes and substitutes values in the place holders for given number of test data. Also, this approach is suitable for larger input sets and Data Driven tests. Due to its capability for automatic execution of the test data and ability to handle larger input data sets, it has become preferred choice for testing teams.
Feature: Registration Page
Scenario Outline: Verify Registration functionality with valid credentials
Given User is on Registration page
When User provides valid credentials “<username>”,” <password>,” “<confirmPassword>”
And clicks the register button
Then User should be able redirected to home page
And user should see a registration confirmation message
Examples:
| username | password | confirmPassword |
| testuser1 | testpassword1 | testpassword1 |
| testuser2 | testpassword2 | testpassword2 |
Scenario Outline: Verify Registration functionality with invalid credentials
Given User is on Registration page
When User provides invalid credentials “<username>”, “<password>”,<"confirmPassword">
And clicks the register button
Then user should see validation error message for mandatory fields
Examples:
|username | password | confirmPassword |
| invaliduser1 | invalidpassword1 | invalidpassword1 |
| invaliduser2 | invalidpassword2 | invalidpassword2 |
Let us look at the Step Definition file for Scenario Outline With Examples

In above Step definition, Scenario Outline with placeholders like <username>, will be automatically substituted with values from Examples. Step parameters having {string} in the definition catch these substitutions and multiple scenarios can be combined into one reusable step definition using multiple @ When annotation.
To Summarize, use:-
Scenario Having Direct Parameters when quick test with one input set is needed
Scenario With Data Table when working with small datasets or grouped logic
Scenario With Examples when working with Data Driven Tests or larger input data sets.


