KEMBAR78
Cucumber Notes | PDF | Software Engineering | Computing
0% found this document useful (0 votes)
61 views36 pages

Cucumber Notes

Cucumber is an open-source testing tool that supports Behavior-Driven Development (BDD) by allowing testers to write test cases in human-readable Gherkin language. It enhances collaboration between business and IT teams, facilitates early issue detection, and simplifies automated regression testing. Key concepts include feature files, scenarios, and the use of tags to manage test execution efficiently.

Uploaded by

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

Cucumber Notes

Cucumber is an open-source testing tool that supports Behavior-Driven Development (BDD) by allowing testers to write test cases in human-readable Gherkin language. It enhances collaboration between business and IT teams, facilitates early issue detection, and simplifies automated regression testing. Key concepts include feature files, scenarios, and the use of tags to manage test execution efficiently.

Uploaded by

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

0

Cucumber
Cucumber is an open-source testing tool that supports Behavior-Driven Development (BDD) approach.
"Cucumber is a software tool used by the testers to develop test cases for the testing of behavior of
the Application."
The main feature of Cucumber is that it facilitates writing tests in human-readable language,
commonly known as Gherkin.

Benefits of Using Cucumber

1- Business and IT Collaboration

● Cucumber business stakeholders aur IT teams ke beech better collaboration facilitate karta hai
kyunki Gherkin scenarios easily understandable hote hain. Business users bhi tests ko read aur
validate kar sakte hain.

2-Early Issue Detection:

● BDD approach aur Cucumber use karne se issues aur misunderstandings ko early stages mein hi
identify kiya ja sakta hai.
● Yeh software development lifecycle ko streamline karta hai.

3-Automated Regression Testing


● Cucumber automated regression testing ko simplify karta hai. Once Gherkin scenarios aur step
definitions likh liye jaate hain, yeh tests ko bar-bar execute kiya ja sakta hai to ensure ki naya
code existing functionality ko break na kare.
1

4- Documentation:

● Gherkin scenarios ek tarah ki living documentation ban jaate hain jo software requirements aur
behavior ko define karte hain.
● Yeh documentation hamesha up-to-date rehti hai kyunki yeh automated tests ke form mein hoti
hai

Basic Term of Cucumber

1- Feature File
2- Features
3- Tags
4- Scenario
5- Gherkins Language
6- Step Definition

How Cucumbers Works


● Cucumber test cases are written parallel with the code development of software. These test
cases are called step in a Gherkin Language.
● Firstly, Cucumber tool reads the step written in a Gherkin or plain English text inside the
feature file.
● Now, it searches for the exact match of each step in the step definition file. When it finds its
match, then executes the test case and provides the result as pass or fail.
2

● The code of developed software must correspond with the BDD defined test scripts. If it does
not, then code refactoring will be required. The code gets freeze only after successful execution
of defined test scripts

Difference between Cucumber and selenium

CUCUMBER SELENIUM

It is a BDD tool used to develop test cases for It is an automated testing tool.
the behavior of application functionality.

Cucumber is a BDD supported tool. Selenium is both a functional and Performance


testing tool.

Cucumber works very fast in plugins. Selenium works slower in plugin than cucumber.

1-Cucumber is used to test only web 1-Selenium is also used to test only web
applications. applications.
2- Cucumber is a free and open source BDD tool. 2- Selenium is also a free or open-source testing
tool.

Behavior-Driven Development (BDD)


3

It is a software development methodology that revolves around software development and testing
based on business logic and user requirements.
The main focus of BDD is to foster collaboration between business stakeholders and technical teams
so that software requirements can be better understood and implemented accurately.

In the following example, we are going to take the login function of a web application.
Example:-
In order to ensure the working of Login Functionality, we are developing acceptance test cases on
the basis of BDD.

Feature: Login Function


To enter in the System User must be able to Access software when login is successful.
4

● Scenario: Login
● Given User has its Email
● And Password
● When User enters the correct Email and Password
● Then It should be logged in
● Scenario: Unsuccessful Login
● When User enters either wrong Email or Password
● Then It should be reverse back on the login page with an error message

Key Concepts of BDD

1- Collaboration
● BDD approach mein business stakeholders (jaise product owners, managers) aur technical teams
(developers, testers) closely collaborate karte hain. Isse business requirements ko clearly define
aur understand kiya ja sakta hai.

2- Common Language

● BDD ek common language (jaise Gherkin) ka use karta hai jo business aur technical teams
dono ke liye understandable hota hai. Yeh common language human-readable aur plain text
mein hoti hai.

3- Scenario and Examples


5

● BDD requirements ko scenarios aur examples ke through define karta hai. Yeh scenarios user
stories aur acceptance criteria ke form mein hote hain jo software behavior ko describe karte
hain.

4- Automation
● BDD automated tests ko define aur execute karne mein madad karta hai. Yeh tests scenarios
ke form mein likhe jaate hain jo software requirements ko validate karte hain.

BDD Process

1- Discovery
● Business stakeholders aur technical teams milkar requirements ko discuss karte hain. Yeh
discussions requirements ko clear aur unambiguous banate hain.
2- Formulation
● Requirements ko Gherkin syntax mein scenarios aur examples ke form mein likha jaata hai.
Yeh scenarios Given-When-Then format mein hote hain.
3- Automation
● Scenarios ko step definitions ke through automate kiya jaata hai. Step definitions mein actual
code hota hai jo Gherkin steps ko implement karta ha.

public class LoginSteps {


@Given("^the user is on the login page$")
public void userOnLoginPage() {
// Code to navigate to the login page
}
@When("^the user enters valid username and password$")
6

public void userEntersValidCredentials() {


// Code to enter username and password
}
@Then("^the user should be redirected to the dashboard$")
public void userRedirectedToDashboard() {
// Code to verify user is on the dashboard
}
}

4- Execution
● Automated tests ko run kiya jaata hai taaki software requirements ko validate kiya ja
sake. Yeh tests ensure karte hain ki software expected behavior follow kar raha hai

Feature File in Cucumber Testing

● "Feature" is a core concept in Cucumber. This is a plain text file that describes the
behavior of the software. Feature files are written in Gherkin syntax and are business
readable.

● The feature file is an entry point, to write the cucumber tests and used as a live
document at the time of testing.
● The extension of the feature file is “.feature”

Example:-
7

In order to ensure the working of Login Functionality, we are implementing the cucumber test by
creating a feature file. It will verify whether the Login Functionality is working properly or not.

Feature file with Multiple Scenario

Feature files can contain multiple scenarios or scenario outlines. We can write all possible Scenarios
of a particular feature in a feature file.
By using the keyword "Scenario" or "Scenario Outline", One Scenario can be separated from another.

Feature File 1:
8

Note:- If we do not need to execute a particular scenario at a time, then we can comment on that
scenario.
9

How to create Feature File


There are many approaches to create the feature file.
Following are steps to create a feature file in Eclipse.

1- Goto the file option and select New, click on the New and select the other option.

2- After selecting Other option, you will get several options, from these options, select
General < File option,
and then click Next.

3- After clicking the Next, select the project inside which you want to create a feature file.
After selecting the project, you can create a feature file by giving a name and ".feature"
extension.
You can provide any name to the feature file on your own choice. After providing a name,
click on the
Finish button.

Tags in Cucumber Testing

In Cucumber, tags are used to associate a test like smoke, regression etc. with a particular scenario.
10

If we have many scenarios in the feature file, to keep them in one group, we use tags in Cucumber,
through which we will be able to prepare reports for specific scenarios under the same tag.

By default, Cucumber executes all the scenarios inside the feature file, but if we need to execute or
skip any specific scenario under a specific test, we can declare scenarios within a tag.

Example:-
@TestName
Scenario: Mention the Scenario

Where @: It is a symbol used to declare a tag.

Example:-
Suppose, a feature file of an application contains 100 test scenarios, and when we test this
application through Cucumber testing each time 100 test scenarios will get executed unnecessarily.
And due to that, system performance is getting low.

To overcome this problem, we can use a tag. Let's take an instance of a feature file with few
scenarios.
11

● First, the @SmokeTest or @RegressionTest tag contains only those scenarios that are
applicable to the smoke or regression testing.
● Second, scenarios can be included or excluded as per the requirement at the time of execution.

Now suppose, we need to test only those scenarios which are declared under the smoke test, then we
can mention @SmokeTest tag inside the testing code in the following way:

Tags={“@SmokeTest”}

After mentioning the tag inside the testing code, only the scenarios which are declared under the
smoke test will be tested and remaining will be skipped.
12

What’s the Need a Tag for Cucumber Testing

● It looks easy when we just have a few numbers of scenarios in a feature file. However, in
real-time projects, it does not happen.

● In real-time projects, there may be a large number of feature files, which may have a different
purpose such as Smoke test/Regression test, different status such as Ready for
execution/Work in progress, different perspective such as Developer/QA/BA, etc.

● In order to manage the execution of such large feature files, we use the tag with scenarios
inside the feature file.

● The benefit of the tag is that we can only test the specific scenario of a feature file, which we
need to execute, and not those scenarios which are unnecessary.

How to create a set of multiple tags in cucumber testing?


We can also use multiple tags when we need to perform more than one testing of a single scenario.

Example:- As per the above example, we can test the first scenario for both smoke testing and
regression testing.
13

The testing through multiple tags can be done by using two operators:

● OR operator
● AND operator

OR Operator

The OR operator can be used in the case, when we need to test an application like this, if the
application has failed in the first test, then the next test should be checked.
If the next test is also failed, then another next test should be checked and so on.
Example:-
Tags={“@SmokeTest, @RegressionTest”}
14

-> OR operator executes all the tagged tests i.e., @SmokeTest, @RegressionTest.

AND Operators

The AND operator can be used in the case, when we need to test an application like this, if the
application has passed in the first test, then jump to check the next test, but if it fails, then testing
should be terminated.

Example:-

tags={“@SmokeTest”, @RegressionTest }

In the above example, AND operator executes the first test, if the result of this test is passed then it
will jump to check the next test. But if the result is failed then testing will be terminated.

How to ignore tags in Cucumber Testing?


If we need to skip the test, then we use a special character “~” within the tag. This special character
also works to skip both scenario and feature.

Example:-
Suppose there is a group of @SmokeTest and @RegressionTest test. Now, we need to skip the
regression test, to do that, consider the following code:-
tags={“@SmokeTest”, “~@RegressionTest”}
15

Gherkins Language

In the cucumber testing, feature files are created with the executable test scripts. The language in
which these executable test scripts are written, is known as Gherkins language.

Gherkin offers the following specific keywords to write the common test scripts in the feature file:

1. Feature
2. Scenario
3. Given
4. When
5. Then
6. But
7. And
8. Background

1- Feature
Each file of cucumber testing starts with a feature keyword. It is a standalone unit or functionality
to be tested.
For Example, Login feature, payment transfer feature, registration feature, etc.
Example:-
16

2- Scenario
Each feature contains the required number of tests to test the feature. Each test is named as a
Scenario.
For example, feature login functionality can contain two scenarios, first for a successful login and
second for unsuccessful login.
17

3- Given
This keyword refers to the precondition of the test. For example, to access any web application, the
first requirement or precondition is to navigate its home page, because, from the home page, we
can navigate to other links such as signup, login, etc.
Example:-
18

4- When
It usually refers to the actions of a user that is to be executed.
Example:-

When the user fills "registration email text box" with "nikita.sharma27@gmail.com".

5-Then

This keyword refers to the outcome of the previous step or upcoming action.
19

6-But
This keyword is used to add the negative condition.

7- And
This keyword is used to add more conditions into your steps.
Example
20

8- Background
This keyword is used to define the steps that are common to all tests in the feature file. For
example, Navigation to Home Page, Click on the Login, Enter User Name and Password, Click
on Submit button are the common steps in almost all web applications.

What is a Feature in cucumber testing?


The feature is a parameter which is used to test the requirements of the customer from the
software product.

● Create and delete the user from the social networking site.
● User login functionality to access the social networking site.
● Sharing videos or photos on social networking sites.
● Sending a friend request.
● Logout or sign out.

At the time of testing, it is best practice that we should determine the features first, before
deriving the test scripts to be tested.

It is clear that, when we talk about cucumber, each independent functionality of the product
or web application can be called as a feature.
21

A feature typically has a list of scenarios to be tested for that feature, and the feature and its
description are stored in the feature.file.

There can be numerous features of a software product. Hence for the better management of
features, we should create a separate feature file for each feature.

The keyword "Feature" represents a feature under the test in Gherkin language.
Example:

Suppose, the feature login functionality of facebook site is under the test. Hence, we need to
test it as per the following aspects:
22

● If both credentials, i.e., username and the password are correct, then the user should log
in into facebook.
● If the username is incorrect, but the password is correct, then the user should be shown
an error message.
● If the user name is correct, but the password is incorrect, then the user should be shown
an error message.
● After the successful login, the user should be navigated to My Account or Profile page.

Now, we are going to create a feature file for the login feature of the social networking site:
23

According to the above example, we can create feature files as per the particular feature.
A feature file is always based on the behavior of an application under specific circumstances.

What is Scenario in Cucumber Testing?


The scenario is one of the core structures of the Gherkin language. Scenario includes all the
possible circumstances of the feature and test scripts for these circumstances.
One feature can have multiple scenarios, and each scenario consists of one or more steps.
24

Example:
In order to ensure Feedback functionality of a web application, we are creating a cucumber
test to verify whether it is working or not.

● Given: It describes the task of a particular function of an application. It is basically a


pre-known state or pre-condition for testing.
● When: It is used to describe the action. For example, when the user tries to login, it is
considered as an action.
● Then: The expected result should be placed here. For example, the verification of a
successful login is a result.
● And: We can use it to combine two or more actions of the same type. For example,
username and password belong to the same type, i.e., login function.
25

Scenario Outline

In Gherkin language, scenario outline is the keyword which is used to run the same scenario
multiple times.

It is also defined as "Scenario outlines are used when the same test is performed multiple
times with a different combination of values."

Example:-

Suppose we need to test whether the login functionality for multiple users is working properly
or not. This case requires the execution of the login functionality scenario multiple times.

Hence, the scenario outline can be used for the testing of login functionality scenarios with
multiple usernames and passwords.

While mentioning variables such as "Username" and "Password" in gherkin statements,


mention them with "<>". Consider the following test script:
26

Let's take the above test script, in which we need to test login functionality with several
different sets of username and password.
27

In the above example, we have provided multiple input values for the variables "Username"
and "Password". While executing the actual test, Cucumber will replace the variables with the
provided input values.
Install Cucumber Eclipse Plugin- See all steps from java points.

What is the Data Table in Cucumber?

Data tables are used when we need to test numerous input parameters of a web application.
For example, the registration form of the new user involves several parameters to test, so for
this, we can use the data table.

Example to understand the Data table:-


The registration form contains the following parameters:
● User Name
● Email
● Password
● Confirm Password
● Birth-date
● Gender
● Phone Number
28

Feature file to test the feature "User Registration."

In the above feature file, we can see that it looks a bit complex at first glance due to using
"And" multiple times.
So, to reduce such complexity, we can use "Data Table."
Data table involves a set of input parameters, and these parameters are provided to a single
tag such as GIVEN, WHEN, or THEN.
Let's create the above feature file with the data table, and see how it will look:
29

In the above feature file, we can see that all parameters of the registration form have been
organized in a simple and recognizable manner.

How to create a Selenium Maven Project in Eclipse?

Note:- See all the steps from java points:-


30

What is Hook in Cucumber?

A "hook" in Cucumber is a special mechanism that lets you run code at specific points of test
execution.
You can use Hooks to perform certain setup and teardown operations before or after the
execution of your test scenarios. These help in making your tests maintainable and organized.
Example:-

@Before setup () {

//logic

@Scenario

Given

When

And

Then

@After cleanup (){

//Logic}
31

As per the code logic, hook's job is to start and close the web driver session after a specific
function/method. Hence, in actuality, it is not relevant to any function/method or scenario.
The Need of Hook

At the time of testing, we may encounter circumstances where we need to perform some
conventional prerequisite steps before the testing of the test scenario.

Consider the following prerequisite to understand the kind of prerequisites which may
encounter at the time of testing:

● To Start a web driver


● Set up of Data Base connections
● Set up of test data
● Set up of browser cookies
● Navigation to a certain page

Similarly, there are always some prerequisite steps which may encounter after testing:

● To stop the web driver


● To Close DB connections
● To Clear the test data
32

● To Clear browser cookies


● To Log out from the application
● Printing reports or logs
● Taking the screenshots of error

In order to handle these types of conventional prerequisite steps, using cucumber hook is the
best option.
Hook Annotations

Unlike TestNG Annotations, the cucumber supports only two hooks:

● @Before
● @After

@Before

As the name suggests, we can use the @Before hook with the function/method after which we
need to start web driver.

@After

As the name suggests, we can use the @After hook with the function/method after which we
need to close the web driver.
33

Here is an instance of a step definition file of a Maven testing project. This project is created
for the testing of web application javaTpoint.

In order to use the hook, we created the step definition file named hookTest.java under the
package javatpointTest.

public class hookDemo {


WebDriver driver = null;

@Before public void setUp(){


driver = new FirefoxDriver();
}

@Given("^User navigates to javatpoint$")


public void goToFacebook() {
driver.navigate().to("https://www.javatpoint.com/");
}

@When("^ user enter Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$" ")


public void User_enter_Username_and_Password(String arg1, String arg2) {
driver.findElement(By.id("emailAddress")).sendKeys(arg1);
driver.findElement(By.id("password")).sendKeys(arg2);
driver.findElement(By.id("u_0_v")).click();
}

@Then("^login should be unsuccessful$")


34

public void validateRelogin() {


if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.javatpoint.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test Pass");
} else {
System.out.println("Test Failed");
}
driver.close();
}

@After public void cleanUp(){


driver.close();
}
}

● At the beginning, @Before annotation will set up the web driver and other required
prerequisites to execute the test.
● After setting up web driver and other prerequisites, the Given statement will be executed.
● After the execution of the Given statement, the When statement will be executed.
● After the execution of the When statement, the Then statement will be executed.
● Now at the last, @After hook will close the web driver and do the cleanup process.
35

Since we know that, to execute step definition file, we should have a complete Maven testing
project so first create it in eclipse.

Tagged Hooks

The hook can also be used with a tag. We can use @before and @after hooks with a specific
test.

1. @Before ('@RegressionTest)
2. @After ('@RegressionTest)

We can also use the same concept of the hook with logical and/or operators.

Example:

1. @Before ('@RegressionTest, @SmokeTest)


2. @ After ('@RegressionTest, @SmokeTest)

You might also like