How to run cucumber scenarios from executable jar ? | Selenium Java, TestNG, Cucumber, Maven | How to use io.cucumber.core.cli.Main.main() ?
jasper-bi-suite.blogspot.com/2022/09/how-to-run-cu…
In this blog post , we will learn majorly the following topics.
jasper-bi-suite.blogspot.com/2022/09/how-to-run-cu…
1. How to create an executable jar for cucumber+selenium+testng+maven project ?
2. How to run scenarios from generated jar ?
To create an executable jar for maven project there are many jar plug-in's available in the maven space.
In this example, I'd like to demonstrate it using maven-assembly-plugin.
The demo project for HRM application can be found at GitHub or download this maven zip file
You could also watch this no voice video tutorial for quick references
(Pls do subscribe for more automation updates)
Now, let's see the core part that we start with pom.xml
We need to have the maven-assembly-plugin is given the pom file, i.e., add below piece of code in the pom.xml file.
Refer the blog post for maven-assembly-plugin in pom.xml as YouTube does not supports angular brackets
In a java class, using "main" method, we need to pass the glue code, tags, plug-ins and etc that are related to cucumber as arguments.
For example, I've used, BasePage.java class that should contain normal "main" method and inside it use Main.main(....) to pass cucumber arguments so the scenarios based on tags would be runnable from command line.
import package : import io.cucumber.core.cli.Main; in the BasePage.java class and the passing of arguments are shown in below class.
BasePage.java
package com.sadakar.common;
import org.openqa.selenium.WebDriver;
import io.cucumber.core.cli.Main;
public class BasePage {
public static WebDriver driver;
public static void main(String args[]) throws Throwable {
try {
Main.main(new String[] {
"-g","com.sadakar.common",
"-g","com.sadakar.stepdefinitions",
"-g","com.sadakar.testng.runner",
"classpath:features",
"-t","@HRMLogin",
"-p", "pretty",
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",
"-m"
}
);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Main method exception : " + e);
}
}
}
In eclipse, build the project using below command:
Ensure that any open folders, command prompts, chrome drivers in task manager are all closed.
Clean the project and then build it.
clean package assembly:single -Dmaven.test.skip=true
Using -Dmaven.test.skip=true option we can skip the tests while the project builds!
maven-assembly-plugin generates two jar files, they are
1. CucumberTestNGSeleniumExecutableJar-0.0.1-SNAPSHOT.jar
2. CucumberTestNGSeleniumExecutableJar-0.0.1-SNAPSHOT-jar-with-dependencies.jar
The jar with the dependencies is called fat jar file and this is the one we call as "executable" or "runnable" jar.
Make sure that this jar contains the MANIFEST.MF file containing the class that has the main method. Use jar extract tools to view the content inside the jar, I've used "Java Decompiler".
The MANIFEST.MF file should contain all the dependencies with Main-Class:com.sadakar.common.BasePage as shown in below.
Now, let's run the jar file:
Double click on the executable jar, it will start executing the scenarios!
Or use below command to run the cucumber scenarios
java -jar CucumberTestNGSeleniumExecutableJar-0.0.1-SNAPSHOT-jar-with-dependencies.jar
-Dcucumber.filter.tags="@HRMLogin"
his execution, will generate, cucumber reports in "target" folder where the jar is located.
The report is based on the plug-in(s) that we provide in BasePage.java file.
For example, we gave it as
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",
click on the "cucumberreport.html" file to analyze the report generated
That's all we have to do to create an executable cucumber, testng jar file using maven and to run the scenarios from it.
I hope you found this write-up useful, keep watch this blog site for more automation!