Jump to content
JasperReports Library 7.0 is now available ×
  • Extent Report for Cucumber 7 (Extent reports for Cucumber-JVM version 7) | Cucumber, TestNG, Selenium Java , Maven

    This post outlines about - how to integrate Extent Report for cucumber 7 that is integrated with TestNG, Selenium Java and Maven. 


    You can clone the complete example from this GitHub repo or down load the zip archive file here


    This code has two cucumber scenarios independent of each other but uses the same Background in the feature files. And, I've deliberately failing these scenarios to view the screen shot attachments in Extent Report(Spark Report). 

    Update this piece of yellow highlighted string from this statement in HRMLoginPage.java so both the scenarios will get succeeded. 




    You could also refer this no voice video walkthrough tutorial. 

    NOTE :

    The best practice is to keep the code under src/test/java or src/test/resources but this makes hard to have the executable jar crated, so code should reside under src/main/java or src/main/resources. 


    Project outline:



    Step 1: Add the following dependency to the pom.xml file
    <dependency>	<groupId>tech.grasshopper</groupId>	<artifactId>extentreports-cucumber7-adapter</artifactId>	<version>1.7.0</version></dependency>

    Step 2 :  Add the extent cucumber adapter as plugin in the @CucumberOptions

     Cucumber options are normally given in the Cucumber Test Runner class, for instance in RunCucumberTest. java

    plugin = { "pretty", "json:target/cucumber-reports/cucumber.json","html:target/cucumber-reports/cucumberreport.html","com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},

     Step 3 : Create extent.properties file in src/main/resources and add below properties

    extent.reporter.spark.start=true extent.reporter.spark.out=target/SparkReport/Spark.html screenshot.dir=target/screenshots/ screenshot.rel.path=../screenshots/ 

    If we want to generate the extent report(say Spark report)  in an executable jar file, add the plugin in main method as shown in below BasePage.java. 

    When run the cucumber scenarios from jar file , the jar generates "target" folder and inside it we can find the Extent Report that is Spark.html


    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","@SmokeTest",	        	                	        "-p", "pretty", 	        "-p", "json:target/cucumber-reports/cucumber.json", 	        "-p", "html:target/cucumber-reports/cucumberreport.html",	        "-p","com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",	        	        "-m"	    }	    );	} catch (Exception e) {	        e.printStackTrace();	        System.out.println("Main method exception : " + e);	}	}}


    Save the project and run it as TestNG from eclipse or you could run the scenarios from the executable jar file and analyze the reports generated in "target" folder. 


    The Extent Report(Spark.html) with successful execution should look as shown in below image. 




    Now, what if you want to attach screen shots for the failed scenarios ? 

    The location is given the extent.properties file 

    screenshot.dir=target/screenshots/ screenshot.rel.path=../screenshots/ 

    In order to get the screen shots , add below piece of code in Cucumber @After hook with order=2 since the order=1 is used to quit the driver after each scenario. This order plays a vital role in terms of attaching screen short after a failed scenario. 

    if ((scenario.isFailed())) {	final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);			scenario.attach(screenshot, "image/png", scenario.getName());}

    package com.sadakar.common;import java.time.Duration;import org.openqa.selenium.OutputType;import org.openqa.selenium.TakesScreenshot;import org.openqa.selenium.chrome.ChromeDriver;import io.cucumber.java.After;import io.cucumber.java.Before;import io.cucumber.java.Scenario;public class Hooks extends BasePage {	@Before //Cucumber hook - runs for each scenario	public static void setupDriver() throws InterruptedException {		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");		driver = new ChromeDriver();		driver.manage().window().maximize();		driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");				driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));	}	@After(order=1) //Cucumber hook - runs for each scenario	public static void quitDriver() throws Exception {		driver.quit();	}	@After(order = 2) // Cucumber After Hook with order 1	public void takeScreenShotOnFailedScenario(Scenario scenario) {		System.out.println("Taking screenshot from Cucumber After hook with order=2 if the scenario fails");		if ((scenario.isFailed())) {			final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);			scenario.attach(screenshot, "image/png", scenario.getName());		}	}}

    The same code base above is given to have bad login so it will fail both the scenarios and so the Sprak.html look like below with the screen shot attached. 







    That's all we have to do to integrate Extent Report with cucumber 7. 

    I hope you found this demo is useful! Keep an eye on this space for interesting automation updates and do subscribe the YouTube channel. 


    User Feedback

    Recommended Comments

    There are no comments to display.

    This is now closed for further comments

  • Create New...