0

I have made a script to generate an extent report, but HTML is not generated.

Please check below code:

Report.java:
package report;

import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class Report {

    ExtentHtmlReporter htmlReporter;
     static ExtentReports extent;
     static ExtentTest logger;


     @BeforeTest
     public void startReport(){

     htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/STMExtentReport.html");
     extent = new ExtentReports ();
     extent.attachReporter(htmlReporter);
     extent.setSystemInfo("Host Name", "SoftwareTestingMaterial");
     extent.setSystemInfo("Environment", "Automation Testing");
     extent.setSystemInfo("User Name", "Rajkumar SM");

     htmlReporter.config().setDocumentTitle("Title of the Report Comes here");
     htmlReporter.config().setReportName("Name of the Report Comes here");
     htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
     htmlReporter.config().setTheme(Theme.STANDARD);
     }

     @Test
     public void passTest(){
     logger = extent.createTest("passTest");
     Assert.assertTrue(true);
     logger.log(Status.PASS, MarkupHelper.createLabel("Test Case Passed is passTest", ExtentColor.GREEN));
     }

     @Test
     public void failTest(){
     logger = extent.createTest("failTest");
     Assert.assertTrue(false);
     logger.log(Status.PASS, "Test Case (failTest) Status is passed");
     logger.log(Status.PASS, MarkupHelper.createLabel("Test Case (failTest) Status is passed", ExtentColor.GREEN));
     }

     @Test
     public void skipTest(){
     logger = extent.createTest("skipTest");
     throw new SkipException("Skipping - This is not ready for testing ");
     }

     @AfterMethod
     public void getResult(ITestResult result){
     if(result.getStatus() == ITestResult.FAILURE){
     //logger.log(Status.FAIL, "Test Case Failed is "+result.getName());
     //MarkupHelper is used to display the output in different colors
     logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
     logger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
     }else if(result.getStatus() == ITestResult.SKIP){
     //logger.log(Status.SKIP, "Test Case Skipped is "+result.getName());
     logger.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + " - Test Case Skipped", ExtentColor.ORANGE)); 
     }
     }
     @AfterTest
     public void endReport(){
     extent.flush();
        }


}

The .xml file :

<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
    <configuration>
        <!-- report theme -->
        <!-- standard, dark -->
        <theme>standard</theme>

        <!-- document encoding -->
        <!-- defaults to UTF-8 -->
        <encoding>UTF-8</encoding>

        <!-- protocol for script and stylesheets -->
        <!-- defaults to https -->
        <protocol>https</protocol>

        <!-- title of the document -->
        <documentTitle>Extent</documentTitle>

        <!-- report name - displayed at top-nav -->
        <reportName>Automation Report</reportName>

        <!-- location of charts in the test view -->
        <!-- top, bottom -->
        <testViewChartLocation>bottom</testViewChartLocation>

        <!-- custom javascript -->
        <scripts>
            <![CDATA[
                $(document).ready(function() {

                });
            ]]>
        </scripts>

        <!-- custom styles -->
        <styles>
            <![CDATA[

            ]]>
        </styles>
    </configuration>
</extentreports>
  • your startReport() method is @BeforeTest so it will execute before each test class and each time it will override the old HTML file, and your last Test is throwing skipexception. – Upkar Singh May 2 at 10:59
  • ok then what should i have to do@upkar Singh – Rutvi Soni May 2 at 11:34
  • is your empty HTML file has been created in your test-output folder? if not then is it throwing any exception ? – Upkar Singh May 2 at 11:51
  • i have encountered the same problem when using TestNG version "7.0.0". Try to use version 6.14.3. PS: ExtentReports version that i'm using is "3.1.5". – Mohamed Achraf Younes Aug 29 at 13:54
1

I have created an example on the basis of your code, please note that here i'm using the Extent Report Version 3 so change your code accordingly.
I hope this code help you-

public class Report  {

     static ExtentTest logger;
     static ExtentReports extent;
     static ExtentHtmlReporter reporter;
    @BeforeClass
    public void startreport() {

        extent = new ExtentReports();       
        extent.attachReporter(getHtmlReporter());
        extent.setSystemInfo("Host Name", "SoftwareTestingMaterial");
        extent.setSystemInfo("Environment", "Automation Testing");
        extent.setSystemInfo("User Name", "Rajkumar SM");               
    }
     public ExtentHtmlReporter getHtmlReporter()
    {
        reporter= new ExtentHtmlReporter(System.getProperty("user.dir") +"\\test-output/STMExtentReport.html");
        reporter.config().setChartVisibilityOnOpen(true);
        reporter.config().setDocumentTitle("QA Automation Report");
        reporter.config().setReportName("Regression Testing");
        reporter.config().setTheme(Theme.STANDARD);
        return reporter;
    }
     @Test
    public void passTest(){
         logger= extent.createTest("Pass Test", "Login");
    //test steps.
     }
     @Test
    public void failTest(){
    logger = extent.createTest("failTest", "Login");
     //test steps.
     }

     //other steps

     @AfterTest
    public void endReport(){
    extent.flush();
       }
}  

Please refresh your project after run the test.
For the more information about report please check Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.