TestNG Extent Report – v1.3.x

Update: Updated to v1.3.1 – Now emailable report will also be generated
Update: Updated to v1.2.0

The TestNG Extents report is a listener plugin which you can add it as a listener to your TestNG suite to generate a nice Extent report.

This listener uses the ExtentReports v3.0.0, a library developed by Anshoo Arora for reporting.

Why another library?

You don’t have to spend time in re-inventing on how to define what. Just add this as a listener or service loader. You will get the nice looking report generated at the end of your test execution.

Also, you will get the emailable report out-of-the box.

Usage

For maven, add the following as dependency:

<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>testng-extentsreport</artifactId>
    <version>1.3.1</version>
</dependency>

Either in your testng.xml, add the listener:

<listeners>
    <listener class-name="com.vimalselvam.testng.listener.ExtentTestNgFormatter" />
</listeners>

or add as a service loader (recommended).

By default, the report will be generated at TestNG’s output directory. i.e., test-output/report.html and the emailable report at test-output/emailable-report.html.

In case you want to generate the report in a different location, make sure you pass the JVM argument called reportPath with the absolute directory path.

For example: I run my maven test as:
mvn clean test -DreportPath=output. This will generate both the reports in my ${project directory}/output.

Adding custom reporter config

You can customize the report using a XML file. The XML file should follow as given here: ExtentReports Configuration.

The XML file should be referred as parameter in your suite xml. The parameter name should be report.config.
For instance, the parameter in the suite xml should be as follows:

<parameter name="report.config" value="src/test/resources/extent-config.xml" />

The value should be the config XML file path.

Adding System Information

In case you want to add system information, you will have to implement an interface com.vimalselvam.testng.SystemInfo. This interface contains a method with return type as Map<String, String>.
Construct your system information with the map and return that map. After you have implemented, the custom implementation should be referred in your TestNG Suite xml as a parameter at the suite level.

For instance add the following parameter at your suite level in the TestNG suite xml:

<parameter name="system.info" value="test.MySystemInfo" />

The parameter name should be system.info and the value should be your fully qualified custom implementation class name.
For example my custom implementation look like this:

package test;

import com.vimalselvam.testng.SystemInfo;
import org.testng.collections.Maps;

import java.util.Map;

/**
 * This is a small utility class to prepare the system information
 */
public class MySystemInfo implements SystemInfo {
    @Override
    public Map<String, String> getSystemInfo() {
        Map<String, String> systemInfo = Maps.newHashMap();
        systemInfo.put("Test Env", "QA");
        systemInfo.put("Browser", "firefox");
        return systemInfo;
    }
}

Instance of the Listener

At any point in time, you can get the instance of the listener as ExtentTestNgFormatter.getInstance()

Adding Screenshot

To add the screenshot, you have two options.
* If the screenshot can be added from the test method, then

ExtentTestNgFormatter.getInstance().addScreenCaptureFromPath(filePath);
  • If the screenshot can be added from the configuration method, then for example:
@AfterMethod
public void afterMethod(ITestResult iTestResult) throws IOException {
    // The ITestResult is a mandatory parameter
    ExtentTestNgFormatter.getInstance().addScreenCaptureFromPath(iTestResult, filePath);
}

Adding info log

If any case, you want to attach the info log on your test method, you simply call TestNg’s Reporter.log method which will be added to your test log. For example:

@Test
public void testMethod() {
    Reporter.log("Custom log");
}

Adding test runner output

To add the test runner output, simply call from anywhere:

ExtentTestNgFormatter.getInstance().setTestRunnerOutput("My output");

Adding new node

Sometimes you may have to add new node under your test. This situation arises when you have iteration in the test method and considering each iteration as a separate node in the report.
This could be solved by using addNewNodeToTest() method.

Refer DemoTestClass on how to add new nodes and in case of failure, how to fail the added node.

Cucumber?

Refer my another library which generates the same report for the cucumber based BDD tests: CucumberExtentReporter

10 Replies to “TestNG Extent Report – v1.3.x”

  1. I am getting the following after capturing screenshot and trying to attach it

    @AfterMethod
    public void afterMethod(ITestResult iTestResult) throws IOException {
    System.out.println(“ITestResult ” + iTestResult.getStatus());
    if(iTestResult.getStatus()==ITestResult.FAILURE) {
    String screenfilepath = capture_screenshot(iTestResult.getName());
    System.out.println(screenfilepath);
    ExtentTestNgFormatter.addScreenCaptureFromPath(iTestResult, screenfilepath);
    }
    }

    Screenshot path is something like “C:\Screenshots\xyz.png”

    Getting the following error when executing:
    java.lang.NullPointerException at com.vimalselvam.testng.listener.ExtentTestNgFormatter.addScreenCaptureFromPath(ExtentTestNgFormatter.java:124)

    Can you please help me in resolving the issue?

    1. The iTestResult.getName() will output only the name. You should have an absolute file path to attach a screenshot. Kindly make sure you pass the absolute file path.

  2. Hi Vimal,

    Able to add info steps in the report using Reporter.log, but how to Pass or Fail steps with descriptions.
    I couldn’t find it. Can you please help?

    Thanks.
    Rajaseekhar

  3. I am getting this error on using your library:
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project ServiceCloudAndroid: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
    [ERROR] java.lang.NoClassDefFoundError: com/aventstack/extentreports/reporter/ExtentHtmlReporter
    [ERROR] at com.vimalselvam.testng.listener.ExtentTestNgFormatter.(ExtentTestNgFormatter.java:54)

  4. Hi and thank you.

    Im not sure how to re-point screenshot to another folder. By default it expects that the image file located at test-output folder.

    report.addScreenCaptureFromPath(fileName);

    Basically I need screenshots at the same location as my config reportPath=myOutput

    Please advise

    1. If you give the full path of the screenshot, it will use that. For example:
      report.addScreenCaptureFromPath(“C:\users\myprojectdir\myOutput\screenshot.png”);

Leave a Reply