Cucumber — Example with Java and Spring Boot | Code Factory

Donate : Link

WordPress Blog : Link

Applications… : Link

Cucumber Tutorial Index Page: Link

pom.xml

<?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>

Test.java

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();
}
}

Run Test.java and you observe following things :

  • 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.

cucumberJava.feature

Feature: CucumberJava
Scenario: Login functionality exists
Given I have open the browser
When I open Facebook website
Then Login button should exits

cucumberJava.java

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();
}
}

runTest.java

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 {
}

Run the test using option :

  • Select runTest.java file from the package explorer.
  • Right-click and select the option, Run as.
  • Select JUnit test.

You observe following things :

  • 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.