Home > front end >  Extent Report not being generated
Extent Report not being generated

Time:09-05

I tried generating an extent report with a listener, extentreportgen, base class, and the main test class but it isn't being generated. I don't know what's wrong, I would appreciate the help.The first is my main test class, the second is my listeners class, the third is my Base class, and the last one is my extent report gen class.

Log in main test code


import java.io.IOException;

import org.testng.annotations.Test;

import extentListners.Listeners;
import pages.loginPage;

public class LoginTest extends Listeners{
    
    @Test
    public void LogInTest() throws IOException {
    driver = initialiseDriver();
    loginPage log = new loginPage(driver);

    log.user("[email protected]");
    log.pass("Cust@123");
    log.loginbt();
    log.logoutbt();
    driver.quit();
    }
}

Listeners class


import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import resources.BaseClass;

public class Listeners extends BaseClass implements ITestListener{
    ExtentReports extent = ExtentReportGen.ExtentReportGenerator();
    
    public static ExtentTest test;
    
    @Override
    public void onTestStart(ITestResult result) {
        System.out.println("Test case :" result.getName());
        test = extent.createTest(result.getName());
    }

    public void onTestSuccess(ITestResult result) {
        System.out.println("Test case passed :" result.getName());
        test.log(Status.PASS, "Test case passed");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println("Test case failed :" result.getName());

        test.fail(result.getThrowable());
    }

    @Override
    public void onTestSkipped(ITestResult result) {

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

    }

    @Override
    public void onTestFailedWithTimeout(ITestResult result) {

    }

    @Override
    public void onStart(ITestContext context) {
        System.out.println("Execution started :" context.getName());
    }

    @Override
    public void onFinish(ITestContext context) {
        System.out.println("Execution finished :" context.getName());
        extent.flush();
    }
}

BaseClass


import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BaseClass {
    public WebDriver driver;
    public ExtentReports extent = new ExtentReports();
    public ExtentSparkReporter spark;
    public ExtentTest test;
    
    @BeforeTest
    public WebDriver initialiseDriver() throws IOException {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("C:\\Users\\thora\\eclipse-workspace\\Biskane_NEW\\src\\main\\java\\resources\\data.properties");
        prop.load(fis);
        String browsername = prop.getProperty("browser");
        
        switch(browsername){ 
        case "chrome":
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
            driver.get("https://qa.biskane.com/login");
            driver.manage().window().maximize();
            break;
        case "firefox":
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
            driver.get("https://qa.biskane.com/login");
            driver.manage().window().maximize();
            break;
        case "edge":
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();
            driver.get("https://qa.biskane.com/login");
            driver.manage().window().maximize();
            break;
        default:
            System.out.println("browser: "   browsername   " is invalid, launching FireFox browser as browser of choice..");
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
            driver.get("https://qa.biskane.com/login");
            driver.manage().window().maximize();
            break;
        }
        return driver;
    }
}

ExtentReportGen class


import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class ExtentReportGen {

    static ExtentReports extent ;
    
    public static ExtentReports ExtentReportGenerator() {
        String path = System.getProperty("user.dir") "//ExtentReports//Biskanereport.html";

        ExtentHtmlReporter reporter = new ExtentHtmlReporter(path);


        reporter.config().setReportName("BiskaneTestReport");

        reporter.config().setTheme(Theme.STANDARD);

        extent = new ExtentReports();

        extent.attachReporter(reporter);

        extent.setSystemInfo("OS", "Windows");

        extent.setSystemInfo("Organization name", "Appzlogic");

        extent.setSystemInfo("Executed by", "...);


        return extent;
    }

}

I also have another problem where when I run the log in test class it opens 2 chrome windows, can you guys help me solve that?

CodePudding user response:

Please try the below codes ,

   package com.auto.framework;
    
    import com.relevantcodes.extentreports.ExtentReports;
    import com.relevantcodes.extentreports.ExtentTest;
    import com.relevantcodes.extentreports.LogStatus;
    
    public class SampleReport {
    
        public static ExtentReports report;
        public static ThreadLocal<ExtentTest> test1 = new ThreadLocal<ExtentTest>();
    
        public static void startReporting() {
    
            report = new ExtentReports(
                    System.getProperty("user.dir")   "\\test-output\\extentReport\\ExtentReportResults.html");
    
        }
    
        public static void startTest(String testcaseName) {
            test1.set(report.startTest(testcaseName));
    
        }
    
        public static void endTest() {
            report.endTest(test1.get());
        }
    
        public static void endReporting() {
            report.flush();
        }
    
        public static void log(LogStatus logStatus, String msg) {
    
            test1.get().log(logStatus, msg);

        }
    
    }



Listeners :

    import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import resources.BaseClass;

public class Listeners extends BaseClass implements ITestListener{
   
    
    @Override
    public void onTestStart(ITestResult result) {
        System.out.println("Test case :" result.getName());
       SampleReport.startReporting();
    SampleReport.startTest(method.getName());
    }

    public void onTestSuccess(ITestResult result) {
        System.out.println("Test case passed :" result.getName());
       SampleReport.log(LogStatus.PASS,"Test case passed");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println("Test case failed :" result.getName());

       SampleReport.log(LogStatus.FAIL,"Test case failed ");
    }

    @Override
    public void onTestSkipped(ITestResult result) {

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

    }

    @Override
    public void onTestFailedWithTimeout(ITestResult result) {

    }

    @Override
    public void onStart(ITestContext context) {
        System.out.println("Execution started :" context.getName());
    }

    @Override
    public void onFinish(ITestContext context) {
        System.out.println("Execution finished :" context.getName());
       SampleReport.endTest();
    SampleReport.endReporting();
    }
}

CodePudding user response:

Have you tried debugging the test and see if the methods in the Listeners class has been called and if the those methods are executed or not?

  • Related