Giter Site home page Giter Site logo

Trying to generate extent report by putting the listener at suite level but the report only displays the last test case ran in the report about extentreports-java HOT 3 CLOSED

DipikaMishra13 avatar DipikaMishra13 commented on August 15, 2024
Trying to generate extent report by putting the listener at suite level but the report only displays the last test case ran in the report

from extentreports-java.

Comments (3)

DipikaMishra13 avatar DipikaMishra13 commented on August 15, 2024

package CustomTestListener;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**

  • Created by ID850378 on 17/07/2017.
    */
    public class CustomTestListener implements ITestListener {

    private static String dateTimeFormat = "yyyy-MM-dd_kk-mm";
    private static String executionReportsHome = "target";
    private static String FILE_NAME = "Report_%s.html";
    private static ExtentReports extent;
    private static ExtentHtmlReporter htmlReporter;
    private static ExtentTest currentTest;

    private String getDateString(){
    Date now = Calendar.getInstance().getTime();
    SimpleDateFormat format = new SimpleDateFormat(dateTimeFormat);
    return format.format(now);
    }

    public void onStart(ITestContext iTestContext) {

     //	making sure paths are in place
     FILE_NAME = String.format(FILE_NAME, "Dipika");
    
     //	configuring the extent reporter
    
     htmlReporter = new ExtentHtmlReporter(executionReportsHome+"/"+FILE_NAME);
    
    
     htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
     htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
     htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
     htmlReporter.config().setChartVisibilityOnOpen(true);
     htmlReporter.config().setTheme(Theme.STANDARD);
    
    
     extent = new ExtentReports();
     extent.attachReporter(htmlReporter);
     extent.setReportUsesManualConfiguration(true);
    

    }

    private void assignCategories(ITestResult iTestResult){

     ArrayList<String> labels = new ArrayList<String>();
    String className = iTestResult.getTestClass().getRealClass().getName();
     //String methodName = iTestResult.getMethod().getMethodName();
     String[] groupName = iTestResult.getMethod().getGroups();
    
     //labels.addAll(Arrays.asList(className.split("\\.")));
     labels.addAll(Arrays.asList(className));
     labels.addAll(Arrays.asList(groupName));
    

    //labels.addAll(Arrays.asList(methodName.split("\.")));

     for (String label : labels) {
         currentTest.assignCategory(label.toLowerCase());
         //currentTest.assignAuthor(label.toLowerCase());
    
     }
    

    }

@BeforeSuite
public void onTestStart(ITestResult iTestResult) {

    try {
        String testName = iTestResult.getMethod().getMethodName();
        String[] test = iTestResult.getMethod().getGroups();
        currentTest = extent.createTest(testName);

        assignCategories(iTestResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void onTestSuccess(ITestResult iTestResult) {
    currentTest.pass("<strong>TEST PASSED!!!!!!!</strong>");
    currentTest.getClass().getPackage();
    currentTest.getModel().getDescription();
}

public void onTestFailure(ITestResult iTestResult) {

    try {
        String testName = iTestResult.getMethod().getMethodName();
        String screenshotName = testName+".jpg";
        if(null != iTestResult.getThrowable()){
            currentTest.fail( iTestResult.getThrowable().getMessage() );
            //currentTest.fail(iTestResult.getThrowable().getStackTrace().toString());
            currentTest.fail( iTestResult.toString());
            Throwable exception = iTestResult.getThrowable();
            if(exception!=null)
            {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                exception.printStackTrace(pw);
                currentTest.fail(sw.toString());
            }

        }else {
            currentTest.fail("Could not get cause!!!");
        }
        currentTest.addScreenCaptureFromPath(screenshotName);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void onTestSkipped(ITestResult iTestResult) {
    try {
        if(null == iTestResult.getThrowable()){
            currentTest.skip("<strong>TEST SKIPPED!!!!!!!</strong>");
        }else {
            currentTest.skip(iTestResult.getThrowable().getMessage());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    try {
        onTestFailure(iTestResult);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@AfterSuite
public void onFinish(ITestContext iTestContext) {
try {
htmlReporter.flush();
htmlReporter.stop();
extent.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}

from extentreports-java.

DipikaMishra13 avatar DipikaMishra13 commented on August 15, 2024

from extentreports-java.

foursyth avatar foursyth commented on August 15, 2024

Did you see the examples section already for ITestListener?

http://extentreports.com/docs/versions/3/java/

It should work as-is and you can customize it to output the info you have in the code

from extentreports-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.