Introduction to Cucumber Cucumber is a testing approach/tool that supports Behaviour Driven Development (BDD). It provides a way to write tests that anybody can understand, regardless of their extent of technical knowledge. It explains the behavior of the application in a simple English text using Gherkin language. I hope that you got a glimpse about what is Cucumber. Now, let’s move further and understand some of the facts that depict the need for Cucumber in a testing framework.

Why Cucumber?

Well, Cucumber is one of the most popular tools because of the reasons listed below:

  1. Cucumber is open source and hence, its free to use.
  2. On using Cucumber, you can write your test scripts in multiple languages such as Java, Ruby, .NET, Python, etc.
  3. It also integrates with Selenium, Ruby on Rails, Watir and other web-based testing tools.
  4. Cucumber is one of the most widely used BDD tools. 

These are some of the unique features that make Cucumber helpful for testing a website. Now that you know what is Cucumber and why do you need it, let’s understand one of the unique features of Cucumber that makes it easy for a non-techie to understand the test cases. 

Behavior Driven Development(BDD)

In very simple terms, BDD or Behavior-Driven Development is a technique where your specifications or test cases are written in plain English like sentences. With this approach, the non-technical team members find it easy to understand the flow and collaborate more in the process of software development. 

Introduction to Selenium

Selenium -Selenium Maven with Eclipse

Selenium is the preferred tool when it comes to automating the tests which are carried out on web browsers. It is useful only for testing of web applications. Any desktop (software) application or mobile application cannot be tested using Selenium. It is very helpful in writing functional test cases. It also provides reliable performance with ‘n’ number of test cases and it is obviously the best suitable automation tool for web applications.

Now that you know what is Selenium, let’s move further in this article on Cucumber Selenium Tutorial and understand why use Cucumber with Selenium?

Why use Cucumber with Selenium?

Many organizations use Selenium for functional and regression testing. Selenium and Cucumber are a great combination when it comes to web application automation, as Cucumber allows you to write your tests quickly in English-like language and Selenium allows you to run on various combinations of browsers.

Cucumber tool is based on the Behavior Driven Development framework that acts as the bridge between the Software Engineer and Business Analyst and also between Manual Tester and Developers.

When using Selenium with Cucumber for test automation, tests are written in feature files that can be understood by various stakeholders in an agile environment such as Business Analysts. Cucumber also comes with its ability to support multiple scripts and programming languages and JUnit is used to execute these scripts and generate the output.

Having understood this, let’s now see the various steps to create Cucumber application and run the test cases.

Steps to create Cucumber Application

The various steps involved in creating a Cucumber application are as follows:

  1. Download the Jar files of Cucumber and Selenium and configure the build path.
  2. Add Cucumber Plugin from Eclipse Marketplace.
  3. Create a feature file and add scenarios.
  4. Implement the steps for the scenarios.
  5. Write the runner class and execute the code.

Now, let’s understand each of these steps in detail.

Step1: Download Cucumber and Selenium Jar files

Cucumber doesn’t actually interact with the browser and performs actions on the website under test. Interacting with the website is the action that is performed by Selenium WebDriver. Due to this reason, you will need to download Cucumber as well as Selenium Webdriver jar files. If you wish to know how to install Selenium on your system, you can check this article on Selenium Installation. Below is the list of jar files for Cucumber.

  • Cucumber-core
  • Cucumber-html
  • cobertura code coverage
  • Cucumber-java
  • Cucumber-junit
  • Cucumber-jvm-deps
  • Cucumber-reporting
  • Hamcrest-core
  • Gherkin
  • Junit

Once you download these jar files from Maven Repository, you can configure the build path of your project and add all the .jar files and libraries as shown in the below snapshot.

Configure Build Path - Cucumber Selenium tutorial

Step 2: The next step is to install Cucumber on your Eclipse. For that, you need to go to Help -> Eclipse Marketplace -> Search Cucumber and install Cucumber and Naturals on your eclipse. Below snapshot depicts the same.

Eclipse Marketplace - Cucumber Selenium Tutorial

Step 3: Once you configure Cucumber, the next step is to create a feature file. First, you have to create a new source folder and then add a feature file as shown below.

Creating feature file - Cucumber Selenium Tutorial - Edureka

Next, you need to specify the name of the feature file as shown below.

Feature file - Cucumber Selenium Tutorial - Edureka

Once you create the feature file, you can write the scenarios as shown in the below code.

Test Scenario I:

Feature: Reset functionality on login page of Application
Scenario: Verification of Reset button
Given Open the Firefox and launch the application
When Enter the Username and Password
Then Reset the credential

 Step 4: After that, you need to implement the scenarios with the help of step definitions. In the below code, I have implemented the methods for the scenario that I have written in the feature file.

package stepDefinitions;
public class Steps {
@Given("^Open the Firefox and launch the application$")
public void open_the_Firefox_and_launch_the_application() throws Throwable
System.out.println("This Step open the Firefox and launch the application.");
@When("^Enter the Username and Password$")
public void enter_the_Username_and_Password() throws Throwable
System.out.println("This step enter the Username and Password on the login page.");
@Then("^Reset the credential$")
public void Reset_the_credential() throws Throwable
System.out.println("This step click on the Reset button.");

Step 5: Now, the last step is to create the ‘runner class’ and execute the program. Basically, your project structure should look like as depicted in the below snapshot.

Project structure - Cucumber Selenium Tutorial - Edureka

In the above snapshot, ‘’ is an executable file. Let’s now code this file and understand how it works.

package Runner;
import org.junit.runner.RunWith; 
import cucumber.api.CucumberOptions; 
import cucumber.api.junit.Cucumber;
public class Runner 

In the above code, I have ‘@CucumberOptions’ and specify the package name of both feature file and Runner file. So that it will implement the methods and run the code. When you run the program, it will display the methods from the step definition. Your output should look like as shown below:

This Step open the Firefox and launch the application.
This step enter the Username and Password on the login page.
This step click on the Reset button.
1 Scenarios (32m1 passed)
3 Steps (32m3 passed)

So, this is how you need to write all the files and execute the program.

Test Scenario II:

Now, let’s understand one more scenario wherein I have integrated Selenium with Cucumber. Consider a scenario where you want to test the Twitter website. One of the test scenarios will verify the login credentials. With BDD, you can write this test scenario in this format:

Feature: Test Twitter smoke scenario
Scenario: Test login with valid credentials
Given Open the firefox and start application
When I enter valid username and valid password
Then user should be able to login successfully

Next, let’s write the Step definition class to implement all the scenarios.

package stepD;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SmokeTest {
WebDriver driver;
@Given("^Open the firefox and start application$")
public void open_the_firefox_and_start_application() throws Throwable {
System.setProperty("webdriver.gecko.driver", "C:geckodriver-v0.23.0-win64geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
@When("^I enter valid username and valid password$")
public void I_enter_valid_username_and_valid_password() throws Throwable {
driver.findElement(By.xpath("//input[@placeholder='Phone, email or username']")).sendKeys("your_username");
driver.findElement(By.xpath("//div[@class='clearfix field']//input[@placeholder='Password']")).sendKeys("your_password");
@Then("^user should be able to login successfully$")
public void user_should_be_able_to_login_successfully() throws Throwable {
driver.findElement(By.xpath("//button[@class='submit EdgeButton EdgeButton--primary EdgeButtom--medium']")).click();

In the above program, I have used GeckoDriver to launch firefox browser and using driver.get() it will navigate through the Twitter log in page. Further, it will locate the username and password text box using XPath locator and enter the valid credentials. Finally, it will log in successfully and run the test cases. You have to execute the runner as a JUnit test to know whether the tests are passed or failed.

Leave a Reply

Privacy Settings
We use cookies to enhance your experience while using our website. If you are using our Services via a browser you can restrict, block or remove cookies through your web browser settings. We also use content and scripts from third parties that may use tracking technologies. You can selectively provide your consent below to allow such third party embeds. For complete information about the cookies we use, data we collect and how we process them, please check our Privacy Policy
Consent to display content from Youtube
Consent to display content from Vimeo
Google Maps
Consent to display content from Google
Consent to display content from Spotify
Sound Cloud
Consent to display content from Sound