MainMenu

Home Java Overview Maven Tutorials

Friday 8 March 2024

Cucumber Tutorial with TestNG

Hello Friends,
In this article , we will work with Selenium BDD (Behaviour driven development) using Cucumber.
Here we will run our test with TestNG




Now Create a Maven Project


Now configure the POM.xml file == >Add Cucumber, TestNG 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>
<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>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration> </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.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
//@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 extends AbstractTestNGCucumberTests{
}

}





Now create TestNG.xml file



<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name = "Runner.TestRunner"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Now run the cucumber test either via TestNG or by Maven



SetUp Cucumber Maven TestNG from scratch


Create project with Cucumber Maven TestNG from scratch




No comments:

Post a Comment