Cucumber with Selenium Tutorial for Test Automation

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated May 9, 2025

In the last Selenium tutorial, we introduced you to Selenium Grid, which is a distributed test execution environment to speed up the execution of a test pass.

Now at the end of this comprehensive Selenium training series, we are learning advanced Selenium testing and related concepts.

In this and the next tutorial, we will introduce you to the Cucumber – a Behavior Driven Development (BDD) framework that is used with Selenium for performing acceptance testing.

Cucumber Introduction

Automation Testing Using Cucumber Tool and Selenium

Cucumber is a tool based on the Behavior Driven Development (BDD) framework which is used to write acceptance tests for the web application. It allows automation of functional validation in an 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 the BDD framework. Initially, Cucumber was implemented in Ruby and then extended to the Java framework. Both 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 about the BDD and the style of writing BDD tests.

Cucumbers can be used along with Selenium, Watir, 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

To understand cucumber, we need to know all the features of cucumber and its usage.

#1) Feature Files

Feature files are an essential part of Cucumber, which is used to write test automation steps or acceptance tests. Users can use this as a live document. The steps are the application specification. All the feature files end with the feature extension.

Sample feature file:

Feature: Login Functionality Feature

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

Scenario: Login Functionality

Given user navigates to SOFTWARETETINGHELP.COM
When a user logs in using Username “USER” and Password “PASSWORD”
Then login should be successful

Scenario: Login Functionality

Given user navigates to SOFTWARETETINGHELP.COM
When a user logs in using Username “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 the Application under test. Everybody should be able to understand the intent of the feature file by reading the first Feature step. We typically keep this part concise.

#3) Scenario

A scenario represents a particular functionality that 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 the given, when, and then format. This language is called “gherkin”.

  1. Given: As mentioned above, the given specifies the pre-conditions. It is a known state.
  2. When: This is used when some action is to be performed. As in the above example, we have seen when the user tries to log in using a 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, and 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 the database before each scenario, then those steps can be put in the 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 SOFTWARETETINGHELP.COM
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 sets. Let’s take the same example. We have to test login functionality with multiple different sets of username and password.

Feature: Login Functionality Feature

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 SOFTWARETESTINGHELP.COM
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 the 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 that is related to the 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

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 SOFTWARETESTINGHELP.COM
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 scenarios. 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 and for scenario.

@SmokeTest @LoginTest

Feature: Login Functionality Feature

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 SOFTWARETETINGHELP.COM
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 the below example to run a specific scenario.

Feature: Login Functionality Feature

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 SOFTWARETETINGHELP.COM
When user logs in using Username as “USER” and Password “PASSWORD”
Then login should be successful

@negaviveScenario
Scenario: Login Functionality

Given user navigates to SOFTWARETETINGHELP.COM
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 the 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.

Cucumber Project Setup

Detail explanation of Cucumber project set up is available separately in the next tutorial. Please refer to Cucumber Tutorial Part2 from more information about the project setup. Remember, there are no extra software installations required for Cucumber.

Implementation of Feature file:

We have to implement these steps in Java to test the feature files. Need to create a class that contains those given, when, and then statements. Cucumber utilizes its annotations to embed all the steps (given, when, then). Each phrase starts with “^” so that Cucumber understands the start of the step. Similarly, each step ends with “$”.

The user can use regular expressions to pass different test data. Regular expressions take data from feature steps and pass to step definitions. The order of parameters depends on how they are passed from the feature file.

Please refer next tutorial for project setup and mapping between feature files and Java classes.

Example:

Below example is to illustrate how feature files can be implemented.

In this example, we have used no selenium API. This is to just show how cucumber works as a standalone framework. Please follow the next tutorial for selenium integration with cucumber.

public class LoginTest {
@Given("^user navigates to SOFTWARETETINGHELP.COM$")
public void navigatePage() {
system.out.println(“Cucumber executed Given statement”);
}
@When("^user logs in using Username as \"(.*)\" and Password \"(.*)\"$")
public void login(String usename,String password) {
system.out.println(“Username is:”+ usename);
system.out.println(“Password is:”+ password);
}
@When("^click the Submit button$")
public void clickTheSubmitButton() {
system.out.println(“Executing When statement”)
}
@Then("^Home page should be displayed$")
public void validatePage() {
system.out.println(“Executing Then statement”)
}
@Then("^login should be successful$")
public void validateLoginSuccess() {
system.out.println(“Executing 2<sup>nd</sup> Then statement”)
}
}

When you execute the Cucumber runner class, cucumber will start reading feature file steps. For example, when you execute @smokeTest, cucumber will read Feature step and Given a statement of scenario.

As soon as Cucumber finds Given the statement, the same Given statement will be searched for your java files. If the same step is found in Java file, then cucumber executes the function specified for the same step, otherwise cucumber will skip the step.

Conclusion

In this tutorial, we have covered features of the Cucumber tool and its usage in real-time scenarios.
Cucumber is the most favorite tool for many projects as it is easy to understand, readable, and contains business functionality.

In the next chapter, we will cover how to set up a Cucumber-Java project and how to integrate Selenium WebDriver with Cucumber.

Was this helpful?

Thanks for your feedback!

Recommended Reading

  • Working with Excel Objects in the VBScript

    Introduction to VBScript Excel Objects: Tutorial #11 In my previous tutorial, I explained ‘Events’ in the VBScript. In this tutorial, I will be discussing Excel Objects that are used in the VBScript. Please note that this is the 11th tutorial in our 'Learn VBScripting' series. VBScript supports different types of…

  • Cucumber Java Selenium WebDriver Integration

    Cucumber Selenium WebDriver Java Integration with Example: In the last tutorial, we discussed the Cucumber tool, its usage, and different features. Moving ahead in our free Selenium online training series, we will discuss how to set up a cucumber project and will discuss the integration of Selenium WebDriver with Cucumber.…

  • selenium automation with phantomjs

    In This Article, Selenium Automation With PhantomJS is Explained With Code Examples: PhantomJS is a headless browser that is primarily used for GUI less automation. The performance and execution happening on this browser is faster and is generally used in the scenarios where manual monitoring is not required and on…

  • Introduction To Selenium WebDriver

    Introduction to Selenium WebDriver: Earlier in this series, we published tutorials that focused more on Selenium IDE and its various aspects. We introduced the tool and discussed its features. We also constructed a few scripts using Selenium IDE and Firebug. From there, we moved on to different types of web…


READ MORE FROM THIS SERIES:



57 thoughts on “Cucumber with Selenium Tutorial for Test Automation”

  1. Find another job if you have to use cucumber, it’s the worst thing you can have when it comes to writing tests as a developer.

    No code completion, no way of reusing most of the code (you reuse only methods which are already part of any decent framework like click/type), you have to search for tests with regex, no concept of page objects, no images (an image says more than 1000 words). God save you when you have to combine it with screenshots and visual testing.

    Also if the project has a monorepo it will be hellish to update any feature that has tests in multiple projects. Even as bad as it would take you a day to change something and a week to make the tests pass and get the approvals.

    Reply
  2. What are the maximum number of scenarios and scenario steps that we can use in a single feature file.

    Anyone knows answer please mail me Thanks…………..

    Reply
  3. really helpful. Thank you. Looking more like this..I am completely a beginner to automation. No coding knowledge and only came here with the willing to learn. Will move ahead with the same interest believe and perseverance in my work 🙂

    Reply
  4. Hi ,

    I am looking for certification in cucumber or something similar to that. Could you please suggest what should I go for.

    Thanks
    Pankaj

    Reply
  5. Hope sometimes someone comes with TestNG with Cucumber so we can make parallel execution possible. Has anyone done parallel execution with Cucumber before?

    Reply
  6. Thank you team.

    I have gone through all the topics like maven, cucumber BDD, Java and Selenium Webdriver and
    All the study material/ note are very useful in interview process.
    Could you please create some more blogs on advanced level interview questions with examples
    And one more point I would like to add like provide java programs with detailed information or explanation so that it will easy us to understand those who are new to java.

    Thanks and Best Regards
    Bhagyashree Zaware

    Reply
  7. how Hooks and html report generation can be integrated with this, can we get any single project which used
    Base class, step Definition, content management page, page object, Hooks, Runner class, Report in HTML, generic utilities.

    Reply
  8. Hi,

    I want to call a method, when each test scenario is failed.Which method I can call in Cucumber.?
    Thanks in advance.

    Regards,
    Poornima

    Reply
  9. Hi All,
    Can we send report through the email when the test case executed in Cucumber bdd framework.
    I have added function in After hook its sending report but report is empty.
    Can anyone please guide me here?

    Reply
  10. Hi friends
    Cucumber cannot be used with TestNg
    because TestNg has Inbuilt Annotations
    as @Test,@BeforeTest,@AfterTest——–
    TestNg is Very Good at Understanding with simple Code with Selenium and platform java
    but in cucumber we have to write Each Step

    Reply
  11. Anyone knows answer please mail me

    Reply
  12. Thanks! Such an incredible blog and blog that offers such knowledge. Your Post Provides Numerous Answers to Questions Regarding How Cucumber Automation Services Can Benefit Businesses in the Modern Era

    Reply
  13. When i am trying to run cucumber class some scenarios is working after that driver object is showing null why it ts?

    Reply
  14. what can i do if duplicate step def file error occurs at the time of cucumber selenium feature file run.
    plz anyone know this then let me know.
    Thanks in advanced.

    Reply
  15. scenario outline and maintaining data in the feature file makes the feature file clumsy and difficult to edit and update. is there a something like DataProvider in testng that is available in cucumber that can be used to load data from csv, xls.

    Reply
  16. This was not very helpful. It talks in such vague generalities, you’d pretty much have to already know Cucumber to learn it from this page.

    Reply
  17. Very useful information for a beginner.
    @William Deich : Sir, Please guide us from where we can get more basic info we don’t know cucumber at all ?

    Reply
  18. What are the maximum number of scenarios and scenario steps that we can use in a single feature file

    Any number. In our project we are following so

    Reply
  19. Hello i need to know how can i use the data from database like ms sql as data table in example of scenario outline for my selenium scripts ?

    Reply
  20. This website is very useful. i learnt many new things.
    I appreciate the content and precise information.
    Thanks,
    Mahesh

    Reply
  21. Hi Team,

    First of all let me tell you one thing is you all doing a fantastic job by giving us knowledge and making our future very best.

    I would like to ask how can i download cucumber ?

    Please provide me link or files ?

    email id – vshah07@gmail.com

    Reply

Leave a Comment