Introduction

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.

Prerequisites

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 {
    jcenter()    
}

dependencies {
    implementation('io.cucumber:cucumber-java:5.3.0')
	implementation('io.cucumber:cucumber-junit:5.3.0')
	implementation('org.seleniumhq.selenium:selenium-server-standalone:2.53.0')
	testImplementation('org.junit.jupiter:junit-jupiter-engine:5.6.0')
	testImplementation('org.junit.vintage:junit-vintage-engine:5.6.0')
}

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>com.roytuts</groupId>
	<artifactId>cucumber-tags</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>at least 1.8</java.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-java</artifactId>
			<version>5.3.0</version>
		</dependency>
		
		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-junit</artifactId>
			<version>5.3.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-server-standalone</artifactId>
			<version>2.53.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.6.0</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.junit.vintage</groupId>
			<artifactId>junit-vintage-engine</artifactId>
			<version>5.6.0</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
                <configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

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;

@RunWith(Cucumber.class)
@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;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class CukeStepDefinition {

	WebDriver driver = null;

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

		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.navigate().to("https://www.google.com/");
	}

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

	@Then("Google should return some results")
	public void google_should_return_some_results() {
		new WebDriverWait(driver, 10);
		try {
			List<WebElement> results = driver
					.findElements(By.xpath("//div[@class='bkWMgd']//div[@class='srg']//div[@class='g']/link"));
			System.out.println("Elements: " + results.size());

			assertTrue(!results.isEmpty());

			for (WebElement webElement : results) {
				System.out.println(webElement.getAttribute("href"));
			}
		} finally {
			driver.close();
			driver.quit();
		}
	}

}

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)
0m19.704s

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 *