extent-framework / extentreports-java

Extent Reporting Library, Java
http://extentreports.com
Apache License 2.0
220 stars 126 forks source link

Extent Report not generating #425

Closed EhrlerJacob closed 7 months ago

EhrlerJacob commented 7 months ago

Here is my Base Test method with all my extent report info.

`public class BaseTest {

// Declare WebDriver and Extent Reports globally

public static WebDriver driver;
ExtentReports extent = new ExtentReports();
ExtentSparkReporter spark = new ExtentSparkReporter("target/results.html");
ExtentTest logger;

// Configure Extent Reporter to the test scripts @BeforeTest public void beforeTestMethod() {

    spark = new ExtentSparkReporter("target/results.html");
    spark.config().setTheme(Theme.DARK);
    spark.config().setDocumentTitle("Selenium Report");
    spark.config().setReportName("Suite Report");
    extent.attachReporter(spark); 
    extent = new ExtentReports();

}

// Set driver to the specific driver stated in the TESTNG test suite & configure window/ wait @BeforeMethod @Parameters("browser") public void beforeMethod(String browser, Method testMethod) { logger = extent.createTest(testMethod.getName()); setupDriver(browser); driver.manage().window().maximize(); driver.get(utils.Constants.url); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); }

public void setupDriver(String browser) {
    if(browser.equalsIgnoreCase("chrome")) {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
    }
    else if(browser.equalsIgnoreCase("firefox")) {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    }
    else if(browser.equalsIgnoreCase("edge")) {
        WebDriverManager.edgedriver().setup();
        driver = new EdgeDriver();
    }

}

// Add a logger for test results @AfterMethod public void afterMethod(ITestResult result) { if(result.getStatus() == ITestResult.FAILURE) { 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, MarkupHelper.createLabel(result.getName() + " - Test Case Skipped", ExtentColor.ORANGE)); } else if(result.getStatus() == ITestResult.SUCCESS) { logger.log(Status.PASS, MarkupHelper.createLabel(result.getName() + " - Test Case Passed", ExtentColor.GREEN));

    }
}

// Results are sent through the extent report with this method @AfterTest public void afterTest() { extent.flush(); driver.quit();

}

} `

I also made a simple listener class

`public class SuiteListener implements ITestListener, IAnnotationTransformer {

// Configure a test failure method to retry the script and send screenshots for the failed test public void onTestFailure(ITestResult result) { String filename = System.getProperty("user.dir")+File.separator+"screenshots"+File.separator+result.getMethod().getMethodName(); File f1 = ((TakesScreenshot)BaseTest.driver).getScreenshotAs(OutputType.FILE);

        try {
            FileUtils.copyFile(f1, new File(filename+ ".png"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
      }

// Configure retry method public void transform( ITestAnnotation annotation, Method testMethod) { annotation.setRetryAnalyzer(RetryAnalyzer.class); }

}`

Am I implementing my listener class incorrectly or do I need to make an ExtentManager/TestManager class like I have seen? My TestNG suite is running fine, and my scripts are also working as expected.

apex-sewilliams commented 7 months ago

it looks like you are overwriting your extend object with

// Configure Extent Reporter to the test scripts
@BeforeTest
public void beforeTestMethod() {
        spark = new ExtentSparkReporter("target/results.html");
    spark.config().setTheme(Theme.DARK);
    spark.config().setDocumentTitle("Selenium Report");
    spark.config().setReportName("Suite Report");
    extent.attachReporter(spark); 
    extent = new ExtentReports();

} 

you go and attach the reporter, and then create a new report and abandon the one with the reporter attached.

EhrlerJacob commented 7 months ago

I think you were correct in me overwriting the initial report. I changed a few things around and they work. The new code looks is as follows: `public class BaseTest {

// Declare WebDriver and Extent Reports globally

public static WebDriver driver;
ExtentReports extent = new ExtentReports();
ExtentSparkReporter spark = new ExtentSparkReporter("target/results.html");
ExtentTest logger;

// Configure Extent Reporter to the test scripts @BeforeTest public void beforeTestMethod() {

    extent.attachReporter(spark); 
    spark.config(
            ExtentSparkReporterConfig.builder()
            .theme(Theme.DARK)
            .documentTitle("Selenium Report")
            .reportName("Suite Report")
            .build());
}

` It is now giving me my report. Thanks for your help!