0% found this document useful (0 votes)
11 views

Cucumber Introduction

Uploaded by

Ashok Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Cucumber Introduction

Uploaded by

Ashok Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Cucumber Introduction

A cucumber is a tool based on Behavior Driven Development


(BDD) framework which is used to write acceptance tests for the
web application. It allows automation of functional validation in
easily readable and understandable format (like plain English) to
Business Analysts, Developers, Testers, etc.

Cucumber feature files can serve as a good document for all.


There are many other tools like JBehave which also support BDD
framework. Initially, Cucumber was implemented in Ruby and
then extended to Java framework. Both the tools support native
JUnit.

Behavior Driven Development is an extension of Test Driven


Development and it is used to test the system rather than testing
the particular piece of code. We will discuss more the BDD and
style of writing BDD tests.

Cucumber can be used along with Selenium, Watir, and Capybara


etc. Cucumber supports many other languages like Perl, PHP,
Python, Net etc. In this tutorial, we will concentrate on Cucumber
with Java as a language.

Cucumber Basics
In order to understand cucumber, we need to know all the
features of cucumber and its usage.

#1) Feature Files:


Feature files are the essential part of cucumber which is used to
write test automation steps or acceptance tests. This can be used
as the live document. The steps are the application specification.
All the feature files end with .feature extension.

Sample feature file:


Feature: Login Functionality Feature
In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working
Scenario: Login Functionality
Given user navigates to primusbank
When user logs in using Username as “USER” and Password
“PASSWORD”
Then login should be successful
Scenario: Login Functionality
Given user navigates to PRIMUS
When user logs in using Username as “USER1” and Password
“PASSWORD1”
Then error message should be thrown
#2) Feature:
This gives information about the high-level business functionality
(Refer to the previous example) and the purpose of Application
under test. Everybody should be able to understand the intent of
feature file by reading the first Feature step. This part is basically
kept brief.
#3) Scenario:
Basically, a scenario represents a particular functionality which is
under test. By seeing the scenario user should be able to
understand the intent behind the scenario and what the test is all
about. Each scenario should follow given, when and then format.
This language is called as “gherkin”.

1. Given: As mentioned above, given specifies the pre-


conditions. It is basically a known state.
2. When: This is used when some action is to be performed.
As in above example, we have seen when the user tries to
log in using username and password, it becomes an action.
3. Then: The expected outcome or result should be placed
here. For Instance: verify the login is successful, successful
page navigation.
4. Background: Whenever any step is required to perform in
each scenario then those steps need to be placed in
Background. For Instance: If a user needs to clear database
before each scenario then those steps can be put in a
background.
5. And: And is used to combine two or more same type of
action.
Example:
Feature: Login Functionality Feature
Scenario: Login Functionality
Given user navigates to primusbank
When user logs in using Username as “USER”
And password as “password”
Then login should be successful
And Home page should be displayed
Example of Background:
Background:
Given user logged in as databases administrator
And all the junk values are cleared
#4) Scenario Outline:
Scenario outlines are used when the same test has to be
performed with different data set. Let’s take the same example.
We have to test login functionality with multiple different sets of
username and password.

Feature: Login Functionality Feature


In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working

Scenario Outline: Login Functionality


Given user navigates to primus
When user logs in using Username as <username> and
Password <password>
Then login should be successful
Examples:
|username |password |
|Tom |password1 |
|Harry |password2 |
|Jerry |password3 |
Note:
1. As shown in above example column names are passed as a
parameter to When statement.
2. In place of Scenario, you have to use Scenario Outline.
3. Examples are used to pass different arguments in the
tabular format. Vertical pipes are used to separate two
different columns. An example can contain many different
columns.
#5) Tags:
Cucumber by default runs all scenarios in all the feature files. In
real time projects, there could be hundreds of feature file which
are not required to run at all times.

For instance: Feature files related to smoke test need not run all
the time. So if you mention a tag as smokeless in each feature
file which is related to smoke test and runs cucumber test with
@SmokeTest tag. Cucumber will run only those feature files
specific to given tags. Please follow the below example. You can
specify multiple tags in one feature file.
Example of use of single tags:
@SmokeTest
Feature: Login Functionality Feature
In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working

Scenario Outline: Login Functionality


Given user navigates to primus
When user logs in using Username as <username> and
Password <password>
Then login should be successful
Examples:
|username |password |
|Tom |password1 |
|Harry |password2 |
|Jerry |password3 |
Example of use of multiple tags:
As shown in below example same feature file can be used for
smoke test scenarios as well as for login test scenario. When you
intend to run your script for a smoke test then use @SmokeTest.
Similarly when you want your script to run for Login test use
@LoginTest tag.

Any number of tags can be mentioned for a feature file as well as


for scenario.
@SmokeTest @LoginTest
Feature: Login Functionality Feature
In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working

Scenario Outline: Login Functionality


Given user navigates to PRIMUS
When user logs in using Username as <username> and
Password <password>
Then login should be successful
Examples:
|username |password |
|Tom |password1 |
|Harry |password2 |
|Jerry |password3 |
Similarly, you can specify tags to run the specific scenario in a
feature file. Please check below example to run specific scenario.

Feature: Login Functionality Feature


In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working

@positiveScenario
Scenario: Login Functionality
Given user navigates to PRIMUS
When user logs in using Username as “USER” and Password
“PASSWORD”
Then login should be successful
@negaviveScenario
Scenario: Login Functionality
Given user navigates to PRIMUS
When user logs in using Username as “USER1” and Password
“PASSWORD1”
Then error message should throw
#6) JUnit Runner:
To run the specific feature file cucumber uses standard JUnit
Runner and specify tags in @Cucumber. Options. Multiple tags
can be given by using comma separate. Here you can specify the
path of the report and type of report you want to generate.

Example of Junit Runner:


import cucumber.api.junit.Cucumber;</pre>
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Cucumber.Options(format={"SimpleHtmlReport:report/
smokeTest.html"},tags={"@smokeTest"})
Public class JUnitRunner {
}
Similarly, you can give instruction to cucumber to run multiple
tags. Below example illustrates how to use multiple tags in
cucumber to run different scenarios.

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Cucumber.Options(format={"SimpleHtmlReport:report/
smokeTest.html"},tags={"@smokeTest",”@LoginTest”})
Public class JUnitRunner {
}

7) Cucumber Report:
Cucumber generates its own HTML format. However, better
reporting can be done using Jenkins or bamboo tool. Details of
reporting are covered in next topic of cucumber.

You might also like