4.2 Sample Extent Reports

Extent Reports is a multi-format reporting library for Java Developers which creates an interactive HTML report, an email report, and also provides real-time log information. The ExtentAPI also integrates with the ExtentX server to provide in-depth views of your tests.Extent addresses the reporting issues in TestNG.

Below is the code which can be used to run tests

package code;

import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.relevantcodes.extentreports.DisplayOrder;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.NetworkMode;

import CONSTANTS;

public class ExtentManager {
    public static ExtentReports extent;
    protected static final String filePath=System.getProperty("user.dir")+File.separator
    +"ExtentReports"+File.separator+"TestReport.html";
    private static boolean replaceExisting=true;
    protected static final DisplayOrder displayOrder= DisplayOrder.NEWEST_FIRST;

    public static ExtentReports getInstance(String EnvironmentToRunOn) {
        if (extent == null) {
            extent = new ExtentReports(filePath, replaceExisting,displayOrder, NetworkMode.OFFLINE); 
            extent.loadConfig(new File(CONSTANTS.ExtentConfigPath));
            Map<String, String> sysInfo = new HashMap<String, String>();
            sysInfo.put("Selenium Version", "2.53.1");
            sysInfo.put("Environment", EnvironmentToRunOn);
            extent.addSystemInfo(sysInfo);            
        }
        return extent;
    }
}

class ExtentReports {

    public ExtentReports extent;
    public ExtentTest test;
    public String getCurrentlyLoggedInUser=System.getProperty("user.name");

    @Test       
   public void InitializeExtentReport(){
        //Instantiating the ExtentReports
        extent=ExtentManager.getInstance(Environment);
        extent.assignProject("Test Extent");
        // start x requires MongoDB hence commented to generate reports on the other machines
        Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
        mongoLogger.setLevel(Level.SEVERE);
        extent.x("localhost");*/
        test=extent.startTest("Test1", ""Application started." );
        test.assignAuthor(getCurrentlyLoggedInUser);
        test.assignCategory("RegressionTestCases");
        }
    }

We need to keep in mind that the extent reports must be under @Test annotation of TestNG otherwise it will not generate the reports.

After running the tests we will be able to view report in the specified folder as "TestReport.html"

Last updated

Was this helpful?