Close Menu
TechBeamersTechBeamers
    TechBeamersTechBeamers
    • Python
    • Java
    • C
    • SQL
    • MySQL
    • Selenium
    • Testing
    • Agile
    • Linux
    • WebDev
    • Technology
    TechBeamersTechBeamers
    Selenium Tutorial

    3 Unique Ways to Generate Reports in Selenium

    By Harsh S.Updated:Oct 31, 202311 Comments11 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    This Selenium tutorial describes three cutting-edge techniques that can help you generate reports that are user-friendly, intuitive, and informative.

    A perfect test automation tool is what you need for the successful execution of the testing requirements in the Agile process. And there are multiple factors that play a critical role in the formation of a robust automation framework. One such element is reporting.

    The automation reports not only make you aware of the status of the success or failure but also help you in finding out the potential bugs. So, you should mindfully choose how you are going to generate reports in the Selenium Webdriver project. That’s where this post would lead you to make a decision.

    Introduction

    Before we proceed with the topic, it is essential that you understand with clarity – What is the purpose of reporting? The simple reason behind this is a good test report serves as a health certificate for your Project. Once you show it to management, they can make decisions to ship it to the customer or allow the team to focus on the shortcomings cited in the report.

    What are the essential qualities of a good test report?

    • Brevity –
      • A report should be short and concise.
      • It should reveal the total number of successes as well as failures.
    • Trackability –
      • Captures all the footprints that could lead to the root cause of a failure.
    • Traceability –
      • It must provide the ability to review the following.
        • Historical data for test cases and failures
        • Age of a particular defect
    • Sharable –
      • It should support a format that you can share through email or integrate with CI tools like Jenkins/Bamboo.
    • Test coverage –
      • It should highlight the test coverage for the following.
        • Test coverage of the module under test.
        • Test coverage of the application under test.

    Now you would have gained a fair idea of the fact that a good test report can maximize the returns on investment. Let’s move our focus to three techniques that you may use to generate reports in Selenium Webdriver. Here are the quick links that can navigate you to the report generation methods of your choice.

    • TestNG HTML Report Generation.
    • JUnit-Style Report Generation.
      • Simple JUnit Reports, and
      • Convert JUnit to HTML Reports.
    • Generating Extent HTML Reports.

    3 Techniques to Generate Reports in Selenium Webdriver.

    3 Unique Ways to Generate Reports in Selenium Webdriver
    3 Unique Ways to Generate Reports.

    Selenium Webdriver doesn’t have a built-in reporting feature, but there are plugins like TestNG and JUnit that can add this functionality.

    1- Generate Reports Using TestNG.

    TestNG library brings a very convenient reporting feature. Once you execute the tests, TestNG generates a test output folder at the root of the project. It combines two kinds of reports.

    Detailed report.

    You can find this report in the <index.html> file. It combines detailed information like the errors, test groups, execution time, step-by-step logs, and TestNG XML file.

    TestNG framework in Java
    Generate Reports (index.html) in TestNG

     Summary report.

    It is the trimmed version and informs about the number of “Passed”/”Failed”/”Skipped” cases. You can see it from the <emailable-report.html> file. It’s an email-friendly report which you can embed and share with the stakeholders.

    Generate Reports (emailable-report.html) in TestNG
    Generate Reports (emailable-report.html) in TestNG.

    1.1- Steps to Generate Reports Using TestNG.

    Step#1 Select the TestNG Reporting Interface.

    The TestNG framework provides two methods for generating reports in a Selenium project.

    1. The most common method is using the TestNG <ITestListener> Interface.
    2. Another method is to use the <IReporter> Interface.
    See also  Handle Checkbox & Radio Button in Webdriver

    In this blog post, we’ll cover the application of the <ITestListener> Interface. You’ll have to create a TestNG project in Eclipse. You can refer to our below post that teaches how to create a basic TestNG project.

    Also Read: Learn to Create a TestNG Project in Eclipse

    Step#2 Generate Reports Using the <ITestListener> Interface.
    import org.testng.ITestContext;
    import org.testng.ITestListener;
    import org.testng.ITestResult;
    
    public class GenerateReport implements ITestListener {
    
        @Override
        public void onStart(ITestContext arg0) {
    
            System.out.println("+Begin test: " + arg0.getName());
        }
    
        @Override
        public void onTestStart(ITestResult arg0) {
    
            System.out.println(" Starting test: " + arg0.getName());
        }
    
        @Override
        public void onTestSuccess(ITestResult arg0) {
    
            System.out.println(" Test passed: " + arg0.getName());
        }
    
        @Override
        public void onTestFailure(ITestResult arg0) {
    
            System.out.println(" Test failed: " + arg0.getName());
        }
    
        @Override
        public void onTestSkipped(ITestResult arg0) {
    
            System.out.println(" Test ignored: " + arg0.getName());
        }
    
        @Override
        public void onFinish(ITestContext arg0) {
    
            System.out.println("-End test: " + arg0.getName());
        }
    
        @Override
        public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
    
            // TODO Auto-generated method stub
        }
    }
    Step#3 Verify the Report Generation Process.
    import org.testng.Assert;
    import org.testng.annotations.Listeners;
    import org.testng.annotations.Test;
    
    @Listeners(GenerateReport.class)
    public class VerifyReportTest {
    
        @Test
        public void testSimulation0() {
    
            Assert.assertTrue(true);
    
        }
    
        @Test
        public void testSimulation00() {
    
            Assert.assertTrue(false);
    
        }
    
        // Test case <testSimulation000> depends on the intentionally
        // failed test case <testSimulation00>
    
        @Test(dependsOnMethods = "testSimulation00")
        public void testSimulation000() {
    
        }
    }

    2- Generate Reports Using JUnit.

    JUnit is another useful framework that can add the ability to generate reports in Selenium. It provides the JUnit <TestWatcher> class to introduce reporting ability.

    The JUnit’s TestWatcher class has the <failed()> and <succeeded()> methods which you can override. The JVM would call them automatically whenever it smells a pass or failure.

    2.1- Steps to Generate Reports in JUnit style.

    We’ve just summarized the summary of the steps that you can use to generate reports using the JUnit plugin.

    1. Create a new Java class (name it JUnitTestReporter) that applies the JUnit rules with the help of the TestWatcher() class.
    2. Override the <succeeded()> method so that the names of passed tests could be displayed at the console with the <Passed> status.
    3. Override the <failed()> method so that the names of the failed tests could appear in the console with the <Failed> status.
    4. Create a sample test class as <JUnitSampleTest> which must extend the <JUnitTestReporter> class to utilize the overridden <succeeded()> and <failed()> methods.

    Now we’ll explain how simple it is to work with JUnit so that you can quickly generate the summary of the test execution.

    2.2- Simple Report Generation Using JUnit.

    2.2.1- Generate Reports by creating a JUnit Test Watcher Class.

    Create a simple project in Eclipse and add the below file <JUnitTestReporter.Java> to your project. This file will display the report in the Eclipse console.

    import org.junit.Rule;
    import org.junit.rules.TestRule;
    import org.junit.rules.TestWatcher;
    import org.junit.runner.Description;
    import org.junit.runners.model.Statement;
    
    public class JUnitTestReporter {
    
        @Rule
        public TestRule junitWatcher = new TestWatcher() {
    
            @Override
            public Statement apply(Statement base, Description description) {
                return super.apply(base, description);
            }
    
            @Override
            protected void succeeded(Description description) {
                System.out.println(description.getDisplayName() + " " +
                    "Test Passed!");
            }
    
            @Override
            protected void failed(Throwable e, Description description) {
                System.out.println(description.getDisplayName() + " " +
                    e.getClass().getSimpleName());
            }
        };
    }
    2.2.1- Prepare a Sample JUnit Test Suite to Verify Report Generation.

    Also, add the below file <JUnitSampleTest.Java> to your project. It’ll be the main test file which you’ll execute from the Eclipse.

    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    
    public class JUnitSampleTest extends JUnitTestReporter {
    
        @Test
        public void sampleTest0() {
            assertTrue(1 < 2);
        }
    
        @Test
        public void sampleTest1() {
            assertTrue(1 > 2);
        }
    
        @Test
        public void sampleTest2() {
            assertTrue(1 < 2);
        }
    
        @Test
        public void sampleTest4() {
            assertTrue(1 > 2);
        }
    }

    When you run the above JUnit tests from Eclipse, it’ll show you the test results in Eclipse as captured in the below screenshot. The downside to this technique is that it doesn’t save the output in an HTML file.

    Generate Reports Using JUnit Plugin
    Generate Reports Using JUnit Plugin.

    But you can generate an HTML report by modifying the above code. We’ve given the technique to produce the test summary in the next section.

    See also  Selenium Java App to Find Blog Rank in Google

    2.3- HTML Report Generation Using JUnit.

    We’ll create a new JUnit class that would enable the HTML report generation. This class will also override the TestWatcher methods to implement the desired features.

    • We’ll define two static members; one is a File object and the second one is BufferWriter’s handle that will help us add the test execution summary to the report file.
    • We’ll use the following JUnit annotations.
      • @BeforeClass – It’ll help us define the setup() method. It’ll create/open the HTML report file as per the situation.
      • @AfterClass – We’ll use it to execute clean-up tasks. It’ll also update the HTML report file to add the HTML footer and close all the open handles.
    • We’ll override the following two methods.
      • <succeeded()> – It’ll write the names and status of the test cases passed during execution.
      • <failed()> – It’ll log the names and state of the test cases that failed during execution.

    Now, you’ll find the source code of the <JUnitHTMLReporter> class.

    import java.awt.Desktop;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Rule;
    import org.junit.rules.TestRule;
    import org.junit.rules.TestWatcher;
    import org.junit.runner.Description;
    import org.junit.runners.model.Statement;
    
    public class JUnitHTMLReporter {
    
        static File junitRpt;
        static BufferedWriter junitWrt;
    
        @BeforeClass
        public static void setUp() throws IOException {
    
            String junitFile = System.getProperty("user.dir") +
                "\\junitReportFile.html";
            DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
            Date date = new Date();
            junitRpt = new File(junitFile);
            junitWrt = new BufferedWriter(new FileWriter(junitReport, true));
            junitWr.write("<html><body>");
            junitWr.write("<h1>Test Execution Summary - " + dateFormat.format(date) +
                "</h1>");
    
        }
    
        @AfterClass
        public static void tearDown() throws IOException {
    
            junitWrt.write("</body></html>");
            junitWrt.close();
            Desktop.getDesktop().browse(junitReport.toURI());
    
        }
    
        @Rule
        public TestRule watchman = new TestWatcher() {
    
            @Override
            public Statement apply(Statement base, Description description) {
                return super.apply(base, description);
            }
    
            @Override
            protected void succeeded(Description description) {
                try {
                    junitWrt.write(description.getDisplayName() + " " +
                        "success!");
                    junitWrt.write("<br/>");
                } catch (Exception e1) {
                    System.out.println(e1.getMessage());
                }
            }
    
            @Override
            protected void failed(Throwable e, Description description) {
                try {
                    junitWrt.write(description.getDisplayName() + " " +
                        e.getClass().getSimpleName());
                    junitWrt.write("<br/>");
                } catch (Exception e2) {
                    System.out.println(e2.getMessage());
                }
            }
        };
    }

    So, we have a ready-to-use HTML reporter class, all we need now is the JUnit Test class that will extend it and call its methods. The code of this new class is available below.

    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    
    public class JUnitHTMLSampleTest extends JUnitHTMLReporter {
    
        @Test
        public void sampleTC0() {
            assertTrue(1 < 2);
        }
    
        @Test
        public void sampleTC1() {
            assertTrue(1 > 2);
        }
    
        @Test
        public void sampleTC2() {
            assertTrue(1 < 2);
        }
    
        @Test
        public void sampleTC3() {
            assertTrue(1 > 2);
        }
    }

    When you run the above class file as a JUnit test case, then the <junitReportFile.html> will get generated. It’ll contain the test case’s pass/fail info and look like the one given in the below screenshot.

    JUnit HTML Format
    Generate Reports in HTML Format Using JUnit

    3- Generate Reports Using Extent Library.

    The third and last technique for generating some stylish reports is by using the <Extent Report> library. It comes with a rich set of features.

    • Ability to generate dynamic HTML logs
    • Represents test case status with the help of Pie Charts
    • Creates a step-by-step test case summary
    • Ability to filter reports based on test status
    • Maintains execution history
    • It captures details like OS, Memory, Java version, and so on
    • Can attach error screenshots within the report
    See also  Understand Webdriver Fluent Wait with Examples

    You can download the Extent library from here: https://github.com/extent-framework

    Generate Reports Using Extent Library
    Download Extent Library for Java

    Once you have the reporting library, then follow the below steps to use them in a Selenium Webdriver project.

    • Create or open a demo project in Eclipse
    • Add the Jar files shown in the above picture as external libraries to your project.
    • Also, make sure to add the Jars that are in the <lib> folder else the exception may occur at run-time.

    Now, we are attaching the source code of the sample Java program that we created for the Extent library report validation. You can use the below Java file as is in your demo project.

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.Assert;
    import org.testng.annotations.Test;
    
    import com.relevantcodes.extentreports.ExtentReports;
    import com.relevantcodes.extentreports.ExtentTest;
    import com.relevantcodes.extentreports.LogStatus;
    
    public class ExtentReportTest {
    
        @Test
        public void verifySeleniumBlog() {
    
            String extfile = System.getProperty("user.dir") +
                "\\extfile.html";
            String extimg = System.getProperty("user.dir") +
                "\\extimg.png";
    
            // Create object of extent report and specify the report file path.
            ExtentReports ext = new ExtentReports(extfile, false);
    
            // Start the test using the ExtentTest class object.
            ExtentTest extTest = ext.startTest("My First Test",
                "Verify WebSite Title");
    
            // Launch the FireFox browser.
            WebDriver driver = new FirefoxDriver();
    
            driver.manage().window().maximize();
    
            extTest.log(LogStatus.INFO, "Browser Launched");
    
            // Open application.
            driver.get("/");
    
            extentTest.log(LogStatus.INFO, "Moved to www.techbeamers.com");
    
            // get title.
            String title = driver.getTitle();
    
            extentTest.log(LogStatus.INFO, "Get the WebSite title");
    
            // Verify title.
            Assert.assertTrue(title.contains("Selenium Webdriver"));
    
            extentTest.log(LogStatus.PASS, "Title verified");
    
            // In case you want to take an error screenshot
            extTest.log(
                LogStatus.INFO,
                "Error Snapshot : " +
                extentTest.addScreenCapture(extentReportImage));
    
            // Close application.
            driver.quit();
    
            extTest.log(LogStatus.INFO, "Browser closed");
    
            // close report.
            ext.endTest(extTest);
    
            // writing everything to document.
            ext.flush();
        }
    }

    Once you run the above code, it’ll launch the specified URL in Firefox and produce an intuitive Extent test report. The final test report would look like the one shown in the below screenshot.

    Generate Reports in Selenium Webdriver
    Live Extent Report Example.

    So, that was all we wanted to share in this post. We hope it will enable you to use the three distinct methods to generate reports in Selenium. And, you’ll be able to reap the real benefits of the test automation.

    Quick Review – Report Generation Techniques in Selenium.

    In this post, we discussed all the traditional and modern-day report generation techniques. We hope that they will be quite useful in your projects.

    Now, it’s entirely up to you how you manage them. However, you can always ask for help when the solution given here doesn’t work, or you are stuck at some point. We’ll try to respond to your questions as soon as possible.

    You are also welcome to share if you’ve implemented reporting in Selenium differently. We’ll review and publish it on our blog with a ‘Thank You‘ note mentioning your name.

    All the Best,

    TechBeamers.

    Advanced Selenium Tutorial
    Previous Article3 Unique Ways to Handle File Upload In Selenium
    Next Article Handle Ajax Calls using Selenium Webdriver
    Harsh S.

    I'm Harsh, an experienced Software developer with a passion for technology. Specializing in C, Python, Java, Linux, Agile, and more, I love sharing insights through detailed tutorials, quizzes, exercises, and interview questions on various tech topics. Don't miss out on my latest tutorials to level up your skills!

    View 11 Comments

    11 Comments

    1. Jigs on Apr 26, 2017 10:36 pm

      Hi,

      The link [http://relevantcodes.com/ExtentReports-for-selenium/] is not working. Also, if I download regular extent Jar, it is not working w the code. I am assuming u guys have modified it. Any suggestion?

      Reply
      • Meenakshi Agarwal on Apr 27, 2017 7:53 pm

        Hello, Jigs – It seems the extent report has gone commercialized now. However, they are still providing a community version. Follow them here http://extentreports.com/community/.
        Let me know if you still fall into issues. I’ll try to revert as the earliest.

        Reply
    2. Sebastian on Feb 16, 2017 12:18 am

      Suppose I am using data driven framework and I have an excel with over 20 username and passwords. I have to login into each account and perform the same operation. So How will I report it to the client?

      Reply
      • Meenakshi Agarwal on Feb 16, 2017 10:49 pm

        Hi Sebastian – I think the scenario like you said (multiple users/passwords) is covered in this tutorial. Because here also you have multiple rows with different inputs (however, not exactly the users/passwords). But similarly, you can update the input sheet with a set of user names and passwords. And also write a function in the data-driven framework that you want to call. After execution, you can share the report inside the “index.html” file which shows the passed/failed status of each record.

        Another approach could be to write back the pass/fail status into the excel sheet for each record. You can search this blog for writing results to excel file. We’ve also got it covered in one of the Selenium tutorials.

        Reply
    3. Yogiraj on Aug 23, 2016 3:56 pm

      Hello Techbeamers,

      I have a query regarding extent reporting, suppose i have a simple test method similar to what you have written here but it calls some other methods from it e.g i have a loginPage class which has login method which accepts username and password and perform login.(basically i have page object model)
      Now in this case you have declared and initialized the extent class in test class but how would i access the same report/log file from login method and add logs from it.

      Reply
      • Meenakshi Agarwal on Aug 24, 2016 11:10 pm

        Hello Yogiraj – It’s indeed a very good question that you’ve asked. And as you want to access the Extent report instance in some other class, so consider declaring it as static in your project’s primary class. You would then need to initialize it in some startup code like the @before class method. Then, you can access it as Classname.variableName from any class of your project.

        Reply
        • Yogiraj on Aug 26, 2016 12:16 pm

          Thanks Meenakshi, it worked.. you guys are making life easier keep it up 🙂

          Reply
          • Meenakshi Agarwal on Aug 27, 2016 12:34 pm

            Great. Thanks for confirming it.

            Reply
          • vijay on Jul 04, 2017 3:14 pm

            Facing same issue. Could you please share the code..

            Reply
    4. SRIDHAR KRISHNAPPA on Jul 28, 2016 8:15 am

      Which one is the best approach to generate reports?

      Reply
      • Meenakshi Agarwal on Jul 28, 2016 11:37 pm

        In our opinion, TestNG reports are the best, but you can club extent report along with it. TestNG reports also have a better integration with CI tools like the Jenkins and Bamboo.

        Reply

    Leave A Reply Cancel Reply

    Top Selenium Questions
    • Selenium Interview Q&A
    • Selenium Webdriver Q&A
    • Selenium Testing Q&A
    • Common Selenium Q&A
    • Selenium Coding Q&A
    • Selenium Java Q&A
    • Selenium Appium Q&A
    • Selenium Cucumber Q&A
    • TestNG Interview Q&A
    Top Selenium Quizzes
    • Selenium Quiz-1
    • Selenium Quiz-2
    • Selenium Quiz-3
    • Selenium Python Quiz
    • Selenium Appium Quiz
    • Selenium TestNG Quiz-1
    • Selenium TestNG Quiz-2
    Top Selenium Tutorials
    • Selenium WebDriver Download & Install
    • Selenium Report Generation
    • Handle File Upload
    • Handle HTML Tables
    • Handle Keyboard/Mouse
    • Handle AJAX Calls
    • Handle Date-Time Picker
    • Run Selenium Tests Using Task Schedular
    • Selenium to Add Items to a Cart
    Selenium Grid Tutorial
    • Selenium Grid Download
    • Setup Selenium Grid
    Top Selenium Tips
    • Selenium HowTos
    • Selenium Coding Tips
    • Essential Selenium Skills
    • Sample Selenium Resume
    • Selenium Job Profile Tips
    TestNG Tutorials
    • Install TestNG In Eclipse
    • TestNG Annotations
    • TestNG Assertions
    • TestNG Parameters
    • TestNG DataProvider
    • Create TestNG Tests
    • TestNG Using Maven
    • TestNG Result in Excel
    • TestNG Threads - Demo
    • TestNG Factory - Demo
    • Write Data Driven Tests
    Selenium IDE
    • Selenium IDE Download
    • Use Selenium IDE
    • Selenium IDE Add-ons
    Selenium Locators
    • How to Use Locators
    • FireBug & FirePath
    • XPath Chrome Extensions
    • Locator Best Practices
    • Locator Strategies
    Selenium Web Elements
    • Find Web Elements
    • Dropdown & Multi Selects
    • Checkbox & Radio Buttons
    Create Selenium Projects
    • Setup Selenium2 Project
    • Setup Selenium3 Project
    • Use Selenium Maven
    • Selenium Maven Project
    • How to Use IE Driver
    • How to Use Chrome Driver
    Selenium Framework- Java
    • Data Driven Framework
    • Page Object Model (POM)
    • Object Repository (OR)
    • POM vs OR
    Latest Posts
    • 10 Selenium Technical Interview Questions and Answers.
    • 100+ Selenium Interview Questions and Answers You Need to Know in 2024
    • 15 Java Coding Questions for Testers
    • 20 Most Unique Appium Questions and Answers
    • 3 Unique Ways to Handle File Upload In Selenium
    • 35 Selenium Webdriver Questions for Interview
    • 4 Must-have Selenium IDE Add-ons for Firefox

    Subscribe to Updates

    Get the latest tutorials from TechBeamers.

    Loading
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    • Terms of Use
    © 2023 TechBeamers. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.