This tutorial will show you how to perform automation testing using Cucumber and Selenium web driver. Cucumber is a BDD framework used to perform behavior testing whereas Selenium is used to perform automation testing for web applications across different browsers and platforms.

In this example we will search a keyword in Google website and verify whether it returns a result or not. The entire process will be automated using the Selenium driver. The driver will open the browser, type the keyword into the Google search box, submit the form, verify the result is not empty and finally close the browser.


Eclipse 2019-12, Java at least 8, Maven 3.6.3, Gradle 6.1.1, Cucumber 5.3.0, Junit 5.6.0, Selenium 2.53.0, Chrome Driver

Create Project

Create gradle or maven based project in Eclipse. The name of the project is cucumber-selenium-driver. We are using here Junit 5 so we need to use Junit Vintage engine as Cucumber is compatible with Junit 4 only as of now.

Remember feature file, Java classes are written into src/test/resources or src/test/java folder.

If you are creating gradle based project then you can use the below build.gradle script.

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

If you creating maven based project then you can use below pom.xml file:

<project xmlns="" xmlns:xsi=""
		<java.version>at least 1.8</java.version>

Feature File

Feature file, is a language agnostic, where we write steps for software features to group scenarios using Gherkin language. More information can be found in the documentation.

The following feature says that we are going to perform a search activity in Google search box.

The following file cukeSelenium.feature is kept under src/test/resources/cuke/features folder.

Feature: Search Automation

Scenario: Search in Google

	Given user navigates to Google
	When I enter spring in search box
	Then Google should return some results

Runner File

The runner class is required to execute your feature file and step definition file which we will see later.

The class we have to configure mainly using @RunWith and @CucumberOptions.

The @RunWith annotation tells to run the class with Cucumber framework. The @CucumberOptions annotation has different options as given below:

  • features – location of the feature file
  • glue – the package where the step definition class will be written
  • monochrome – we want the output in console in human readable format
  • plugin – in what format and where we want the generated output file
package com.roytuts.cucumber.selenium.driver.runner;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@CucumberOptions(strict = true, features = "classpath:cuke/features/cukeSelenium.feature", glue = "com.roytuts.cucumber.selenium.driver.step", monochrome = true, plugin = {
		"pretty", "html:target/cucumber", "json:target/Cucumber.json", "junit:target/Cucumber.xml" })
public class CukeRunner {


Step Definition

The step definition file is language dependent and here we are using Java language to write our step definition file.

Running the above CukeRunner file will give you the steps definition in the eclipse console. If you want you may also write yourself such step definition to match the steps written in feature file.

The methods which have been generated from the feature file throw PendingException() because we have not yet implemented any step defined in feature file.

Now removed the PendingException() to denote that these methods are no more pending and write the required implementation code.

The final class is given below:

package com.roytuts.cucumber.selenium.driver.step;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

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


public class CukeStepDefinition {

	WebDriver driver = null;

	@Given("user navigates to Google")
	public void user_navigates_to_Google() {
		System.setProperty("", "C:/chromedriver.exe");

		driver = new ChromeDriver();

	@When("I enter spring in search box")
	public void i_enter_spring_in_search_box() {
		WebElement element = driver.findElement("q"));

	@Then("Google should return some results")
	public void google_should_return_some_results() {
		new WebDriverWait(driver, 10);
		try {
			List<WebElement> results = driver
			System.out.println("Elements: " + results.size());


			for (WebElement webElement : results) {
		} finally {


In the above file we have setup the Google Chrome driver as we are using Chrome web driver. Please make sure you have downloaded the chrome driver from the link given in Prerequisites section.

Testing the Application

Now running the CukeRunner class will pass all the test cases as shown below from he console log:

1 Scenarios (1 passed)
3 Steps (3 passed)

So when you run the CukeRunner class you will see that a Chrome browser is opened, maximized, word “spring” is typed into the search box, results are displayed and browser gets closed automatically.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *