This Informative Tutorial Explains Advantages of Cucumber Gherkin Framework And How To Write Automation Scripts Using Gherkin Language with Clear Examples:
Cucumber is a tool based on Behavior Driven Development (BDD) framework. BDD is a methodology to understand the functionality of an application in the simple plain text representation.
The main aim of the Behavior Driven Development framework is to make various project roles such as Business Analysts, Quality Assurance, Developers, etc understand the application without diving deep into the technical aspects.
Cucumber tool is generally used in real-time to write acceptance tests of an application. Cucumber tool provides support for many programming languages such as Java, Ruby, .Net, etc. It can be integrated with multiple tools such as Selenium, Capybara, etc.
Table of Contents:
What Is Gherkin?
Gherkin is the language used by Cucumber tool. It is a simple English representation of the application behavior. Cucumber uses the concept of feature files for documentation purposes. Content within the feature files is written in Gherkin language.
In the following topics, we will see more about the advantages of Cucumber Gherkin framework, Integrating Cucumber with Selenium, Creating a feature file & its corresponding step definition file and a sample feature file.
Common Terms For Cucumber Gherkin Framework
Cucumber Gherkin framework uses certain keywords that are essential for writing a feature file.
The following terms are most commonly used in feature files:
#1) Feature:
A feature file must provide a high-level description of an Application Under Test (AUT). The first line of the feature file must start with the keyword ‘Feature’ following the description of an application under test. As per the standards prescribed by Cucumber, the feature file must include the following three elements as the first line.
- Feature Keyword
- Feature Name
- Feature Description (optional)
The feature keyword must be followed by a feature name. It can include an optional description section that can span across multiple lines of the feature file. A feature file has the extension .feature.
#2) Scenario:
A scenario is a test specification of the functionality to be tested. Ideally, a feature file can contain one or more scenario as a part of the feature. A scenario includes multiple test steps. As per the cucumber standards, a scenario must include 3-5 test steps as lengthy scenarios tend to lose their expressive power once the number of steps increases.
A scenario can include the following steps:
- Action to be performed by a user.
- Expected Results of the action.
In Gherkin language, a scenario must include the following keywords:
- Given
- When
- Then
- And
Given:
Given keyword is used to specify the preconditions for executing a specific scenario. A scenario may include more than one Given statements or there can be no Given statements for a scenario.
When:
This keyword is used to specify the action or an event performed by the user such as clicking on a button, entering data onto textbox etc. There can be multiple when statements in a single scenario.
Then:
Then keyword is used to specify the expected outcome of an action performed by the user. Ideally, When keyword must be followed by Then keyword to understand the expected result of user actions.
And:
And keyword is used as a conjunction keyword to combine multiple statements. For Example, multiple Given and When statements in a scenario can be combined using the keyword ‘And’.
#3) Scenario Outline:
A scenario outline is a way of parameterization of scenarios.
This is ideally used when the same scenario needs to be executed for multiple sets of data, but the test steps remain the same. Scenario Outline must be followed by the keyword ‘Examples’, which specify the set of values for each parameter.
Below is the example to understand the concept of Scenario Outline:
Example:
Scenario Outline: Upload a file
Given that a user is on upload file screen.
When the user clicks on the Browse button.
And user enters <filename> onto upload textbox.
And user clicks on the enter button.
Then verifies that file upload is successful.
Examples:
|filename|
|file1|
|file2|
Parameters within the scenario outline must be specified with the characters ‘<’ and ‘>’. A list of data values for the parameter must be specified using the Pipe (|) symbol.
#4) Background:
Background keyword is used to group multiple given statements into a single group.
This is generally used when the same set of given statements are repeated in each scenario of a feature file. Instead of specifying the given statements for each scenario repeatedly, they can be specified with the keyword ‘Background’ before the first scenario.
Example:
Background:
Given user is on the application login page
Advantages Of Cucumber Gherkin Framework
Given below are the advantages of Cucumber Gherkin framework that make Cucumber an ideal choice for rapidly evolving Agile methodology in today’s corporate world:
- Cucumber is an open-source tool.
- Plain Text representation makes it easier for non-technical users to understand the scenarios.
- It bridges the communication gap between various project stakeholders such as Business Analysts, Developers, and Quality Assurance personnel.
- Automation test cases developed using the Cucumber tool are easier to maintain and understand.
- Easy to integrate with other tools such as Selenium and Capybara.
Integration of Cucumber With Selenium
Cucumber and Selenium are the two most powerful functional testing tools. Integration of Cucumber with Selenium Webdriver helps various non-technical members of a project team to understand the application flow.
Given below are the steps to be followed for the integration of Cucumber with Selenium Webdriver:
Step #1:
Cucumber can be integrated with the Selenium Webdriver by downloading the necessary JAR files.
Given below is the list of JAR files that are to be downloaded for using Cucumber with Selenium Webdriver:
- cobertura-2.1.1.jar
- cucumber-core-1.2.2.jar
- cucumber-java-1.2.2.jar
- cucumber-junit-1.2.2.jar
- cucumber-jvm-deps-1.0.3.jar
- cucumber-reporting-0.1.0.jar
- gherkin-2.12.2.jar
- hamcrest-core-1.3.jar
- junit-4.11.jar
The above JAR Files can be downloaded from the Maven website.
Each of the above JAR Files must be downloaded individually from the above website.
Step #2:
Create a new project in the Eclipse and add the above JAR files to the project. To add the JAR files to the project, right-click on the project -> Build Path -> Configure Build Path.
Click on Add External JAR’s button and add the list of above JAR files to the project.
Step #3:
Before creating the feature files and step definition files, we need to install a Natural plugin into Eclipse. It can be done by copying and pasting the URL onto Help -> Install New Software -> URL
Click on the Next button to install the plugin into Eclipse.
Creating a Feature File
Create separate folders for feature files and step definition files in the project structure. Step definition files include Java coding lines while the feature file contains English statements in the form of Gherkin language.
- Create a separate folder for storing feature file by Right Click on the project -> New -> Package.
- Feature file can be created by navigating to Right Click on the project/package -> New -> File.
- Provide a name for the feature file. Feature file must be followed by the extension .feature
- Project structure must look like the below structure.
Creating a Step Definition File
Each step of the feature file must be mapped to a corresponding step definition. Tags used on the Cucumber Gherkin file must be mapped to its step definition by using the tags @Given, @When and @Then.
The following is the syntax of a step definition file:
Syntax:
@TagName (“^Step Name$”)
Public void methodName ()
{
Method Definition
}
Step names must be prefixed with the symbol carat (^) and suffixed with the symbol ($). Method name can be any valid name that is acceptable as per Java coding standards. Method definition includes coding statements in Java or any other programming language of tester’s choice.
Feature File And Step Definition File Examples
For creating feature file and step definition file, the following scenario can be used:
Scenario:
- Open the Login page of an application under test.
- Enter the username
- Enter the password
- Click on the Login button.
- Verify if the user login is successful.
Feature File:
The above scenario can be written in the form of a feature file as below:
Feature: Login into an application under test.
Scenario: Login to the application.
Given Open Chrome browser and launch the application.
When User enters username onto the UserName field.
And User enters a password into the Password field.
When User clicks on the Login button.
Step Definition File:
In the above feature, a file can be mapped to its corresponding step definition file as shown below. Please note that in order to provide a link between the feature file and step definition file, a test runner file must be created.
Below is the representation of step definition file as per its feature file.
package com.sample.stepdefinitions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; import cucumber.api.java.en.When; public class StepDefinition { WebDriver driver; @Given("^Open Chrome browser and launch the application$") public void openBrowser() { driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("www.facebook.com"); } @When("^User enters username onto the UserName field$") public void enterUserName() { driver.findElement(By.name("username")).sendKeys("test@demo.com"); } @And("^User enters password onto the Password field$") public void enterPassword() { driver.findElement(By.name("password")).sendKeys("test@123"); } @When("^User clicks on Login button$") public void clickOnLogin() { driver.findElement(By.name("loginbutton")).click(); } }
TestRunner class is used to provide the link between feature file and step definition file. Below is the sample representation of how TestRunner class looks like. A TestRunner class is generally an empty class with no class definition.
Package com.sample.TestRunner import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions(features="Features",glue={"StepDefinition"}) public class Runner { }
We need to run the TestRunner class file for execution of feature files and step definition files.
Examples
Below is the feature file representation of various scenarios.
Example #1:
To verify if the username and password are available on the login page:
Feature: Verify the display of username and password fields on a login page.
Scenario: To verify the display of username and password fields.
Given User opens Firefox browser and navigates to the Application Under Test.
When User navigates to a Login page.
Then Verify the display of username field on the Login page.
And Verify the display of password field on the Login page.
Example #2:
Below is the example for scenario outline keyword in Cucumber Gherkin:
Feature: Verify if the login is successful for multiple sets of test data.
Scenario Outline: To verify if login is successful for multiple sets of test data.
Given Open Chrome browser and launch the application.
When User enters <username> onto the UserName field.
And User enters <password> onto the Password field.
When User clicks on the Login button.
Examples:
|username|password|
|user1|password1|
|user2|password2|
Conclusion
- BDD is a methodology to understand the functionality of an application in simple plain text representation.
- Cucumber is a tool that uses Behaviour Driven Development to write acceptance tests of an application. It is used to bridge the communication gap between various project stakeholders.
- The main use of Cucumber lies in its simplicity to understand the use of feature files by non-technical users.
It’s not too late to give a try to this Cucumber tool using Gherkin language.