javareportextent

Extent Reports 4 - Java


Trying to generate one extent report, that will allow me to run a suite of test classes and give output as one report.

My current code below runs fin, it will run my testNG.xml file and successfully run all test classes in my suite. THe extent report itself however, only seems to save the last test cases run to it. I can't for the life of me figure out how to stop it overwriting, and append to it instead. Older version of extent report uses htmlreporter.setAppendExisting(true); but that does not exist in extent 4..

public class ExtentManager {
    ExtentHtmlReporter htmlReporter;
    ExtentReports extent;
    ExtentTest parentTest;
    ExtentTest childTest;
    DriverManager driverManager;
    WebDriver driver;
    Screenshot screenshot;
    Properties config;
    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());

    /** Suite test as it will run, with Before/After instructions*/
    @BeforeTest
    public void beforeTest() throws IOException {
        /**Create extent reports and configure html report*/
        htmlReporter = new ExtentHtmlReporter(".//reports/ExtentReport"+timeStamp+".html");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        htmlReporter.config().setTheme(Theme.DARK);
        htmlReporter.config().setDocumentTitle("Automation Testing");
        htmlReporter.config().setReportName("My Report");
        htmlReporter.config().setAutoCreateRelativePathMedia(true);
        htmlReporter.config().setTimeStampFormat("HH:mm:ss");
        screenshot = new Screenshot();
        config = new Properties();
        FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "/src/main/resources/Config.properties");
        config.load(fis);
    }

    public ExtentReports getExtent(){
        if(extent != null){
            return this.extent;
        } else {
            return new ExtentReports();
        }
    }

    @BeforeClass
    public void beforeClass() {
        /**Setup parent test, all child tests to follow
         *will attach to it for html report*/
        extent = getExtent();
        parentTest = extent.createTest(getClass().getSimpleName());
        driverManager = new DriverManager();
        driver = driverManager.getWebDriver();
        driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void getResult(ITestResult result) throws IOException {
        if (result.getStatus() == ITestResult.SUCCESS){
            childTest.log(Status.PASS, MarkupHelper.createLabel("Test Case: " + result.getName() + " PASSED", ExtentColor.GREEN));
        }else if(result.getStatus() == ITestResult.FAILURE){
            String screenshotPath = screenshot.getScreenshot(driver, result.getName());
            childTest.log(Status.FAIL, MarkupHelper.createLabel("Test Case: " + result.getName() + " FAILED", ExtentColor.RED));
            childTest.fail(result.getThrowable().getMessage());
            childTest.log(Status.WARNING, "Error screenshot captured and stored @ " + screenshotPath);
            childTest.addScreenCaptureFromPath(screenshotPath);
        }else if(result.getStatus() == ITestResult.SKIP){
            childTest.log(Status.SKIP, MarkupHelper.createLabel("Test Case: " + result.getName() + " SKIPPED", ExtentColor.AMBER));
            childTest.log(Status.SKIP, "Skipped test: " + result.getName());
        }
    }

    @AfterTest
    public void afterTest(){
        driverManager.quitWebDriver();
    }

    @AfterSuite
    public void afterSuite() {
        extent.flush();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="sanity_tests" parallel="false" verbose="10">
    <test name="test1" parallel="false" preserve-order="true" verbose="10">
        <classes>
            <class name="com.testFramework.Tests.test1"/>
        </classes>
    </test>
    <test name="test2" parallel="false" preserve-order="true" verbose="10">
        <classes>
            <class name="com.testFramework.Tests.test2"/>
        </classes>
    </test>

</suite>

Solution

  • you can use the Extent report adapters and with that there is no need to write any code, check the below article out. In your case- you will need to use the TestNG adapter-

    Try this: https://www.linkedin.com/pulse/extent-framework-adapters-anshoo-arora/`