top of page

My First Cucumber Test

Cucumber is a testing tool that supports Behavior Driven Development (BDD). The main advantage is, it is written in plain simple English which clients and business users with limited technical knowledge can easily understand.

Let us see, how to create a simple cucumber test in java where we check if google search is working as expected.

Before we begin, the following should be installed in your system.

  1. Java

  2. Eclipse IDE

  3. Maven

After installing, lets proceed with the following steps to create our first test.

  • In eclipse, navigate to Help --> Marketplace. In the search, type in "Cucumber" and hit "Go"

  • Choose the Cucumber Eclipse Plugin and click "Install". Agree to the terms and conditions.

  • You may get security warning. Click on "Install Anyway"

  • You will be prompted to restart eclipse after installation. Restart eclipse.

  • Now, goto "File" menu --> "New" --> "Other" --> "Maven Project"

  • In the POM.xml include the dependencies. At the time of writing, these are the versions of Dependencies added from

io.cucumber cucumber-java Version 6.10.4

junit junit Version 4.13.2

test io.cucumber cucumber-junit Version 6.10.4

test org.seleniumhq.selenium selenium-java Version3.141.59

Code to be included in POM.xml is as follows

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">








<!-- -->






<!-- -->







<!-- -->







<!-- -->








  • Now Clean and Build the Project for the dependencies to be reflected in the project.

  • It's time to get more organized and create some folders and packages.

    • Under src/test/resources, Right click --> "New" --> "Folder" --> Give folder name as "Features"

    • Under the Features folder, Right click --> "New" --> "File" --> Give name as "GoogleHomeTest.feature". (Note: File extension should be .feature). A file with some auto-generated contents will be created. Clear all the auto-generated contents.

    • Under src/test/java, Right click --> "New" --> "Package" --> Give name as "stepDefinition"

  • Under src/test/java/stepDefinition, Right click --> "New" --> "Class" --> Give filename as "GoogleSearch"

  • Organizing done, let's get back. Goto the feature file that we created. After clearing all the auto generated content in the file, enter the following feature, scenario and gherkin (The given, when, then format is called Gherkin format)

Feature: feature to test google search
Scenario: Validate google search is working
          Given browser is open
          And User is on google search page
          When User enters a text in a search box
          And hits Enter
         Then User is navigated to search feature

Note: To format it, just Right Click --> "Pretty Format" and the system will automatically format the file for you :)

  • Now, select GoogleHomeTest.Feature --> "Run As" --> "Cucumber Feature"

  • When you see the console, it will be something like below. Note that it says "You can implement missing steps with the snippets below". Copy the code below it and paste it in ""

  • After pasting in "", clear all the exceptions by importing the relevant classes.

  • Note that the "And" condition in our feature file was not reflected. Instead, "Given" and "When" comes twice. Change the second occurrences to "And" annotation. Also, clear all the exceptions. Your Java file will look like below.

  • Now, create all the implementations. Mention what has to be done at each step under the relevant method.

  • This will be the final source code of

package stepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;


public class GoogleSearch {

 WebDriver driver = null;
 @Given("browser is open")
 public void browser_is_open() {
 // Initiate the Chrome-driver and open the browser.
  System.setProperty("", "/Users/macuser/Selenium/Drivers/chromedriver");
  driver = new ChromeDriver();

 @And("User is on google search page")
 public void user_is_on_google_search_page() {
  // Navigate to

 @When("User enters a text in a search box")
 public void user_enters_a_text_in_a_search_box() {
  // In the google search box, enter any text - Say "Cucumber Test"
  driver.findElement("q")).sendKeys("Cucumber Test");

 @And("hits Enter")
 public void hits_enter() {
 // Simulate the clicking of enter key

 @Then("User is navigated to search feature")
 public void user_is_navigated_to_search_feature() {
 // The test case should pass if the resulting page 
 // contains an expected text - Say ""What is Cucumber Test"
 driver.getPageSource().contains("Test automation Software"); 
 // Finally, closing driver after test is complete

Now, goto GoogleHomeTest.feature and Right Click --> "Run As" --> Cucumber Feature. This will be the console output.

Notice that our scenarios and steps have passed. CONGRATULATIONS...!!!! You have successfully created the first Cucumber test :)

  • Now, to Run the cucumber test as a JUnit, you need a Runner. For this, under src/test/java/stepDefinition, Create new class named ""

  • The source code for is as follows:

package stepDefinition;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

//Cucumber options:
 // Path of our Feature file(s)
 // Glue contains the package(s) where we have implemented our step definitions 
@CucumberOptions (features="src/test/resources/Features", glue={"stepDefinition"}) 

public class MyRunner {


  • Within the, Right Click --> Run As --> JUnit Test

  • After execution, you will get an output as follows.

Its Time to pat yourself on your back... Yessssss! You have successfully implemented and executed your first cucumber test.. One down, many more to go.......!

1,033 views0 comments

Recent Posts

See All

Exception Handling in Selenium Webdriver

What is an exception? An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prev


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page