Extent Report Not Generating

72 Views Asked by At

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.

My previous script was all written in one fie and everything worked fine. I am trying to use the POM for creating an automation framework and for some reason it is not generating a new report after deleting the one that was previously created and working. Some of the items in the parenthesis are changed from the original for security purposes, so ignore those.

Preivous Script:

public class FirstTest {
    
    WebDriver driver;
    ExtentReports extent = new ExtentReports();
    ExtentSparkReporter spark = new ExtentSparkReporter("target/results.html");
    ExtentTest test1, test2;
    
    @BeforeSuite
    public void setUpTest() {
        
         driver = new EdgeDriver();
         driver.manage().window().maximize();
         spark.config().setTheme(Theme.DARK);
         spark.config().setDocumentTitle("Selenium Report");
         extent.attachReporter(spark);
         
    }

    @Test
    public void Test() {
         test1 = extent.createTest("Login to SMB").assignAuthor("Jacob").
                assignCategory("Functional Test Case").assignDevice("Windows X64");
                
        try {
            test1.info("Get Login Page");
            driver.get("randomwebsite.com");
            
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
            
            test1.info("Find Email Text-box and input email");
            WebElement username = driver.findElement(By.cssSelector("#signin-email"));
            username.click();
            username.sendKeys("fakeUser");
            
            test1.info("Find Password textbox and input password.");
            WebElement password = driver.findElement(By.cssSelector("#signin-password"));
            password.click();
            password.sendKeys("password");
            
            test1.info("Clicking Login button");
            WebElement button = driver.findElement(By.cssSelector("#btn-login"));
            button.click();
            
            test1.log(Status.PASS, "Successfully logged in");
            
        } catch (Exception ex){
            test1.log(Status.FAIL, ex);
            throw ex;
        }
        
    }
    
    @Test
    public void Test2() {
         test2 = extent.createTest("Navigate to My Invoices Page").assignAuthor("Jacob").
                assignCategory("Functional Test Case").assignDevice("Windows X64");
         
         try {
             test2.info("Find Invoice dropdown menu");
             WebElement invoiceButton = driver.findElement(By.cssSelector("#outlined/archive"));
             invoiceButton.click();
             
             test2.info("Select My Invoices in the dropdown");
             WebElement dropdown = driver.findElement(By.id("dropdown"));
             dropdown.click();
             
             test2.log(Status.PASS, "Sucessfully loaded the Page");
             
         } catch (Exception ex) {
             test2.log(Status.FAIL, ex);
             throw ex;
         };
                
    } 
    
    @AfterSuite 
    public void EndTest() {
        extent.flush();
        driver.quit();
        
    }

}
1

There are 1 best solutions below

0
Jacob Ehrler On

I posted my question on the https://github.com/extent-framework/extentreports-java/issues page, and someone mentioned that I was overwriting my Extent Report instance here:

   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();
        
    }

So I made a few changes and mirrored what was on the https://www.extentreports.com/docs/versions/5/java/spark-reporter.html page. My code is now working as expected and the Extent Report is now generating at a suite level. I changed my BaseTest class to this:

    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());
    }

Hopefully this can help someone, too. Thanks.