0
0
Selenium Javatesting~15 mins

Extent Reports setup in Selenium Java - Deep Dive

Choose your learning style9 modes available
Overview - Extent Reports setup
What is it?
Extent Reports is a tool that helps testers create clear and attractive reports for automated tests. It collects test results and shows them in a web page with colors and details. Setting it up means adding it to your Selenium Java project so you can see which tests passed or failed easily.
Why it matters
Without Extent Reports, test results are often just plain text or hard-to-read logs. This makes it difficult to understand what happened during testing, especially for teams or managers. Extent Reports solves this by giving a visual summary that saves time and reduces confusion.
Where it fits
Before setting up Extent Reports, you should know basic Selenium WebDriver and Java programming. After setup, you can learn how to customize reports, add screenshots, and integrate with test frameworks like TestNG or JUnit.
Mental Model
Core Idea
Extent Reports transforms raw test results into easy-to-understand, colorful web reports that show test status and details.
Think of it like...
It's like turning a messy notebook of test notes into a neat, colorful presentation slide deck that everyone can quickly understand.
┌───────────────────────────────┐
│       Test Execution           │
│  (Selenium Java Tests Run)    │
└──────────────┬────────────────┘
               │
               ▼
┌───────────────────────────────┐
│      Extent Reports Setup      │
│  (Initialize, Configure, Log) │
└──────────────┬────────────────┘
               │
               ▼
┌───────────────────────────────┐
│      HTML Report Generated     │
│  (Pass/Fail, Details, Colors)  │
└───────────────────────────────┘
Build-Up - 7 Steps
1
FoundationAdd Extent Reports dependency
🤔
Concept: Learn how to include Extent Reports library in your Selenium Java project.
To use Extent Reports, first add its dependency to your project. If you use Maven, add this to your pom.xml: com.aventstack extentreports 5.0.9 This downloads the Extent Reports library so you can use it in your code.
Result
Your project now has Extent Reports available to import and use.
Knowing how to add dependencies is the first step to using any external tool in your project.
2
FoundationInitialize Extent Reports objects
🤔
Concept: Create the main Extent Reports objects needed to start logging test results.
In your test code, create these objects: ExtentReports extent = new ExtentReports(); ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReport.html"); extent.attachReporter(htmlReporter); The ExtentHtmlReporter sets the output file. The ExtentReports object manages the report.
Result
You have a report object ready to log test steps and results.
Initialization sets the foundation for capturing and saving test information.
3
IntermediateCreate and log test cases
🤔Before reading on: do you think you log test steps before or after running the test? Commit to your answer.
Concept: Learn how to create test entries and log pass/fail status during test execution.
Inside your test method, create a test entry: ExtentTest test = extent.createTest("Test Case Name"); Then log steps: test.pass("Step passed message"); test.fail("Step failed message"); Use these to mark what happened during the test.
Result
Test steps and results are recorded in the report object.
Logging test steps as they happen helps build a detailed and useful report.
4
IntermediateFlush and generate report file
🤔Before reading on: do you think the report file updates automatically or needs a manual save? Commit to your answer.
Concept: Understand how to save the collected test data into the final report file.
After all tests finish, call: extent.flush(); This writes all logged information to the HTML file you specified earlier. Without this, the report file stays empty.
Result
An HTML report file is created or updated with test results.
Knowing when to flush prevents missing or incomplete reports.
5
IntermediateIntegrate Extent Reports with TestNG
🤔Before reading on: do you think Extent Reports setup goes inside each test or in test lifecycle methods? Commit to your answer.
Concept: Learn to set up Extent Reports in TestNG's lifecycle methods for better management.
Use TestNG annotations: @BeforeSuite public void setup() { // initialize ExtentReports } @AfterSuite public void tearDown() { extent.flush(); } @BeforeMethod public void startTest(Method method) { test = extent.createTest(method.getName()); } This organizes report creation and flushing properly.
Result
Reports are created and saved automatically with TestNG tests.
Using test framework hooks ensures reports are consistent and complete.
6
AdvancedAdd screenshots to reports
🤔Before reading on: do you think screenshots are added before or after test failure? Commit to your answer.
Concept: Learn how to capture and attach screenshots to Extent Reports on test failure.
On test failure, capture screenshot: File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); String path = "screenshots/" + System.currentTimeMillis() + ".png"; FileUtils.copyFile(src, new File(path)); Attach to report: test.fail("Test failed", MediaEntityBuilder.createScreenCaptureFromPath(path).build()); This shows exactly what went wrong visually.
Result
Reports include screenshots for failed tests, improving debugging.
Visual evidence in reports speeds up problem solving and communication.
7
ExpertCustomize report look and behavior
🤔Before reading on: do you think Extent Reports styling is fixed or customizable? Commit to your answer.
Concept: Explore advanced configuration options to change report theme, title, and system info.
Use htmlReporter.config(): htmlReporter.config().setDocumentTitle("My Test Report"); htmlReporter.config().setReportName("Regression Tests"); htmlReporter.config().setTheme(Theme.DARK); Add system info: extent.setSystemInfo("OS", "Windows 10"); extent.setSystemInfo("Tester", "Alice"); These make reports clearer and branded.
Result
Reports look professional and provide useful context.
Customizing reports improves readability and stakeholder trust.
Under the Hood
Extent Reports works by creating objects in memory that collect test information as tests run. It stores test names, statuses, logs, and attachments. When you call flush(), it converts this data into an HTML file with embedded CSS and JavaScript for interactivity. The report file is static but visually rich, showing colors and expandable details.
Why designed this way?
It was designed to separate test execution from reporting, so tests run fast without waiting for file writes. The flush step batches all data into one file for performance and simplicity. Using HTML allows easy sharing and viewing without special software.
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Test Execution│──────▶│ ExtentReports │──────▶│ HTML Report   │
│ (Selenium)    │       │ (In-memory)   │       │ (File Output) │
└───────────────┘       └───────────────┘       └───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does Extent Reports automatically save the report file after each test? Commit yes or no.
Common Belief:Extent Reports saves the report file automatically after every test step.
Tap to reveal reality
Reality:Extent Reports only writes the report file when you call extent.flush(), usually at the end of all tests.
Why it matters:If you forget to call flush(), the report file will be empty or incomplete, causing confusion and wasted debugging time.
Quick: Can you add screenshots to Extent Reports without saving image files? Commit yes or no.
Common Belief:You can embed screenshots directly without saving image files on disk.
Tap to reveal reality
Reality:Extent Reports requires screenshots to be saved as files first, then referenced in the report.
Why it matters:Trying to embed unsaved screenshots causes errors or missing images in reports, reducing their usefulness.
Quick: Does Extent Reports replace your test framework like TestNG or JUnit? Commit yes or no.
Common Belief:Extent Reports is a test framework that runs tests and reports results.
Tap to reveal reality
Reality:Extent Reports only creates reports; it does not run or manage tests. You still need TestNG, JUnit, or similar.
Why it matters:Confusing these leads to improper test setup and missing test execution control.
Quick: Is it safe to share Extent Reports HTML files publicly without risk? Commit yes or no.
Common Belief:Extent Reports HTML files contain only test results and no sensitive data.
Tap to reveal reality
Reality:Reports may include system info, logs, or screenshots that contain sensitive information.
Why it matters:Sharing reports without review can expose confidential data, causing security risks.
Expert Zone
1
Extent Reports supports parallel test execution but requires thread-safe handling of ExtentTest objects to avoid data mix-ups.
2
You can extend Extent Reports with custom reporters or listeners to integrate with CI/CD pipelines and dashboards.
3
The report's HTML and CSS can be customized deeply by modifying the reporter's config or using custom CSS files.
When NOT to use
Extent Reports is not ideal for very large test suites with thousands of tests due to report size and performance. In such cases, lightweight logging or specialized test management tools are better.
Production Patterns
In real projects, Extent Reports is integrated with TestNG listeners to automatically log test start, success, failure, and skip events. Screenshots are captured on failure and attached. Reports are archived per build and linked in CI tools like Jenkins.
Connections
TestNG Listeners
Builds-on
Understanding TestNG listeners helps automate Extent Reports logging without manual calls in each test.
Continuous Integration (CI)
Used together
Integrating Extent Reports with CI tools like Jenkins provides quick feedback on test results after each code change.
Data Visualization
Shares principles
Extent Reports applies data visualization principles by turning raw test data into clear, interactive visual summaries.
Common Pitfalls
#1Forgetting to call extent.flush() after tests finish.
Wrong approach:public void tearDown() { // no flush call here }
Correct approach:public void tearDown() { extent.flush(); }
Root cause:Misunderstanding that flush() is required to write the report file.
#2Creating ExtentReports and ExtentTest objects inside each test without sharing.
Wrong approach:ExtentReports extent = new ExtentReports(); ExtentTest test = extent.createTest("Test1"); // repeated in every test method
Correct approach:Initialize ExtentReports once in @BeforeSuite and create ExtentTest in @BeforeMethod or test method.
Root cause:Not managing object lifecycle leads to multiple reports or lost data.
#3Not saving screenshots to disk before attaching to report.
Wrong approach:test.fail("Failed", MediaEntityBuilder.createScreenCaptureFromPath("temp.png").build()); // but temp.png does not exist
Correct approach:Save screenshot file first, then attach: File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(src, new File("temp.png")); test.fail("Failed", MediaEntityBuilder.createScreenCaptureFromPath("temp.png").build());
Root cause:Assuming Extent Reports can embed images directly from memory.
Key Takeaways
Extent Reports turns raw test results into clear, colorful HTML reports that improve understanding and communication.
You must add Extent Reports as a dependency and initialize it properly before logging tests.
Logging test steps and calling flush() at the end are essential to generate complete reports.
Integrating Extent Reports with test frameworks like TestNG automates report creation and management.
Adding screenshots and customizing reports makes debugging easier and reports more professional.