Cucumber — Example with Java and Spring Boot | Code Factory

Cucumber Tutorial Index Page: Link

<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.codeFactory</groupId>
<artifactId>cucumber-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cucumber-demo</name>
<description>Demo project for Cucumber</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.codeFactory.cucumberJava;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String[] args) throws InterruptedException {

// https://github.com/mozilla/geckodriver/releases

/*System.setProperty("webdriver.gecko.driver", "<path to your gecko driver executable>");*/
System.setProperty("webdriver.gecko.driver", "C:\\Users\\CodeFactory\\Downloads\\geckodriver-v0.28.0-win64\\geckodriver.exe");

WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/signup");
if (driver.findElement(By.id("u_0_v")).isEnabled()) {
System.out.println("Test 1 Pass");
} else {
System.out.println("Test 1 Fail");
}
driver.close();
}
}
  • An instance of Firefox web browser will open.
  • It will open the Facebook signup/register page on the browser.
  • It will detect the <div> of password field.
  • The browser will close.
  • In the JUnit window, you will see a scenario with green tick mark, which indicates success of the test execution.
Feature: CucumberJava
Scenario: Login functionality exists
Given I have open the browser
When I open Facebook website
Then Login button should exits
package com.codeFactory.cucumberJava;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
/**
* @author code.factory
*
*/
public class cucumberJava {
WebDriver driver = null;
static {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\CodeFactory\\Downloads\\geckodriver-v0.28.0-win64\\geckodriver.exe");
}

@Given("^I have open the browser$")
public void openBrowser() {
driver = new FirefoxDriver();
}
@When("^I open Facebook website$")
public void goToFacebook() {
driver.get("https://www.facebook.com/signup");
}
@Then("^Login button should exits$")
public void loginButton() {
if (driver.findElement(By.id("u_0_v")).isEnabled()) {
System.out.println("Test 1 Pass");
} else {
System.out.println("Test 1 Fail");
}
driver.close();
}
}
package com.codeFactory.cucumberJava;import org.junit.runner.RunWith;import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
/**
* @author code.factory
*
*/
@RunWith(Cucumber.class)
@CucumberOptions(format = { "pretty", "html:target/cucumber" })
public class runTest {
}
  • Select runTest.java file from the package explorer.
  • Right-click and select the option, Run as.
  • Select JUnit test.
  • An instance of Firefox web browser will open.
  • It will open the Facebook signup/register page on the browser.
  • It will detect the <div> of password field.
  • The browser will close.
  • In the JUnit window, you will see a scenario with green tick mark, which indicates success of the test execution.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Troubleshoot Problems with Your String Trimmer

Build Your Own EC2 Machine Images with Packer & Ansible on AWS for Immutable AWS Deployments

Integrating LVM With Hadoop and Provide Elasticity

Streaming with gRPC on .NET

Tips to Make Mobile App Development Outsourcing Easier

How to use multiple keyboards — AutoHotkey & Lua Macros

Photo by Cristi Goia on Unsplash

Case Study: Uphold transfer process optmization — Code for Venezuela Hackathon

Setting up a Windows 10 Surface Pro for your Web App Kiosk

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Java Virtual Machine

What is SPRING BOOT ?

Cloud Storage, Java

Apache Kafka Configuration In Spring Boot With Producer And Consumer Example