Introduction to Cucumber

Published On: 13 September 2021.By .
  • Quality Engineering

What is Cucumber?

Cucumber is a testing tool that supports Behavior Driven Development (BDD) Framework.
Test are written in simple English language and use different English language tag name their are caller Gherkin language
Gherkin tags Given-When-Then.
Cucumber is a tool to automate acceptance tests created in BDD format.
Cucumber improve communication between non-technical and technical member of same project.

Behavior Driven Development (BDD)

BDD is an extension of TDD(Test Driven Development). Testcase are explained as behavior of software/application and more end-user-focused.

Collaboration between QA Team to business stakeholders, Developers, Use simple English language understand by non-technical stakeholder. BDD frameworks work as “Bridge” between business & Technical Language.

Setup Cucumber with Selenium in Eclipse

Install Java.
Start Eclipse.
In Eclipse install cucumber
Download cucumber jar for Eclipse
Configure cucumber in Eclipse

What is Gherkin?

Gherkin is simple English text language, which used in cucumber to interpret and execute the test script.
Gherkin provides common set of keywords in English text.

Following are Gherkin Keywords

Feature :- Is functionality of project, features usually contains a list of Scenarios.
Description (Optional) :- Description of feature.
Scenario :- Test Scenario.
Given :- Prerequisite/ Pr-step before the test steps get execute.
When :- Specific condition
Then :- What should happen if condition mention in When is satisfied.
And :- Use for conjunction between two conditions.
Background :- List of steps for run before each of the scenarios.
But:- Use for conjunction between two conditions. OR condition between any two statements.
Scenario outline :- List of step for data-driven as an example and <Placeholder>

Cucumber Mainly have 3 file/class there are

1. Feature files.
2. Step Definition.
3. Runner

Feature files

In Feature file cucumber test are written. Each functionality have different feature file. The Extension of feature files “.feature” example of feature file name “UserLogin.feature”. Entry point of cucumber tests.

Steps to create Feature file :-

Step 1 − Create a Maven project named as Test.
Step 2− Create a package named features under src/test/java
Step 3− Create a feature file named cucumberTest.feature.

Create two Scenario in features file Example
1. Successfully login in Facebook and
2. Successfully logout.

Below are example of features file.

Step definition

Step definition is a java class store and mapping feature file step/scenario with a code of function to be execute. If run cucumber step and scenario mention in feature file, it scan in step definition file and figure out which method to be called.

Step to Create Step definition file

Step 1 − Select and right-click on the package Stepdefinitions.
Step 2 − Click on ‘New’ file.
Step 3 −Give then feature file first.
Step 4 −file a name such as login.java
Step 5 −Then Right click on feature file>>Run as>>Cucumber feature.
Step 6 −Eclipse console window says “You can implement missing steps with the snippets below:” copy the complete text marked in a Red box and paste it into the above created login.class

See below Screenshot of Step definition class

Test Runner Class

Create a new Class file in the ‘testrunner‘ package and name it as ‘CucuberRunner‘, by right click on the Package and create new>>class. Runner class need tag/annotations to understand cucumber features class should be run through it and feature files to be picked up steps package location.

First statement “import org.junit.runner.RunWith;”
@Runwith annotation is used for junit test should run using cumcuber.class existing in ‘Cucumber.api.junit‘ package.

Second statement “import io.cucumber.junit.CucumberOptions;”
import @CucumberOptions annotation. This annotation tells cucumber have many file and element example feature file, Step definition, Reporting system, Property files, Setting for test & enable all things if we user cucumber command line.

Following table are present Cucumber Option

Cucumber Option

Use/Purpose

Default Value

Features

set : The path of feature class

{}

glue

set : The path of step definition class

{}

tags

Should be executed tags used in feature file

{}

dryRun

true: Check all the step/tests have step definition class

false

monochrome

true: Display more readable output in console

false

strict

True : fail execution if any step/test are undefined

false

format

Set : use for report format

false

Now give it a run by Right Click on TestRunner class and Click Run As> Junit.

Related content

That’s all for this blog