How To Generate Extent Reports In Selenium

Shalini Baskaran

Posted On: May 19, 2022

view count257962 Views

Read time24 Min Read

Selenium is a great tool when it comes to reporting, as it provides inbuilt reports using various frameworks like TestNG and JUnit. Though a few built-in reports are available, they do not provide information on the steps executed as part of the test suite. Therefore, custom reporting needs to be implemented to make it convenient for all major project stakeholders.

With the advent of custom report functionality that can be used to create reports for additional test-case information, it is even more important for the testers to configure those reports to be helpful for the entire project team.

For someone who is new to the concept of Extent Reports, the idea of generating them may seem strange at first. This step-by-step guide will make it a little easier to understand how to generate Extent Reports in Selenium WebDriver with example codes.

Let’s get started!

What are test reports in automation?

Creating a report for a task makes the task 100% complete. The report not only provides a summary but also helps the person to visualize and summarize the results of the task. A report plays a vital role in test automation as well. A test automation report helps the testers and the other stakeholders understand the stability of the tests executed before the product goes live. This helps the stakeholders to understand the stability of the application and also provides confidence for going live.

An automation test report should be designed in such a way that it depicts the results of the tests to the end-user. A good test report should hold various test statistics like the total number of automation test cases, the total number of test cases passed, the total number of failed test cases, the percentage of passed and failed cases, classification of the tests based on certain criteria, etc.

In recent years, various reporting tools like TestNG Reports, JUnit Reports, Allure Reports, etc., are available for generating the test automation report. Out of those, Extent Reports is one of the best reporting tools for Selenium that is being widely used in various organizations. It has gained immense popularity because of its unique features like report customization, integration with different test frameworks, amazing data visualization, etc. In this blog on Extent Reports in Selenium, we shall be looking at the most popularly used test report – the Extent Reports.

Introduction to Extent Reports

Extent Report is an open-source library used for generating test reports in automation testing. It has been more widely used for report generation than the inbuilt reports in various test frameworks because of its enhanced features and customization. It is a simple yet powerful reporting library that can be integrated with the automation framework for generating the automation test report.

Extent Reports can also be integrated with other popular test automation frameworks like JUnit and TestNG. It provides a detailed outlook on the automated test cases in a graphical way. It is very easy to integrate Extent Reports with the automation framework.

Watch this video to know more about how to generate Extent Reports in Selenium, a popular testing framework for web applications. Also, learn different ways to configure Extent Reports to capture your test results and more.

Advantages of Extent Reports

Let us pen down the advantages of using Extent Reports in Selenium for automation testing.

  • It is an open-source library.
  • It provides a pictorial representation of the test results.
  • It can be customized as required.
  • It allows the users to attach screenshots and logs in the test report for a detailed summary of the tests.
  • It can be easily integrated with other frameworks like JUnit, TestNG, NUnit, etc.
  • It can be easily configured with Jenkins, Bamboo, etc.

Prerequisites to generate Extent Reports

In this blog on how to generate Extent Reports in Selenium, we shall set up and generate our first Extent Report in Selenium for our automation tests implemented with Selenium Java. For more information about Selenium Java, please read our Selenium Java Tutorial. To set up and generate Extent Reports in Selenium upon test execution, let us see the prerequisites to be done.

  1. Java: Download and install Java in the system.
  2. Selenium: For framing our automation test cases, we need to install Selenium by adding a maven dependency in the pom.xml file.
  3. Prerequisites to generate Extent Reports

  4. Extent Reports: To integrate our automation framework, we need the Extent Report dependency either as a maven dependency or a jar file. In the case of a maven project, add maven dependency in the pom.xml file.

    Things for Generating Extent Reports
    You can also download the jar file and add the jar to the build path. You can refer to the mvn repository for adding the Extent Report dependency.

  5. TestNG: To design and execute the tests, we shall use TestNG by adding the TestNG dependency.
    Things for Extent Reports
  6. Now we have set up for automating a web application and generating the Extent Reports in Selenium.

    In the next section of this article on Extent Reports in Selenium, let us understand the terminologies in Extent Report in detail before adding our tests.

How to generate and customize the Extent Reports?

There are three classes that are used for generating and customizing the Extent Reports in Selenium. They are:

  1. ExtentHtmlReporter
  2. ExtentReports
  3. ExtentTest

The ExtentHtmlReporter is used for creating an HTML file, and it accepts a file path as a parameter. The file path represents the path in which our extent report would be generated.

In the above line, we have used ExtentHtmlReporter and specified the file path where the Extent Report in Selenium has to be generated.
In Java, the getProperty(String key) method returns the system property specified by the key, which is passed as the argument. Here we have specified “user.dir” as the key, and System.getProperty(“user.dir”) returns our current working directory. So instead of specifying the full path, we can use this to fetch the value of our directory and add the path where we want our Extent Report in Selenium to be saved.
ExtentHtmlReporter is also used to customize the extent reports. It allows many configurations to be made through the config() method. Some of the configurations that can be made are described below.

  1. To set the title of the extent report, we can use setDocumentTitle(“YOUR_TITLE”).
  2. To set the report name, we can use setReportName(“YOUR_REPORT_NAME”).
  3. We can also specify the time format in our extent report by using the setTimeStampFormat() method.

Now, once the Extent Report in Selenium gets generated, we can visualize all the above changes in the report.

Extent Reports Generation

In addition to these changes, if you are a great fan of dark themes, then Extent Reports in Selenium can also be customized with dark themes using the setTheme() method.

We have two themes – STANDARD and DARK for customizing the look and feel of our extent reports.

Extent Reports Theme

  • For Light Theme, we can set the Theme as STANDARD.
  • Below is the screenshot of the extent report with STANDARD THEME
    Extent Report with STANDARD THEME

  • For the Dark theme, we can set the theme as DARK.
  • Extent Report with Dark THEME

In a nutshell,

  • The ExtentHtmlReporter class is used for creating the HTML reports.
  • The ExtentReports class is used for creating the tests.
  • The ExtentTest class is used for generating the logs in the Extent Report.

So now, we have gone through various configurations that can be made to customize the look of the Extent Reports in Selenium. We can initialize the ExtentHtmlReporter and add the required configurations in the @BeforeTest TestNG annotation.

ExtentHtmlReporter

Now let us see how to write simple test cases and generate our first Extent Report in Selenium. We shall create test cases that end in three different statuses like Pass, Fail and Skip.

To create a test, we can use createTest(test_name,test_description) using ExtentReports class.

  1. We have created a simple test test_1 which ends up in PASS by asserting the condition as true.
  2. We have created test_2, which would fail as we have passed False in the assetTrue condition.
  3. We have created test_3, which would be skipped.

Extent Report Selenium

To get the status of our tests and publish it in our report, we can use the log() method from ExtentTest class.

log() method from ExtentTest class
In the end, we can use the flush()method, which is used for removing any previous data and creating a new Extent Report in Selenium.

flush()method

Code snippet for creating simple tests and generating our first extent report:

Test Execution:
Upon running the tests, our output console would look similar to the screenshot below.
Test Execution

Now the Extent Report in Selenium would be generated and saved in the path provided initially.

Right-click on the Extent Report 🡪 Open in Browser 🡪 Chrome (Browser of your choice).

Extent Report in Selenium path

We have successfully generated our first Extent Report in Selenium..!!!

Here you can see the tests that have been marked with different statuses.

Test with Different Statuses

If you’re a Java developer, the following certification from LambdaTest can help you improve your skills.

This Selenium Java 101 certification is for anyone who wants to upskill their knowledge of the Java programming language and learn more about the Selenium automation testing.

How to generate Extent Reports in Selenium using a cloud grid?

So far, we have written simple test cases to understand the basics of Extent Reports in Selenium. Now let us automate some test cases using Selenium and generate our real automation report. We shall use our cloud Selenium Grid like LambdaTest to execute our tests.

LambdaTest is a cloud-based cross browser testing platform that supports Selenium Grid, which can be leveraged for running our tests in the grid. Test automation platforms like LambdaTest offer an online browser farm of 3000+ online browsers to perform Selenium automation testing effortlessly.

Here’s a glimpse of LambdaTest cloud Selenium Grid:

You can also Subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorials around automated browser testing, Cypress UI automation, CI/CD, and more.

Let us create two classes for our tests, execute them in Cloud Selenium Grid and then finally generate Extent Reports in Selenium for automation.

 
Scenarios:

We shall be using the LambdaTest playground for all our tests, which opens the gate to explore various pages to learn automation.

Test case 1: To verify the title of the webpage
Step 1: Launch https://www.lambdatest.com/selenium-playground/.
Step 2: Get the title of the web page and verify it.

Test case 2: To enter single input and verify the message displayed
Step 1: Launch https://www.lambdatest.com/selenium-playground/.
Step 2: Click Simple Form Demo under the Input Forms Section.
Step 3: Enter the message in the Enter Message text box.
Step 4: Click the Get Checked Value button.
Step 5: Verify the message displayed in the Your Message section.

Test Case- 2 -Enter Single Input

Test case 3: To enter multiple inputs and verify the value displayed
Step 1: Launch https://www.lambdatest.com/selenium-playground/.
Step 2: Click Simple Form Demo under the Input Forms Section.
Step 3: In the two input Fields section, enter the value of a and b.
Step 4: Click the Get Values button.
Step 5: Verify the value displayed in the Total a+b section.

Test Case 3 - Enter Multiple Inputs

Test case 4: Verify the alert message on the web page
Step 1: Launch https://www.lambdatest.com/selenium-playground/.
Step 2: Click Bootstrap Alerts under the Alerts & Modals section.
Step 3: Click Normal Success Message and verify the message displayed.

Test Case 4 -Verify the Alert Message

We understood the scenarios to be automated. Let us split them into two different classes – InputFormTest having the first three scenarios and AlertTests having the last test case.

Before we implement the test cases, let us frame a BaseTest class, which will have the common methods used for initializing and generating Extent Reports in Selenium.

BaseTest class

Code Walkthrough

  1. Initialize the global variables
    In the BaseClass, let us initialize the global variables.
  2. Initialize the Global Variables (1)

  3. Initialize the extent report
    We have created a method initializeReport(), which has the steps to initialize and configure Extent Reports in Selenium.
  4. Initialize the extent report

  5. Include common methods
    In the BaseTest class we can add commonly used methods like capturing screenshots which can be used across various tests.

Let us frame the automation test cases.

Test Class 1 – InputFormTest

Code Walkthrough

  1. Extend BaseTest.
  2. Initialize the global variables like username and access key for running the tests in cloud Selenium Grid.
  3. Initialize the Global Variables- Username & Access Key

  4. @BeforeTest
    Use the initializeReport() method from BaseTest class for initializing the Extent Report set up.
  5. initializeReport() method from BaseTest

  6. @BeforeMethod
    Define the desired capabilities like browser, browser version, operating system and its version, build and test name for running our tests in cloud Selenium Grid like LambdaTest. In addition to this, we need to specify the grid URL for the tests to be executed.
  7. BeforeMethod

  8. @Test
    Test annotations must be defined at the start of every test method. If the @Test annotation is not found on a given test method, then the method will not be executed as a part of the test code.
  9. Let us implement our test cases.
    Test case 1: To verify the title of the webpage

    To get the title of the web page, we can use driver.getTitle(). Then we can use TestNG assertions to compare the actual and expected title on the web page.
    TestNG assertions
    The ExtentTest class provides a method to segregate our tests into various categories.

    Let us assign this test case as P0 category.
    ExtentTest class
    Test 2: SingleInputTest – This is to verify the form submission and verification of the message in the form

    Upon launching the browser and navigating to the website, we have to click the Simple Input Form. For this, we have first to inspect the Simple Form Demo link and then perform a click.

    Form Demo

    We will be directed to the form page, where we have to provide some value in the input form. We shall inspect the text box web element and enter the value by using the sendkeys(your_message) method. You can go through this article on sendKeys in Selenium to know more about sendKeys.

    Hello World

    After entering the value in the form, we have to click the ‘Get Checked value’ button to display our message, which was entered in the form.

    Simple Form Demo
    Now we have to fetch the message displayed.
    fetch the message displayed (1)
    Using assertions let us verify if the actual and expected messages are the same.
    Actual and expected messages (1)

    Test 3: MultipleInputTest – This test is to verify if the user is able to enter two input fields in the form.
    First we need to click the Simple Form Demo option.

    Simple Form Demo option
    This navigates us to the form page and here we have to enter two values in the form for which first we have to identify the web elements.
    Form Page

    Upon submitting the form, the sum of the values would be displayed and we have to verify the displayed value.
    Upon submitting the form

    Upon submitting form

  10. @AfterMethod
    Here we will add a step to quit the browser
  11. AfterMethod

  12. @AfterTest
    Here we shall flush the report
  13. AfterTest Method
    Next, we will see AlertTests, where we have added one test which verifies the alert message displayed on the page.

Test Class 2 – AlertTests

Code Walkthrough

  1. Extend BaseTest.
  2. Initialize the global variables like username and access key for running the tests in cloud Selenium Grid.
  3. Initialize the global variables like username and access key for running the tests in cloud Selenium Grid.

  4. @BeforeTest
    Use the initializeReport() method from BaseTest class for initializing the Extent Report set up.
  5. initializeReport

  6. @BeforeMethod
    Define the desired capabilities like browser, browser version, operating system, and its version, build and test name for running our tests in cloud Selenium Grid like LambdaTest. In addition to this, we need to specify the grid URL for the tests to be executed.
  7. BeforeMethod

  8. @Test
    Test annotations must be defined at the start of every test method. If the @Test annotation is not found on a given test method, then the method will not be executed as a part of the test code.

Let us implement our test case.
implement our test case

Once the website has been launched, the Bootstrap Alert webElement has to be identified and clicked.

the Bootstrap Alert webElement

This directs us to the alerts page where we should verify the alert message popped up upon clicking the Normal Success Message.

Normal Success Message

Listener class

TestNG provides the @Listeners annotation, which listens to every event that occurs in a Selenium code. TestNG Listeners are activated either before the test or after the test case. It is an interface that modifies the TestNG behavior. If any event matches an event for which we want the listener to listen then it executes the code, which ultimately results in modifying the default behavior of TestNG.

If you want to know more about Event Listeners In Selenium WebDriver watch this video to learn how the Listeners “listen” to the event defined in the Selenium script and behave accordingly.

There are various types of TestNG listeners out of which we shall be using ITestListener in our tests. The ITestListener is an interface which has unimplemented methods by default and we can add lines of code within each method. So whenever a specific event occurs, the code written within that method will be executed. onTestFailure is a method in which this listener will be invoked whenever the test fails.

Within this method we shall add our code to capture screenshots whenever the test case fails on execution.

Code Walkthrough

  1. Create a class and implement the ITestListener.
  2. implement the ITestListener

  3. Generate all the implemented methods in it.
  4. all the implemented methods

  5. OnTestSuccess()
    Once the test execution is complete and the test has been passed, this method will be invoked. We shall add the log included in the Extent Report to mark the test case as passed within this method.
  6. OnTestSuccess()

    MarkHelper is a class that is used for customizing the Extent Reports in Selenium by creating labels for the tests and adding colors to the test status by using ExtentColor enum.

    Here, the tests which are passed will be updated with a label – testname with “PASS”.

    testname with “PASS”.

    Also, the status of the status will be marked with green color as we have provided ExtentColor.GREEN.
    ExtentColor.GREEN

  7. OnTestFailure()
    Once the test execution is complete and the test has failed, this method will be invoked. We shall add the log to be included in the Extent Report to mark the test case as failed within this method. Also, we add code to capture screenshots whenever the test fails.
  8. OnTestFailure

    So when a test fails, logs will be captured with the exceptions and attached to the Extent Report in Selenium.

    The addScreenCaptureFromPath() is a method in ExtentTest class that attaches the captured screenshot in the Extent Report.

    addScreenCaptureFromPath

    It takes the image path where the screenshot has been captured as the parameter and attaches the screenshot to the Extent Report in Selenium.

    We can invoke the CaptureScreenshot() method which we have implemented in the BaseTest class.

    CaptureScreenshot

    Using getScreenshotAs() method we shall capture the screenshot and save it in the path. This path will be returned to addScreenCaptureFromPath(), which will attach the screenshot in the Extent Report.

    getScreenshotAs

    Upon test execution for the failed tests, you would see the screenshot attached to the respective tests.

    Upon test execution for the failed tests, you would see the screenshot attached to the respective tests.

    Clicking on it will show an enlarged view of the screenshot. This will help in analyzing the test failure.

TestNG.xml

In the TestNG.xml file, we shall add our classes and also the listener class.

Output Console:

Output Console

Cloud Grid Console

In the LambdaTest Dashboard, you can see our tests that are executed. You can see four tests that were executed and on clicking each test you can further navigate and verify the test details like the browser and its version, the platform upon which the tests were executed.

LambdaTest Dashboard

LambdaTest Dashboard test completed

This section shows the browser and platform configurations in which the tests were executed. As mentioned in the DesiredCapabilities, the tests were run in the Chrome browser of version 96.0 and the Windows operating system.

DesiredCapabilities

There is also a video recording attached to the tests, which shows the end-to-end flow of our tests. This will help us to analyze when there are test failures. There is also an option to download the captured video.

video recording

Extent Reports

Now that our tests have been executed in the cloud-based Selenium Grid. Now let’s see if our extent report has been generated.

Yes, it is…! You can see the report being generated in the mentioned path.

  1. Right-click the report and open it in your choice of browser.
  2. Right-click the report and open it in your choice of browser

  3. Upon opening the Extent Report, you can see the summary of the tests executed.
  4. Extent Report

  5. On clicking the category tab, you can see how the tests are categorized into different groups as mentioned in our tests.
     
    We have assigned 3 tests for the P0 category and 1 test for P1 category. In the category tab, you can visualize the same. In a similar way, we can categorize our tests into Regression, Smoke, Sanity, etc. Thus, categorization will help us to get the test execution summary in a structured way. We can take a quick look at the execution of the tests based on the category by filtering out the specific category in the Extent Report.
  6. specific category in the Extent Report

    This is the view of the dashboard in the Extent Report. This page provides a complete view of the total number of the tests executed, passed tests, failed tests, the total time taken for executing the tests, and also the classification of the tests based on the category.

    dashboard in the Extent Report

How to execute parallel tests and generate Extent Reports in Selenium?

As the name suggests, parallel testing is the process of running the test methods/test classes in parallel rather than in sequential order. Parallel execution in TestNG is widely preferred in automation as it reduces the time taken for test execution. We can apply parallel execution on method level, class level, test level, and instance level.

  • Methods: This will run the parallel tests on all @Test methods in TestNG.
  • Tests: All the test cases present inside the tag will run with this value.
  • Classes: All the test cases present inside the classes that exist in the XML will run in parallel.
  • Instances: This value will run all the test cases parallel inside the same instance.

Now let us execute our tests in parallel in a cloud-based Selenium Grid like LambdaTest and then generate Extent Reports in Selenium.

We shall use the two classes – InputFormTest and AlertTests to carry out our parallel execution and then generate the final Extent Report in Selenium.

Parallel execution in Class level

Class 1: InputFormTest

Class 2: AlertTests

TestNG.xml
To run classes in parallel, we have mentioned parallel attribute with classes value and thread-count with a specific value. Here I have mentioned thread-count as 3 which means the classes mentioned in TestNG.xml will be executed in parallel in 3 threads.

Output Console

Here you can see the classes being executed in parallel.

classes being executed in parallel

Finally, the extent report has been generated for the tests executed in parallel.

the extent report has been generated for the tests executed in parallel

In the cloud Selenium Grid, we can visualize the tests executed.

visualize the tests executed

Parallel execution in method level

We shall use the InputFormTest class which has three test methods. Let us run them in parallel.

The implementation of the tests remains the same as used before.

TestNG.xml
To execute the methods in parallel , we use a parallel attribute with methods value and shall define the thread-count as 3. This will execute the methods in the class in parallel in 3 different threads.

Test Execution

When the tests are executed, we shall see the methods running in parallel in three threads.

methods running in parallel in three threads

Extent Report

Once the execution is complete, the Extent Report will be generated.

the Extent Report will be generated

Cloud Selenium Grid Dashboard

Cloud Selenium Grid Dashboard

How to generate Extent Reports in Selenium using Jenkins?

Jenkins is an open-source DevOps tool that has been popularly used for continuous integration and continuous delivery processes. It is a Java-based application and platform-independent. It is a build tool used for running builds from the source code repository, running unit tests, and sending the build reports to the respective member or team.

To get a detailed understanding of Jenkins setup and creating various projects, please go through our blog on continuous integration with Jenkins.

Steps to generate Extent Reports in Selenium using Jenkins

To generate Extent Reports in Selenium using Jenkins, please follow the below-mentioned steps:

  1. Login into Jenkins and click Manage Jenkins.
  2. Login into Jenkins and click Manage Jenkins

  3. Install the HTML Publisher plugin.
  4. Once installed, you will see the HTML Publisher plugin in the Installed tab.
  5. HTML Publisher plugin

  6. After installing the plugin, make sure that you restart the Jenkins.
  7. Now click New Item and create a Project.
  8. New Item and create a Project

  9. Integrate your GitHub account in the source code management section. This helps us to pull the code from the GitHub account and run the tests in Jenkins.
  10. Integrate your GitHub account

  11. In the Post-Build Actions section, select Publish HTML Reports option under Add Post-Build action.
  12. select Publish HTML

  13. In this section, provide the folder name where your report would be generated, the name of the Extent Report, and the title of the Extent Report.
  14. the title of the Extent Report

  15. Save the changes.
  16. Click Build Now.
  17. Build Now

  18. You shall see a job being triggered within the project.
  19. Once the build has been completed, you shall see ExtentReport in the project, as highlighted in the below screenshot.
  20. ExtentReport in the project

  21. Click on the ExtentReport.
  22. Now the generated Extent Report will be displayed. You can access the Extent Report to analyze the test execution.
  23. tent Report to analyze the test execution

Wrapping up..!

In this article on generating Extent Reports in Selenium, we have explored a new topic on integrating a reporting library with our automation framework. Extent Reports have been widely used by the automation testers for generating the test report and sharing it with the stakeholders in the organization.

As test reports play an important role in automation, it is always better to have a good report that captures the entire test execution summary with graphical representation. Extent Reports not only provide a detailed summary of the executed tests but also provide pie charts depicting the test summary in visual form.

Moreover, it is very easy to replace any existing reporting tool with Extent Reports in an automation framework. The Extent Report is very simple yet provides extraordinary test reports. I hope this article has been informative and will help you in integrating the reporting tool into your automation framework without any hurdles. Try your hands on these Extent Reports in Selenium, and let me know your feedback on this article. Keep exploring..!

Happy Testing…!

Frequently Asked Questions (FAQs)

What is the use of extent reports?

Extent Reports is a very easy-to-use library that lets you create custom reports not only during the test runs but in any project you are working on.

Is extent report part of TestNG?

An Extent report is a customized HTML-based report integrated with Selenium using the TestNG framework. It enables customers to create a wide variety of data-driven reports. It allows them to generate documents and XML feeds that execute complex actions and make complex decisions.

Author Profile Author Profile Author Profile

Author’s Profile

Shalini Baskaran

Shalini works as a Technical Writer at LambdaTest. She loves to explore recent trends in test automation. Other than test automation, Shalini is always up for travel & adventure.

Blogs: 18



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free