Cucumber with Selenium WebDriver

In this post, we will automate the Google calculator feature using cucumber as TDD framework and selenium for web automation using Java. You can download the entire project(18kb only) here - Cucumber with Selenium Sample Project.

The structure of the maven project would be-

Cucumber project structure

Now we will refer to each file marked in the image one by one-

POM File

A POM file describes all the dependencies required in the project. This project is created as maven project so all you need to do is include the POM file and all the required dependencies/libraries will get downloaded from maven repositories.
The libraries used in this project are-

  • Selenium, JUnit, Cucumber-Java and Cucumber-JUnit.
  • Pom.xml file content-
<project xmlns="" 

Feature file

This file will defines the scenario to be tested. In this example we we will write scenario to test Google calculator. Feature file content-

Feature: Check addition in Google calculatorcontent
   In order to verify that Google calculator work correctly
   As a user of Google
   I should be able to get correct addition result

   Scenario: Addition
   Given I open Google
   When I enter "2+2" in search textbox
   Then I should get result as "4"

Step definition file

This file implements the steps stated in feature file e.g. for the step "Given I open Google" the step definition file will have a function that will launch browser and open Google as an implementation to this step.

package artOfTesting.test;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;


public class googleCalcStepDefinition {
	protected WebDriver driver;
	    public void setup() {
	        driver = new FirefoxDriver();
	@Given("^I open google$")
	public void I_open_google() {
		//Set implicit wait of 10 seconds and launch google
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	@When("^I enter \"([^\"]*)\" in search textbox$")
	public void I_enter_in_search_textbox(String additionTerms) {
		//Write term in google textbox
		WebElement googleTextBox = driver.findElement("gbqfq"));
		//Click on searchButton
		WebElement searchButton = driver.findElement("gbqfb"));;
	@Then("^I should get result as \"([^\"]*)\"$")
	public void I_should_get_correct_result(String expectedResult) {
		//Get result from calculator
		WebElement calculatorTextBox = driver.findElement("cwos"));
		String result = calculatorTextBox.getText();
		//Verify that result of 2+2 is 4
		Assert.assertEquals(result, expectedResult);
	    public void closeBrowser() {


JUnit Test Runner file

The JUnit Test runner file that will be used to run the project as maven test.

package artOfTesting;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

public class googleCalcTest {


In order to run the project just right click on JUnit test runner file and click on "Run As -> JUnit test", you can also run the project by right clicking on project and clicking on "Run As -> Maven test".