How to use ExtentReport in a framework

29 / Jun / 2016 by Kimi Agarwal 11 comments

The other day while I was executing my TestNG test cases in the Selenium WebDriver, all my test cases got executed, and the report also showed correctly. However, I usually don’t find its user interface very friendly especially when it’s about creating reports. That’s when I came across a user friendly reporting tool called ExtentReport.

ExtentReport is an HTML reporting library for Java, which can be used with Selenium WebDriver. We can use this tool within our TestNG automation framework. As an automation tester, it’s expected of you to provide detailed yet reports and present it to the administration group. ExtentReport is a simple to use tool to make excellent execution reports.

Process to Set up ExtentReport:

  1. Install and set up Java on your system.
  2. Now setup Eclipse.
  3. Launch Eclipse and create a Maven project.
  4. Add Selenium, TestNG, ExtentReport dependency in the pom.xml file.

ExtentReport dependency in our maven project, use the below snippet:-

five

Use Case in which we are going to mechanize

  1. Check the current URL of the web page
  2. Enter invalid email id in the Sign Up content box
  3. Click on the Sign Up button
  4. To verify the error message.

Create a Simple TestNG Script
Following is the code snippet for our use case.

one

  1. startTest(“TestCaseName”, “Description”) : It starts the current test.

            TestCaseName – It will print the name of the Test Case in the Report.
            Description – It will print the description of the Test case.

  1. assignAuthor(“AuthorName”) : Test case designer name is mentioned here.
  2. assignCategory(“CategoryName”) : Category of the Test Case displays here.
  3. log(LogStatus.PASS, “Step details”): It’s a repository which keeps the information about the test cases whether passed or failed.
  4. endTest(test) : Current test case ends here.
  5. flush() : Once the session is finished, specifically call the flush method.
  6. close() : Call this method at the very end in the @AfterSuite annotation, it ought to be called just once.

Once the test cases are run successfully, the report will be created under your folder. Now, go to the folder and view the .html report.
For e.g.: The path of the report will be D:\MyExtentReport.html

ExtentReport Architecture
After executing the test cases, the ExtentReport design will look like this.

 two

three four

In this report, the main page is the dashboard where:-

  • You can view the total number of test cases and test steps
  • Start time and End time are displayed
  • Number of test cases passed, failed, skipped, and so on information is provided
  • Number of steps of test cases passed, failed, and so on data is given
  • Pass Percentage of total number of test cases is given
  • Category Name of the test cases is likewise shown on the dashboard.

Clicking on the middle tab, I will be able to see the suite level information and its status (whether it’s pass or fail). On clicking the respective test case name, it will show the test case details: its start time, end time and execution time.

Points of interest of ExtentReport

  1. Open source reporting library.
  2. Provide delightful and responsive UI.
  3. Gives dashboard to the whole run.
  4. Exceptionally easy to utilize API.
  5. Effortlessly coordinate test-runner logs.
  6. Parallel execution of classes and strategies upheld.
  7. It can also be configured on Jenkins server.

It’s a useful storehouse having an extraordinary HTML reporting feature. Automation Testers ought to utilize this report because of its effective reporting by which anybody can understand easily which test case is working appropriately and which falls flat.

HAPPY TESTING!

FOUND THIS USEFUL? SHARE IT

comments (11)

  1. Jain

    I have issue with ‘extent reports’ display in the browser. Our proxy server doesn’t allow the sites to render Java script and CSS. How can I work around this? Are you familiar with any security threats using this ‘extent reports’?

    Reply
    1. Kimi AgarwalKimi Agarwal

      For Protractor, you can design your report using Allure Reporting. ExtentReport is not supported for the JavaScript project.

      Reply
  2. RAHUL SINGH

    Wonderful explanation and review of extentreport. I certainly enjoyed it. It would have been one point solution for me if has covered mailing of this report to business/team post execution. But nevertheless, v. good effort to have very detail review of the report and hope to have many more articles. Cheers !

    Reply
    1. Kimi AgarwalKimi Agarwal

      As you can see the ‘TestNG Script’. So in that script, you can add more than one Test method then you will be able to see the result in ExtentReport.

      Reply
      1. Gurpreet Singh

        I am getting an exception while creating an instance of ExtentReports
        *********
        extent = new ExtentReports(“C:\\Selenium_Luna\\MyMaven_DataDrivenFramework\\reports\\”+fileName, true, DisplayOrder.NEWEST_FIRST);
        ****************************
        org.testng.TestNGException:
        Cannot instantiate class com.selenium.testng.ddf.testcases.LoginTest
        at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
        at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:363)
        at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:275)
        at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
        at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
        at org.testng.TestClass.getInstances(TestClass.java:100)
        at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:86)
        at org.testng.TestClass.init(TestClass.java:78)
        at org.testng.TestClass.(TestClass.java:41)
        at org.testng.TestRunner.initMethods(TestRunner.java:425)
        at org.testng.TestRunner.init(TestRunner.java:252)
        at org.testng.TestRunner.init(TestRunner.java:222)
        at org.testng.TestRunner.(TestRunner.java:171)
        at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
        at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
        at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:623)
        at org.testng.SuiteRunner.init(SuiteRunner.java:189)
        at org.testng.SuiteRunner.(SuiteRunner.java:136)
        at org.testng.TestNG.createSuiteRunner(TestNG.java:1375)
        at org.testng.TestNG.createSuiteRunners(TestNG.java:1355)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
        at org.testng.TestNG.runSuites(TestNG.java:1133)
        at org.testng.TestNG.run(TestNG.java:1104)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
        Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
        … 25 more
        Caused by: java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source)
        at org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source)
        at org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
        at com.relevantcodes.extentreports.Configuration.getConfigurationMap(Configuration.java:38)
        at com.relevantcodes.extentreports.Report.loadConfig(Report.java:445)
        at com.relevantcodes.extentreports.Report.(Report.java:566)
        at com.relevantcodes.extentreports.ExtentReports.(ExtentReports.java:66)
        at com.relevantcodes.extentreports.ExtentReports.(ExtentReports.java:362)
        at com.selenium.testng.ddf.basetest.ExtentManager.getInstance(ExtentManager.java:24)
        at com.selenium.testng.ddf.basetest.BaseTest.(BaseTest.java:41)
        at com.selenium.testng.ddf.testcases.LoginTest.(LoginTest.java:17)
        … 30 more
        Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        … 60 more

        Reply
        1. Kimi AgarwalKimi Agarwal

          Please use this.

          String workingDir = System.getProperty(“user.dir”);
          System.out.println(“Current working directory : ” + workingDir);
          extent = new ExtentReports(workingDir+”/Test_Execution_Report.html”, true,DisplayOrder.NEWEST_FIRST);
          extent.loadConfig(new File(workingDir+”/extent-config.xml”));

          Reply
          1. Kimi AgarwalKimi Agarwal

            You can use this script.
            package com.DoTrips;

            import com.relevantcodes.extentreports.ExtentReports;
            import com.relevantcodes.extentreports.ExtentTest;
            import com.relevantcodes.extentreports.LogStatus;

            public class Demo {
            public static ExtentReports extent;
            public static ExtentTest test;
            public static void main(String []args){
            extent = new ExtentReports(“D://Report//advanceExtentReport.html”,true);
            ExtentTest test = extent.startTest(“My First Test”, “Sample description”);
            test.log(LogStatus.PASS, “hello test”);
            extent.endTest(test);
            extent.flush();
            }
            }

Leave a comment -