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 1.0.0.202106240526 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 https://mvnrepository.com

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="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>CucumberDemo</groupId>

<artifactId>CucumberDemo</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>CucumberDemo</name>

<description>CucumberDemo</description>


<dependencies>


<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->

<dependency>

<groupId>io.cucumber</groupId>

<artifactId>cucumber-java</artifactId>

<version>6.10.4</version>

</dependency>


<!-- https://mvnrepository.com/artifact/junit/junit -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13.2</version>

<scope>test</scope>

</dependency>


<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->

<dependency>

<groupId>io.cucumber</groupId>

<artifactId>cucumber-junit</artifactId>

<version>6.10.4</version>

<scope>test</scope>

</dependency>


<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>3.141.59</version>

</dependency>


</dependencies>

</project>


  • 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 "GoogleSearch.java"

  • After pasting in "GoogleSearch.java", 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 GoogleSearch.java

package stepDefinition;

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

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


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("webdriver.chrome.driver", "/Users/macuser/Selenium/Drivers/chromedriver");
  driver = new ChromeDriver();
  driver.manage().window().maximize(); 
 }


 @And("User is on google search page")
 public void user_is_on_google_search_page() {
  // Navigate to google.com
  driver.navigate().to("https://google.com");
 }


 @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(By.name("q")).sendKeys("Cucumber Test");
 }


 @And("hits Enter")
 public void hits_enter() {
 // Simulate the clicking of enter key
 driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
 }


 @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
   driver.close(); 
 
 }
}


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 "MyRunner.java"


  • The source code for MyRunner.java is as follows:

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

@RunWith(Cucumber.class) 
//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 MyRunner.java, 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.......!



168 views0 comments

Recent Posts

See All

How to Create your Own API

APIs are everywhere, and they play a vital role in modern-day technology. Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each t