MainMenu

Home Java Overview Maven Tutorials

Monday 7 November 2022

Cucumber Tutorial with JUNIT

Hello Friends,
In this article , we will work with Selenium BDD (Behaviour driven development) using Cucumber.
So we will learn the cucumber with below topics :-

1). How to Configure/Install Cucumber in eclipse

2). How to create maven project & change it to cucumber project

3). Dependencies required to configure the cucumber

4). Create a feature file

5). Create step definition file

6). Create runner file

7). Run cucumber project



How to install the Cucumber plugin in Eclipse: please follow the below path in eclipse

Help -->> Eclipse Market Place --> Search "Cucumber" in search box --> Click on Install
as shown in below image


OR Help -->> Install New Software --> Click on Add button --> http://cucumber.github.io/cucumber-eclipse/update-site -->Save




Now Create a Maven Project


Now configure the POM.xml file == >Add Cucumber, Junit dependency , ==>> add plugins


<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>way2testingCucumber</groupId>
<artifactId>way2testingCucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>way2testingCucumber</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.version>7.14.1</cucumber.version>
<surefire.version>3.2.2</surefire.version>
<maven.complier.version>3.11.0</maven.complier.version>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumber.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.complier.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version></plugin>
</plugins></pluginManagement>
</build>
</project>



Create a Resource folder and feature file inside it




Create a package "StepDefinition" and created a class and write all steps definitions


Create a runner package and a runner class and add Cucumber Options

package Runner;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "D:\\Software\\Selenium\\01_Cucumber_Framework_2024\\WorkSpace\\way2testingCucumber\\Resource",
glue = {"Step_definitions"},
plugin = {"pretty", "html:target/cucumber-reports.html", "json:target/report.json"},
tags="@Regression",
monochrome =true )
public class TestRunner {
}





Now run the cucumber test either via JUNIT or by Maven



SetUp Cucumber Maven JUnit from scratch


Create project with Cucumber Maven JUnit from scratch




No comments:

Post a Comment