Bird
Raised Fist0
Selenium Javatesting~15 mins

Extent Reports setup in Selenium Java - Build an Automation Script

Choose your learning style10 modes available
Setup Extent Reports for Selenium Java Test
Preconditions (3)
Step 1: Create a new ExtentReports instance in the test setup method
Step 2: Attach an ExtentHtmlReporter with a specified report file path
Step 3: Create an ExtentTest object for the test case
Step 4: Log test steps and statuses using the ExtentTest object
Step 5: Flush the ExtentReports instance after test execution to write the report
✅ Expected Result: An HTML report file is generated after test execution showing the logged test steps and their statuses
Automation Requirements - Selenium with TestNG
Assertions Needed:
Verify that the ExtentReports instance is not null after initialization
Verify that the report file is created after flushing the report
Best Practices:
Use @BeforeTest and @AfterTest annotations for setup and teardown
Use explicit waits for Selenium interactions
Use Page Object Model for element locators (if applicable)
Use try-catch blocks to log failures in ExtentReports
Flush the report in @AfterTest to ensure report writing
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

import java.io.File;

public class ExtentReportsSetupTest {
    WebDriver driver;
    ExtentReports extent;
    ExtentTest test;

    @BeforeTest
    public void setUp() {
        // Setup ChromeDriver (assumes chromedriver is in system PATH)
        driver = new ChromeDriver();

        // Initialize ExtentReports and attach reporter
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReport.html");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        // Verify ExtentReports instance is created
        Assert.assertNotNull(extent, "ExtentReports instance should not be null");
    }

    @Test
    public void sampleTest() {
        test = extent.createTest("Sample Test", "Verify Google homepage title");

        try {
            driver.get("https://www.google.com");
            test.info("Navigated to Google homepage");

            String title = driver.getTitle();
            test.info("Page title is: " + title);

            Assert.assertEquals(title, "Google");
            test.pass("Title matched expected value");
        } catch (Exception e) {
            test.fail("Test failed with exception: " + e.getMessage());
            Assert.fail(e.getMessage());
        }
    }

    @AfterTest
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }

        if (extent != null) {
            extent.flush();

            // Verify report file is created
            File reportFile = new File("extentReport.html");
            Assert.assertTrue(reportFile.exists(), "Extent report file should exist after flush");
        }
    }
}

This test class sets up Extent Reports for Selenium tests using TestNG.

@BeforeTest: Initializes the ChromeDriver and ExtentReports with an HTML reporter. It asserts that the ExtentReports instance is not null to confirm setup.

@Test: Creates an ExtentTest to log steps. It navigates to Google, logs info messages, asserts the page title, and logs pass or fail accordingly.

@AfterTest: Quits the browser and flushes the ExtentReports to write the report file. It asserts that the report file exists to confirm report generation.

This structure ensures the report captures test steps and results clearly, and the assertions verify the setup and output.

Common Mistakes - 4 Pitfalls
Not calling extent.flush() after tests
Creating ExtentReports instance inside each test method
Not logging test steps or statuses to ExtentTest
Hardcoding absolute file paths for the report
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify their page titles using Extent Reports

Show Hint

Practice

(1/5)
1. What is the primary purpose of using ExtentHtmlReporter in Selenium Java testing?
easy
A. To execute Selenium WebDriver tests
B. To locate web elements on a page
C. To generate an HTML report for test execution results
D. To manage browser windows during tests

Solution

  1. Step 1: Understand the role of ExtentHtmlReporter

    ExtentHtmlReporter is used to create a visual HTML report that shows test results clearly.
  2. Step 2: Differentiate from other components

    It does not run tests or interact with browsers; it only formats and outputs reports.
  3. Final Answer:

    To generate an HTML report for test execution results -> Option C
  4. Quick Check:

    ExtentHtmlReporter = HTML report generator [OK]
Hint: ExtentHtmlReporter creates HTML reports, not test execution [OK]
Common Mistakes:
  • Confusing ExtentHtmlReporter with WebDriver
  • Thinking it runs tests
  • Assuming it manages browser actions
2. Which of the following is the correct way to initialize ExtentReports with ExtentHtmlReporter in Java?
easy
A. ExtentReports extent = new ExtentReports(); extent.createReporter("report.html");
B. ExtentHtmlReporter reporter = new ExtentHtmlReporter("report.html"); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter);
C. ExtentHtmlReporter reporter = new ExtentHtmlReporter(); extent.attachReporter(reporter);
D. ExtentReports extent = new ExtentReports("report.html"); ExtentHtmlReporter reporter = new ExtentHtmlReporter();

Solution

  1. Step 1: Create ExtentHtmlReporter with file path

    You must instantiate ExtentHtmlReporter with the report file name or path.
  2. Step 2: Attach reporter to ExtentReports instance

    Create ExtentReports object and attach the reporter using attachReporter method.
  3. Final Answer:

    ExtentHtmlReporter reporter = new ExtentHtmlReporter("report.html"); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); -> Option B
  4. Quick Check:

    Initialize reporter with path, then attach to ExtentReports [OK]
Hint: Create reporter with path, then attach to ExtentReports [OK]
Common Mistakes:
  • Not passing file path to ExtentHtmlReporter constructor
  • Calling attachReporter before creating ExtentReports
  • Using non-existent methods like createReporter
3. Given the following code snippet, what will be the output in the Extent Report after execution?
ExtentHtmlReporter reporter = new ExtentHtmlReporter("testReport.html");
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
ExtentTest test = extent.createTest("LoginTest");
test.pass("Login successful");
extent.flush();
medium
A. The report will show a test named 'LoginTest' marked as failed.
B. The report will be empty because flush() is called before creating the test.
C. The code will throw a runtime exception due to missing test initialization.
D. The report will show a test named 'LoginTest' marked as passed with message 'Login successful'.

Solution

  1. Step 1: Analyze test creation and logging

    The test 'LoginTest' is created and logged as passed with message 'Login successful'.
  2. Step 2: Understand flush() role

    Calling extent.flush() writes the report to the file including all logged tests.
  3. Final Answer:

    The report will show a test named 'LoginTest' marked as passed with message 'Login successful'. -> Option D
  4. Quick Check:

    createTest + pass + flush = Passed test in report [OK]
Hint: Create test, log pass, then flush to save report [OK]
Common Mistakes:
  • Thinking flush() clears tests
  • Assuming test is failed without fail() call
  • Believing report is empty without flush()
4. Identify the error in the following Extent Reports setup code:
ExtentHtmlReporter reporter = new ExtentHtmlReporter();
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
ExtentTest test = extent.createTest("SearchTest");
test.pass("Search executed");
extent.flush();
medium
A. ExtentHtmlReporter must be initialized with a file path in its constructor.
B. ExtentReports cannot attach more than one reporter.
C. createTest() method requires two parameters.
D. flush() should be called before creating tests.

Solution

  1. Step 1: Check ExtentHtmlReporter initialization

    ExtentHtmlReporter requires a file path string in its constructor to specify report output location.
  2. Step 2: Verify other method usages

    attachReporter, createTest, and flush are used correctly; no multiple reporter restriction or parameter issues.
  3. Final Answer:

    ExtentHtmlReporter must be initialized with a file path in its constructor. -> Option A
  4. Quick Check:

    Reporter needs file path on creation [OK]
Hint: Always give file path when creating ExtentHtmlReporter [OK]
Common Mistakes:
  • Omitting file path in ExtentHtmlReporter constructor
  • Misunderstanding createTest parameters
  • Calling flush too early
5. You want to create an Extent Report that logs multiple tests and marks one as failed with a custom error message. Which code snippet correctly sets up the report and logs these results?
hard
A. ExtentHtmlReporter reporter = new ExtentHtmlReporter("multiTestReport.html"); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); ExtentTest test1 = extent.createTest("Test1"); test1.pass("Test1 passed"); ExtentTest test2 = extent.createTest("Test2"); test2.fail("Test2 failed due to timeout"); extent.flush();
B. ExtentReports extent = new ExtentReports("multiTestReport.html"); ExtentHtmlReporter reporter = new ExtentHtmlReporter(); extent.createTest("Test1").pass("Test1 passed"); extent.createTest("Test2").fail("Test2 failed due to timeout"); extent.flush();
C. ExtentHtmlReporter reporter = new ExtentHtmlReporter(); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); extent.createTest("Test1").pass("Test1 passed"); extent.createTest("Test2").fail("Test2 failed due to timeout"); extent.flush();
D. ExtentHtmlReporter reporter = new ExtentHtmlReporter(); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); ExtentTest test1 = extent.createTest("Test1"); test1.fail("Test1 failed"); ExtentTest test2 = extent.createTest("Test2"); test2.pass("Test2 passed"); extent.flush();

Solution

  1. Step 1: Initialize ExtentHtmlReporter with file path

    Instantiate ExtentHtmlReporter with the path to the report file, create an ExtentReports instance, and attach the reporter.
  2. Step 2: Create multiple tests and log statuses

    Create ExtentTest instances with createTest(), log one with pass("message") and one with fail("custom error message"), then flush().
  3. Final Answer:

    ExtentHtmlReporter reporter = new ExtentHtmlReporter("multiTestReport.html"); ExtentReports extent = new ExtentReports(); extent.attachReporter(reporter); ExtentTest test1 = extent.createTest("Test1"); test1.pass("Test1 passed"); ExtentTest test2 = extent.createTest("Test2"); test2.fail("Test2 failed due to timeout"); extent.flush(); -> Option A
  4. Quick Check:

    Reporter with path + attach + pass/fail + flush [OK]
Hint: Attach reporter with path, create tests, log pass/fail, then flush [OK]
Common Mistakes:
  • Not passing file path to ExtentHtmlReporter
  • Swapping pass and fail calls
  • Calling flush before logging tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard