Cucumber Quick Guide
Cucumber Quick Guide
Cucumber - Overview
In order to get better advantage of the software testing, organizations are nowadays
taking a step forward. They implement important acceptance test scenarios while
development is in-progress. This approach is commonly known as Behavior Driven
Development (BDD).
Behavior Driven Development gives us an opportunity to create test scripts from both
the developer’s and the customer’s perspective as well. So in the beginning, developers,
project managers, QAs, user acceptance testers and the product owner (stockholder),
all get together and brainstorm about which test scenarios should be passed in order to
call this software/application successful. This way they come up with a set of test
scenarios. All these test scripts are in simple English language, so it serves the purpose
of documentation also.
Example
If we are developing a user authentication feature, then the following can be few key
test scenarios, which needs to get passed in order to call it a success.
The user should be able to login with correct username and correct password.
The user should not be able to login with incorrect username and correct
password.
The user should not be able to login with correct username and incorrect
password.
How it Works
By the time the code is ready, test scripts are ready too. The code has to pass the test
scripts defined in BDD. If it does not happen, code refactoring will be needed. Code gets
freezed only after successful execution of defined test scripts.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 1/62
12/18/2018 Cucumber Quick Guide
It is a very simple notion, but what we need in order to get this concept implemented.
The answer is, Behavior Driven Development (BDD) Framework. Cucumber is one such
open source tool, which supports behavior driven development. To be more precise,
Cucumber can be defined as a testing framework, driven by plain English text. It serves
as documentation, automated tests, and a development aid – all in one.
Cucumber reads the code written in plain English text (Language Gherkin – to be
introduced later in this tutorial) in the feature file (to be introduced later).
It finds the exact match of each step in the step definition (a code file - details provided
later in the tutorial).
The piece of code to be executed can be different software frameworks like Selenium,
Ruby on Rails, etc. Not every BDD framework tool supports every tool.
This has become the reason for Cucumber's popularity over other frameworks, like
JBehave, JDave, Easyb, etc.
Ruby on Rails
Selenium
PicoContainer
Spring Framework
Watir
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 2/62
12/18/2018 Cucumber Quick Guide
It allows the test script to be written without knowledge of any code, it allows
the involvement of non-programmers as well.
Cucumber - Environment
In this chapter, we will see the environment setup for Cucumber with Selenium
WebDriver and Java, on Windows Machine.
Java
Why we need − Java is a robust programming language. Cucumber supports Java
platform for the execution.
How to install −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 3/62
12/18/2018 Cucumber Quick Guide
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 4/62
12/18/2018 Cucumber Quick Guide
Eclipse
Why we need − Eclipse is an Integrated Development Environment (IDE). It contains
a base workspace and an extensible plug-in system for customizing the environment.
How to install −
Maven
Why we need − Maven is a build automation tool used primarily for Java projects. It
provides a common platform to perform activities like generating source code,
compiling code, packaging code to a jar, etc. Later if any of the software versions gets
changed, Maven provides an easy way to modify the test project accordingly.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 5/62
12/18/2018 Cucumber Quick Guide
How to install −
Step 4 − Edit Path variable and include Maven as shown in the following screenshot.
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can
choose any name, which is in lowercase). Click on Finish.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 6/62
12/18/2018 Cucumber Quick Guide
Step 3 − Add dependency for selenium: This will indicate Maven which Selenium jar
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 7/62
12/18/2018 Cucumber Quick Guide
Step 4 − Add dependency for Cucumber-Java: This will indicate Maven, which
Cucumber files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Step 5 − Add dependency for Cucumber-JUnit: This will indicate Maven, which
Cucumber JUnit files are to be downloaded from the central repository to the local
repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Step 6 − Add dependency for JUnit: This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
You will be able to see a Maven repository like shown in the following screenshot.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 8/62
12/18/2018 Cucumber Quick Guide
Cucumber - Gherkins
So far, we have got an understanding of Cucumber and what it does. It executes the
test scripts, which have been defined in the feature file (to be covered in subsequent
chapters). The language, in which this executable feature files is written, is known as
Gherkin. Gherkin is a plain English text language, which helps the tool - Cucumber to
interpret and execute the test scripts.
One may think that, it has been discussed many times that Cucumber supports simple
English text then why we need a separate language - Gherkins. The answer lies in the
concept of the Behavior Driven Development.
As discussed earlier, we had seen that BDD incorporates different prospectives while
creating test scripts. It can be development prospective, business prospective, etc. That
said, we will need people from different community like developers, project managers,
product owners, and testers while developing test scripts. As these people do not
belong to the same category, there is a risk of not using the common language for test
script conceptualizing. This was the evolution point for Gherkins.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 9/62
12/18/2018 Cucumber Quick Guide
Gherkin provides the common set of keywords in English text, which can be used by
people amongst the different community and yet get the same output in the form of
test scripts.
Example
Feature − Login functionality for a social networking site. Given I am a social
networking site user. When I enter username as username1. And I enter password as
password1. Then I should be redirected to the home page of the site.
The above-mentioned scenario is of a feature called user login. All the words
highlighted in bold are Gherkin keywords.
Background
But
Scenario Outline
Examples
Gherkin will parse each step written in step definition file (to be covered later). So the
steps mentioned in the feature file and the step definition file (to be covered later)
should match.
You can locate the Gherkin jars in the Maven Dependency folder in the Package
Explorer. It gets downloaded along with the other Cucumber jars. It will look like the
following screenshot −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 10/62
12/18/2018 Cucumber Quick Guide
Another interesting fact about Gherkin is, it supports not only English but many other
native languages such as French, Finnish, Indonesian, Hungarian, Hindi, Urdu, Gujarati,
etc.
Cucumber - Features
A Feature can be defined as a standalone unit or functionality of a project. Let’s take a
very common example of a social networking site. How does the feature of this
product/project look like? Few basic features can be determined as −
Create and remove the user from the social networking site.
Logout.
By now, it is clear that, each independent functionality of the product under test can be
termed as a feature when we talk about Cucumber. It is a best practice later when you
start testing, that before deriving the test scripts, we should determine the features to
be tested.
A feature usually contains a list of scenarios to be tested for that feature. A file in which
we store features, description about the features and scenarios to be tested is known as
Feature File. We will see more about feature files in the following chapter.
The keyword to represent a feature under test in Gherkins is “Feature”. The suggested
best practice is, to write a small description of the feature beneath the feature title in
the feature file. This will fulfill the need of a good documentation as well.
Example
Feature − Login functionality for a social networking site.
The user should be able to login into the social networking site if the username and the
password are correct.
The user should be shown the error message if the username and the password are
incorrect.
The user should be navigated to home page, if the username and password are correct.
Feature Files
The file, in which Cucumber tests are written, is known as feature files. It is advisable
that there should be a separate feature file, for each feature under test. The extension
of the feature file needs to be “.feature”.
One can create as many feature files as needed. To have an organized structure, each
feature should have one feature file.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 11/62
12/18/2018 Cucumber Quick Guide
For Example −
The naming convention to be used for feature name, feature file name depends on the
individual’s choice. There is no ground rule in Cucumber about names.
When − Specific condition which should match in order to execute the next
step.
Example
Feature − User login on social networking site.
The user should be able to login into the social networking site when the username and
the password are correct.
The user should be shown an error message when the username and the password are
incorrect.
The user should be navigated to the home page if the username and the password are
correct.
The given user navigates to Facebook. When I enter Username as "<username>" and
Password as "<password>". Then, login should be unsuccessful.
| username | password |
| username1 | password1 |
* AND keyword is used to show conjunction between two conditions. AND can be used
with any other keywords like GIVEN, WHEN and THEN.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 12/62
12/18/2018 Cucumber Quick Guide
Steps Definitions
We have got our feature file ready with the test scenarios defined. However, this is not
the complete job done. Cucumber doesn’t really know which piece of code is to be
executed for any specific scenario outlined in a feature file.
This calls the need of an intermediate – Step Definition file. Steps definition file stores
the mapping between each step of the scenario defined in the feature file with a code of
function to be executed.
So, now when Cucumber executes a step of the scenario mentioned in the feature file,
it scans the step definition file and figures out which function is to be called.
So with each function, whatever code you want to execute with each test step (i.e.
GIVEN/THEN/WHEN), you can write it within Step Definition file. Make sure that
code/function has been defined for each of the steps.
This function can be Java functions, where we can use both Java and Selenium
commands in order to automate our test steps.
Cucumber - Scenarios
Scenario is one of the core Gherkin structures. Every scenario starts with the keyword
“Scenario:” (or localized one) and is followed by an optional scenario title. Each feature
can have one or more scenarios and every scenario consists of one or more steps. A
very simple example of scenario can be −
When the user clicks on Help, then the Help page opens.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 13/62
12/18/2018 Cucumber Quick Guide
Consider a case, where we need to execute a test scenario more than once. Suppose,
we need to make sure that the login functionality is working for all types of subscription
holders. That requires execution of login functionality scenario multiple times. Copy
paste the same steps in order to just re-execute the code, does not seem to be a smart
idea. For this, Gherkin provides one more structure, which is scenario outline.
Scenario outline is similar to scenario structure; the only difference is the provision of
multiple inputs. As you can see in the following example, the test case remains the
same and non-repeatable. At the bottom we have provided multiple input values for the
variables “Username” and “Password”. While running the actual test, Cucumber will
replace the variable with input values provided and it will execute the test. Once pass-1
has been executed, the test will rerun for second iteration with another input value.
Such variable or placeholders can be represented with ”<>” while mentioning with
gherkin statements.
Example
Scenario Outline − Login functionality for a social networking site. The given user
navigates to Facebook.
When the user logs in using the Username as "<username>" and the Password as "
<password>", then login should be successful.
| username | password |
| user1 | password1 |
| user2 | password2 |
There are a few tips and tricks to smartly define the Cucumber scenarios.
Each step should be clearly defined, so that it does not create any confusion for
the reader.
Do not repeat the test scenario, if needed use scenario outline to implement
repetition.
Develop a test step in a way that, it can be used within multiple scenarios and
scenario outlines.
As far as possible, keep each step completely independent. For example: “Given
the user is logged in”. This can be divided into two steps
Clicks on login.
Cucumber - Annotations
Annotation is a predefined text, which holds a specific meaning. It lets the
compiler/interpreter know, what should be done upon execution. Cucumber has got the
following few annotations −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 14/62
12/18/2018 Cucumber Quick Guide
Given −
When −
Then −
And −
It provides the logical AND condition between any two statements. AND
can be used in conjunction with GIVEN, WHEN and THEN statement.
But −
Example − THEN login should be successful. BUT home page should not
be missing.
Scenario −
Details about the scenario under the test needs to be captured after the
keyword “Scenario:”
Example −
Scenario:
WHEN I enter my
AND I enter my
Background −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 15/62
12/18/2018 Cucumber Quick Guide
Example −
Background:
Example Scenario
Let’s automate a scenario in order to understand annotations better.
Step 1
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can
choose any name which is in lowercase).
Click on Finish.
Open pom.xml −
Add dependency for Selenium − This will indicate Maven, which Selenium jar
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 16/62
12/18/2018 Cucumber Quick Guide
Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Add dependency for JUnit − This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Verify binaries.
Step 2
Name it as ‘Annotation’.
Save it.
Step 3
Write the following text within the file and save it.
Feature: annotation
#This is how background can be used to eliminate duplicate steps
Background:
User navigates to Facebook Given
I am on Facebook login page
Step 4
Write the following text within the file and save it.
package Annotation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 18/62
12/18/2018 Cucumber Quick Guide
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
Step 5
Write the following text within the file and save it.
package Annotation;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 19/62
12/18/2018 Cucumber Quick Guide
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
Step 6
You will observe the following things when you run this class file −
Step result 1. to 5. Will be re-executed for username as "" and password as "".
Let’s continue with the same example of Facebook login feature. So far we have been
executing one scenario: Upon providing the correct user name, login is successful. Now,
suppose we want to check if login is successful for all three possible types of inputs,
which are username, email address, or phone number. To achieve this, we will need to
write three different scenarios, where each scenario will vary with the type of input,
login is successful. In this case, scenarios will look like the following.
Scenario:
Scenario:
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 20/62
12/18/2018 Cucumber Quick Guide
Scenario:
Here, if we see closely, for above three scenarios: statements are the same, only the
input parameter (username/email address/phone number) is changing. That’s where
the importance of scenario outline comes into picture.
When we define any scenario with scenario outline, we can specify one test scenario
and at the bottom of it we can provide a number of inputs. The scenario will get
executed as many times as the number of inputs provided.
Example
Let’s create an example of a scenario outline −
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can
choose any name which is in lowercase).
Click on Finish.
Open pom.xml −
Add dependency for Selenium − This will indicate Maven, which Selenium jar
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 21/62
12/18/2018 Cucumber Quick Guide
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Add dependency for Cucumber-Java: This will indicate Maven, which Cucumber
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Add dependency for Cucumber-JUnit: This will indicate Maven, which Cucumber
JUnit files are to be downloaded from the central repository to the local
repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Add dependency for JUnit - This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Add dependency for JUnit - This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 22/62
12/18/2018 Cucumber Quick Guide
<scope>test</scope>
</dependency>
Verify binaries.
Write the following text within the file and save it.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 23/62
12/18/2018 Cucumber Quick Guide
Example −
| username | password |
| username1 | password1 |
| username2 | password2 |
Note − Here, example annotation describes the range of input to be provided upon
scenario execution. Test scenario will be executed for each of the input provided. So, in
the given example, test scenario will be executed three times.
Write the following text within the file and save it.
package Outline;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 24/62
12/18/2018 Cucumber Quick Guide
}
}
Note − In the above code, we have to define a function having two input argument:
one username and other will be for password. So for each set of input provided in the
example tag, Set of GIVEN, WHEN and THEN will be executed.
Write the following text within the file and save it.
package Outline;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
You will observe the following things when you run this class file
In nutshell, when scenario does not change but only the data value gets changed, it is
advisable to use scenario outline data tables.
Cucumber - Tags
It looks simple when we just have one, two, or maybe five scenarios in a feature file.
However, in real life it does not happen. For each feature under test, we may have 10,
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 25/62
12/18/2018 Cucumber Quick Guide
20, or may be more number of scenarios in a single feature file. They may represent
different purpose (Smoke test/Regression test), different prospectives
(Developer/QA/BA), different status (Ready for execution/Work in progress), etc. How
to manage execution for such a mass?
For this, Cucumber has already provided a way to organize your scenario execution by
using tags in feature file. We can define each scenario with a useful tag. Later, in the
runner file, we can decide which specific tag (and so as the scenario(s)) we want
Cucumber to execute. Tag starts with “@”. After “@” you can have any relevant text to
define your tag. Let’s understand this with an example.
Suppose, there are two or more scenarios in a feature file. We want to execute only one
scenario as part of smoke test. So first thing is to identify that scenario and second is to
tag it with “@SmokeTest” text at the beginning of the scenario. Let’s take a deep look at
it −
Write the following text within the file and save it. This feature file contains two
scenarios where only one has been marked as SmokeTest tag.
Examples
| username | password |
| username1 | password1 |
| username2 | password2 |
#following scenario has been tagged as SmokeTest and this should get executed.
@SmokeTest
Scenario:
Write the following text within the file and save it.
package cucumberTag;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 27/62
12/18/2018 Cucumber Quick Guide
package cucumberTag;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
You will observe the following things, when you run this class file.
There is no limit in defining tags within the feature file. Based on your need, you can
derive tags to be used and scenarios to be executed.
Custom tag − Custom tag provides you full flexibility to choose appropriate
text for defining your tag.
Tag can also be defined at a feature level. Once you define a tag at the feature level, it
ensures that all the scenarios within that feature file inherits that tag. Depending on the
nature of the scenario, we can use more than one tag for the single feature. Whenever
Cucumber finds an appropriate call, a specific scenario will be executed.
Cucumber also provides a way to inverse the choice of tags. Consider that out of 25
defined scenarios, 10 are marked as smoke test. We are required to execute only
regression test scenarios.
For this, we can use “~” in JUnit runner class to exclude smoke test scenario. It will
look like the following.
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"},
tags = {"~@SmokeTest"})
While defining multiple tags, we can also define logical or/and logical and operation.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 28/62
12/18/2018 Cucumber Quick Guide
Since the beginning, we have been taking an example of login functionality for a social
networking site, where we just had two input parameters to be passed. Let’s think of
some more possibility. How about “New user registration” functionality? Typically, what
can be the input parameters while registering a new user for a social networking site?
Something like the following −
User Name
Email Address
Password
Re-enter password
Birthdate
Gender
Phone number
Verify that the new user registration is unsuccessful after passing the incorrect inputs.
When I enter the user name and an e-mail address as email address and password as,
and re-enter password as and Birthdate as and Gender as and phone number as then
the user registration should be unsuccessful.
It looks a bit messy at first glance. So, is there any better way to manage such chunk
of inputs? Answer can be “Data Table”. Data table is a set of input to be provided for a
single tag. This tag can be GIVEN, WHEN, or THEN.
Let’s write the above scenario with the help of data table and it will look like the
following −
| Fields | Values |
| First Name | Tom |
| Last Name | Kenny |
| Email Address | someone@someone.com |
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 29/62
12/18/2018 Cucumber Quick Guide
Example
Let’s automate an example of a data table.
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can
choose any name which is in lowercase).
Click on Finish.
Open pom.xml −
Add dependency for Selenium: This will indicate Maven, which Selenium jar files
are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber
files are to be downloaded from the central repository to the local repository.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 30/62
12/18/2018 Cucumber Quick Guide
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Add dependency for JUnit − This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Verify binaries.
Verify that the new user registration is unsuccessful after passing incorrect
inputs.
Scenario:
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 31/62
12/18/2018 Cucumber Quick Guide
| Fields | Values |
| First Name | Tom |
| Last Name | Kenny |
| Email Address | someone@someone.com |
| Re-enter Email Address | someone@someone.com |
| Password | Password1 |
| Birthdate | 01 |
Create the step definition file named as ‘dataTable.java’ inside the package
dataTable (see section scenario outline for more detailed steps).
package dataTable;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import cucumber.table.DataTable;
//Enter data
driver.findElement(By.name("firstname")).sendKeys(data.get(1).get(1));
driver.findElement(By.name("lastname")).sendKeys(data.get(2).get(1));
driver.findElement(By.name("reg_email__")).sendKeys(data.get(3).get(1));
driver.findElement(By.name("reg_email_confirmation__")).
sendKeys(data.get(4).get(1));
driver.findElement(By.name("reg_passwd__")).sendKeys(data.get(5).get(1));
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 32/62
12/18/2018 Cucumber Quick Guide
driver.findElement(By.className("_58mt")).click();
// Click submit button driver.findElement(By.name("websubmit")).click();
}
package dataTable;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 33/62
12/18/2018 Cucumber Quick Guide
We will see that home page will not displayed and “Test Pass” will be written on
the console.
Cucumber - Comments
Comment is basically a piece of code meant for documentation purpose and not for
execution. Be it a step definition file or a feature file, to make it more readable and
understandable. So, it is important to use/put comments at appropriate places in the
file. This also helps while debugging the code. Cucumber feature files can have
comments at any place. To put comments, we just need to start the statement with “#”
sign.
Different programming languages have got different norms for defining the comments.
Let’s see how Cucumber deals with it.
Step definition file − If you are using Java as a platform then mark your
comments with “//”.
Feature File − In case of feature file, we just need to put # before beginning
your comment.
Example
The highlighted text in the program refer to the comments in the code.
Feature: annotation
Cucumber - Hooks
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 34/62
12/18/2018 Cucumber Quick Guide
Cucumber hook allows us to better manage the code workflow and helps us to reduce
the code redundancy. We can say that it is an unseen step, which allows us to perform
our scenarios or tests.
To understand this notion better, let’s take an example of a feature file and a step
definition file.
The highlighted portion in the given statement actually does the job of setting up the
webdriver and ending the webdriver session. So, it is actually not relevant to the
essence of “Given statement”, and it is more like a setup for the test. Also if we think
with a broader prospective, then in case of multiple scenarios for this feature, this
webdriver setup and cleanup will run with each given statement. Logically, it makes
sense to have the setup and cleanup executed only once.
So to bring optimization, hooks can be utilized. More often we use two types of hooks:
“Before” hook and “After” hook. Method/function/piece of code, defined within Before
and After hooks, always run, even if the scenario gets passed or failed.
As the name suggests, before hook gets executed well before any other test scenarios,
and after hook gets executed after executing all the scenarios.
Step 3 − Create a step definition file named as hookTest.java under the package.
package hookTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 35/62
12/18/2018 Cucumber Quick Guide
Examples
| username | password |
| username1 | password1 |
Create the runner class as runTest.java inside the package dataTable (see the
section scenario outline for more detailed steps).
package hookTest;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 36/62
12/18/2018 Cucumber Quick Guide
Before hook − Set up the webdriver and other prerequisites to run the test.
Given statement
When statement
Then statement
Tagged Hooks
We can also indicate if we want before and after hooks to be executed with a specific
tag only. Example − @Before(‘@Web’). The same concept of tag logical and/or can be
applied with hooks as well. Example − @Before(@dev,@wip), @Before(@dev,~@wip)
Running any test framework from the Terminal has its own advantages, such as
overriding the run configurations mentioned in the code.
In order to execute Cucumber test with command prompt, use the following steps after
system configuration.
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You can
choose any name which is in lowercase).
Click on Finish.
Open pom.xml −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 37/62
12/18/2018 Cucumber Quick Guide
Add dependency for Selenium − This will indicate Maven, which Selenium jar
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Add dependency for JUnit − This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 38/62
12/18/2018 Cucumber Quick Guide
<scope>test</scope>
</dependency>
Verify binaries.
Examples
| username | password |
| username1 | password1 |
| username2 | password2 |
Note − Here, example annotation describes the range of input to be provided upon
scenario execution. Test scenario will be executed for each of the input provided. So, in
the given example, test scenario will be executed three times.
Write the following text within the file and save it.
package Outline;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 39/62
12/18/2018 Cucumber Quick Guide
import cucumber.annotation.en.Then; i
import cucumber.annotation.en.When;
Note − In code, we have to define a function having two input arguments: one
username and other will be for password. So, for each set of input provided in the
example tag, set of GIVEN, WHEN and THEN will be executed.
Write the following text within the file and save it.
package Outline;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 40/62
12/18/2018 Cucumber Quick Guide
Run the command mvn test: You will see that all the scenario, described in the
feature file have been executed (if there isn’t any error). Finally, at the bottom
you will find the following information.
Result
This describes the total test run, along with failure if any.
The previous command runs everything as mentioned in the JUnit Runner class.
However, if we want to override the configurations mentioned in the Runner, following
are the few examples to do it.
To run the specific tags only, run the command mvn test -Dcucumber.options="-
-tags @SmokeTest" on command prompt. It will run only tags, which are
marked with @SmokeTest.
JUnit shows test progress in a bar that is normally green but turns red when a
test fails.
By now, we have seen the benefits of using JUnit, but the reason we are discussing it
right now is Cucumber framework. On one hand, Cucumber is providing a way for non-
technical person to define test cases for a product, and on the other hand, our
expectation is for smooth and timely execution of such test cases.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 41/62
12/18/2018 Cucumber Quick Guide
JUnit acts as a bridge between these two. So, the flow of execution will look like the
following −
Specify the JUnit runner class to run the series of test cases.
It will execute the functions written in the step definition file according
to feature file statements.
It will build the test report in the specified format (which can be
html/JSON).
The important point here is how to configure JUnit to do all these things? Following are
the steps −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 42/62
12/18/2018 Cucumber Quick Guide
How to install −
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 43/62
12/18/2018 Cucumber Quick Guide
Why we need − Maven is a build automation tool used primarily for Java
projects. It provides a common platform to perform activities like generating
source code, compiling code, packaging code to a jar, etc. Also later if any of
the software versions gets changed, Maven provides an easy way to modify the
test project accordingly.
How to install −
Edit Path variable and include Maven as shown in the following screenshot.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 44/62
12/18/2018 Cucumber Quick Guide
Open Eclipse.
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You
can choose any name which is in lowercase).
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 45/62
12/18/2018 Cucumber Quick Guide
Click on Finish.
Open pom.xml
Add dependency for Selenium − This will indicate Maven, which Selenium jar
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber
files to be downloaded from the central repository to the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Add dependency for Cucumber-JUnit: This will indicate Maven, which Cucumber
JUnit files are to be downloaded from the central repository to the local
repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 46/62
12/18/2018 Cucumber Quick Guide
Add dependency for JUnit − This will indicate Maven, which JUnit files are to be
downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Verify binaries
After that you will be able to see a Maven repository like the following
screenshot.
Step 7 − Create Junit runner to run the test (to be covered later).
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 47/62
12/18/2018 Cucumber Quick Guide
Cucumber - Reports
We do test execution in order to understand the stability of a product, so be it manual
test or an automated test, it is very important to generate a concise report that can
depict the stability of a product. Hence, while we are automating our test scenario with
Cucumber, it is essential to know, how better we can generate our Cucumber test
reports. As we know that Cucumber is a BDD framework, it does not have a fancy
reporting mechanism.
In order to achieve this, we need to integrate Cucumber with other open source tool
like Ant/Junit. Here, we will take examples of JUnit further because, it provides support
for Java language.
Let’s look into the details of different report format, which is available and easy to use
−
Local Directory − We can specify target directory for report as any local
directory of the machine where the test will run.
Example
Let’s automate an example of a pretty format.
Write the following text within the file and save it.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 48/62
12/18/2018 Cucumber Quick Guide
Note − Here scenario first will pass, whereas the second scenario will fail. So that we
can witness how the pass and failed report looks like.
Write the following text within the file and save it.
package CucumberReport;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
}
}
}
package CucumberReport;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(
format = {"pretty", "html:target/Destination"} )
//Specifying pretty as a format option ensure that HTML report will be generated.
//When we specify html:target/Destination - It will generate the HTML report
inside the Destination folder, in the target folder of the maven project.
You will observe the following things when you run this class file.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 50/62
12/18/2018 Cucumber Quick Guide
It exactly highlights the color of failed scenario. Moreover, you will see highlight for
failed step in that scenario. This makes the debugging very easy.
JSON Report
By now we have seen how easy HTML report is. However, if we want to pass on this
report information to any other application, that’s kind of tricky in case of HTML reports.
Here comes the need of another reporting format. JSON-Java script object notation is
another format for generating Cucumber test reports. JSON is an object containing a lot
of information stored in text format. JSON reports bring a different value to the table.
JSON report can also be used as a payload of information to be transferred between
different servers. Further, it can be used to be displayed as a web page. In a nutshell,
JSON reports can be used by other application.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 51/62
12/18/2018 Cucumber Quick Guide
What is payload information? When data is sent over the Internet, each unit
transmitted includes both header information and the actual data being sent. The
header identifies the source and destination of the packet, while the actual data is
referred to as the payload. In order to generate a JSON report, we just need to make a
change in the runner file.
package CucumberReport;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options( format={"json:target/Destination/cucumber.json"})
You will observe the following things when you run this class file.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 52/62
12/18/2018 Cucumber Quick Guide
You will see the report mentioned in the following screenshot after placing line
breaks −
Cucumber - Debugging
It is very likely that your test may fail due to unknown reasons. Identifying the cause of
failure and correcting it is very important. This process is known as debugging.
Following are a few tips and tricks, which makes Cucumber debugging a bit easy.
Debugging option is available with Eclipse. Place the break point. Click the file and
debug using the following steps −
Step 1 − Place the manual break points in the Ruby step definition file.
Example
require 'ruby-debug'
Then /^the process should exit cleanly$/ do
breakpoint
assert @exited_cleanly, "Process did not exit cleanly: #{@stdout}"
end
Step 2 − We can also define a separate debug step like the following in the step
definition file.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 53/62
12/18/2018 Cucumber Quick Guide
Step 3 − Webrat is the default tool for cucumber testing with Rails. It lets you do
things like click links, type and submit forms, and so on.
There will be times where you try to click on something, for example, and it’s not there.
You’ll get a big blob of HTML thrown back, and figuring out what that exactly means can
be a real drag.
save_and_open_page
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Open Eclipse.
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 54/62
12/18/2018 Cucumber Quick Guide
Provide group Id (group Id will identify your project uniquely across all
projects).
Provide artifact Id (artifact Id is the name of the jar without version. You
can choose any name which is in lowercase).
Click on Finish.
Step 6 − Add dependency for Selenium − This will indicate Maven, which Selenium jar
files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Step 7 − Add dependency for Cucumber-Java − This will indicate Maven, which
Cucumber files are to be downloaded from the central repository to the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 55/62
12/18/2018 Cucumber Quick Guide
<scope>test</scope>
</dependency>
Step 8 − Add dependency for Cucumber-JUnit − This will indicate Maven, which
Cucumber JUnit files are to be downloaded from the central repository to the local
repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Step 9− Add dependency for JUnit − This will indicate Maven, which JUnit files are to
be downloaded from the central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Write the following text within the file and save it.
Feature: CucumberJava
Write the following text within the file and save it.
package CucumberJava;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
Write the following text within the file and save it.
package cucumberJava;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})
public class runTest {
}
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 57/62
12/18/2018 Cucumber Quick Guide
In the JUnit window, you will see a scenario with green tick mark, which
indicates success of the test execution.
It is easy to understand.
It is an object-oriented language.
Download the version best suited for your operating system (i.e. 32 or 64 bit).
During the installation, tick the option “Add Ruby…” and “Associate …”, as shown
in the following image.
Download the Devkit version best suited for your operating system (i.e. 32 or
64 bit).
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 58/62
12/18/2018 Cucumber Quick Guide
Next, install the gems you need for Cucumber web testing.
Run Cucumber
C:\Users\Admin\Documents>cucumber –init
C:\Users\Admin\Documents>cucumber
Step 5 − Install Watir − Go to command prompt and run the following command,
"gem install watir"
Step 6 − Install rspec − Go to command prompt and run the following command,
"gem install rspec"
require "watir-webdriver"
require "rspec/expectations"
require "rubygems"
require "test/unit"
require "watir-webdriver"
def teardown
@browser.close
end
def test_search
@browser.goto "google.com"
@browser.text_field(:name => "q").set "watir"
@browser.button.click
@browser.div(:id => "resultStats").wait_until_present assert
@browser.title == "watir - Google Search"
end
end
Name the file as test.rb and choose any folder for example: “e:\WithRuby”
Go to command prompt.
Go to directory e:\WithRuby
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 61/62
12/18/2018 Cucumber Quick Guide
https://www.tutorialspoint.com/cucumber/cucumber_quick_guide.htm 62/62