Selenium HTML result reporting using ExtentReports 2.X

While working with Selenium automation testing, we use different 3rd party frameworks (TestNG, Junit, MBunit, Nunit) in combination with Java or .NET to report the result at the end of the test execution or we use our own dashboard/html kind of reports,

While browsing different ways of reporting, got web links of ExtentReports, most of the links talk about the version 1.x,


but when I looked into the original web link http://extentreports.relevantcodes.com/2x/docs.html#basic-usage-example by AnsooArora, it's upgraded to 2.x, this is something different approach (in configuration wise) than the earlier version 1.x

Let's see some of the highlights of the latest version 2.x (till now),
Note: we will see the implementation with selenium and Java.

Download the jar file from the below location
http://relevantcodes.com/extentreports-for-selenium/


Extract and add the extentreports-java-2.03.jar file along all required Selenium *.jar files in Eclipse Java Build Path.

We will directly move to the code part and then will see details about each statement, and will see the result report at the end.

Have 3 separate class files, 
1. Initial setup of ExtentReports
2. TestCase 1 (open browser, perform test, close browser)
3. TestCase 2 (open browser, perform test, close browser)

Class1
public class ExtentManager {
 
public static ExtentReports Instance()
       {
 ExtentReports extent;
 String Path = "./ExtentReport.html";
 System.out.println(Path);
 extent = new ExtentReports(Path, false);
 extent.config()
              .documentTitle("Automation Report")
              .reportName("Regression");

 return extent;
    }
public static String CaptureScreen(WebDriver driver, String ImagesPath)
{
    TakesScreenshot oScn = (TakesScreenshot) driver;
    File oScnShot = oScn.getScreenshotAs(OutputType.FILE);
 File oDest = new File(ImagesPath+".jpg");
 try {
      FileUtils.copyFile(oScnShot, oDest);
 } catch (IOException e) {System.out.println(e.getMessage());}
 return ImagesPath+".jpg";
        }
}

Class2 : Testcase 1
public class TC1 {
 
 ExtentReports extent;
 ExtentTest test;
 WebDriver driver;
 
 @BeforeClass
 public void M1(){
  extent = ExtentManager.Instance();
  driver = new FirefoxDriver();
 }
 
 @Test
 public void OpenAUT()
 {
  try{
  driver.get("http://www.qavalidation.com/");
  test = extent.startTest("OpenUT", "Verify HomePage");
  if(driver.getTitle().contains("QA & Validation"))
   test.log(LogStatus.PASS, driver.getTitle() +" contain "+"QA & Validation" );
  else
   test.log(LogStatus.FAIL, driver.getTitle() +" doesn't contain "+"QA & Validation" );
  }catch(Exception e){test.log(LogStatus.ERROR, e.getMessage());}
 }
  
 @AfterClass
 public void tear()
 {
  extent.endTest(test);
  extent.flush();
  extent.close();
  driver.quit();
 }
}

Class3: TestCase2
public class TC2 {
 ExtentReports extent;
 ExtentTest test;
 WebDriver driver;
 
 
 @BeforeClass
 public void M1(){
  extent = ExtentManager.Instance();
  driver = new FirefoxDriver();
 }
 
 @Test
 public void M3()
 {
  try{
   test = extent.startTest("ContactPage", "Verify Send button");
   driver.get("http://www.qavalidation.com/");
   
   Assert.assertTrue(driver.getTitle().contains("QA & Validation"));
   test.log(LogStatus.INFO, "site opened");
   
   driver.findElement(By.linkText("Contact!")).click();
   Thread.sleep(2000);
   WebElement Send = driver.findElement(By.id("ContactForm1_contact-form-submit"));
   if(Send.isDisplayed())
   {test.log(LogStatus.PASS, Send.getAttribute("Value")+" button Found");
                         test.log(LogStatus.INFO, test.addScreenCapture(ExtentManager.CaptureScreen(driver, "./Send")));
                        }
   else
   {test.log(LogStatus.FAIL, Send.getAttribute("Value")+" button NOT Found" );}
  
   }catch(Exception e){test.log(LogStatus.ERROR, e.getMessage());}
 }
 
 @AfterClass
 public void tear()
 {
  extent.endTest(test);
  extent.flush();
  extent.close();
  driver.quit();
 }

}

Now create testNG.xml to run the above 2 test cases...

<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="pkg.TC1"/>
      <class name="pkg.TC2"/>
    </classes>
  </test> 
</suite>

ExtentReports.html file will be created under your project folder.

Open this file, in browser and observe a nice result report of your test run.


Now click on the summary tab, to view the steps details

In details:

ExtentReports extent = new ExtentReports(file-path, replaceExisting, DisplayOrder);
                          
file-path:
path to save the result report (in .htm or .html), dataType : String

replaceExisting:
true = Replace new file to the existing report              
false = append to the existing report if any.    
dataType : boolean

DisplayOrder:    
OLDEST_FIRST - Old tests display on top of the report
NEWEST_FIRST - New tests display on top of the report

extent.config()
      .documentTitle("Automation Report")
      .reportName("Regression");

Above is to give some basic information about the report, (this is optional)

extent
     .addSystemInfo("Selenium Version", "2.46")
     .addSystemInfo("Environment", "QA");

This above is will display on the report, this is optional.

Now, in each testcase -

  • extent = ExtentManager.Instance();

          getting the Extentreports object instance.

  • test = extent.startTest("OpenUT", "Verify HomePage");

          Write the startTest to initialize the ExtentTest object.

  • For pass scenarios: test.log(LogStatus.PASS, details);
  • For fail scenarios: test.log(LogStatus.FAIL, details);
  • For info :  test.log(LogStatus.INFO, "site opened");




Depending on the Pass, Fail, Info, Warning and Error - Result report shows with different colored annotations for quick look.



  • As we did, extent.startTest, at the end of test run, we need to do extent.endTest(test); 
  • extent.flush(); 
           To write all above steps, status, details to html

Even we can have screenshots embed inside the report,

test.log(LogStatus.INFO, test.addScreenCapture(ExtentManager.CaptureScreen(driver, "./Send")));
Means,
test.log(LogStatus.INFO, test.addScreenCapture(String path));

We need to write login to get the image path, which we written in ExtentManager class.

More details about How to enter information about System Configuration and environment details in terms of Map, category wise test run, inserting HTML content, you can refer below link
http://extentreports.relevantcodes.com/2x/docs.html#basic-usage-example

-----------------------------------
Please do post comments for any more information.....

If you are looking for earlier versions of the ExtentReports details, follow

http://extentreports.relevantcodes.com/1x/docs.html
http://www.ontestautomation.com/creating-html-reports-for-your-selenium-tests-using-extentreports/
http://learn-automation.com/advance-selenium-reporting-with-screenshots/

8 comments:

  1. when i use 2.10 extent jar the report shows like only 2 tabs in the menu if i add category (smoke) then seeing 3 tabs under menu section.........

    -- is there any possibility to add an image of Project with small icon at top right or left
    -- is there any way to hide the default host name and java version on Pie Chart below thing.
    -- is Jenkins support this report or any reports are there which supports for jenkins.

    ReplyDelete
  2. hi
    when creating extent instance
    if (extent == null) {
    extent = new ExtentReports(filePath, true);

    i got this error

    java.lang.NoSuchFieldError: VERSION_2_3_23
    at com.relevantcodes.extentreports.HTMLReporter.start(HTMLReporter.java:75)
    at com.relevantcodes.extentreports.Report.attach(Report.java:302)
    at com.relevantcodes.extentreports.ExtentReports.(ExtentReports.java:78)
    at com.relevantcodes.extentreports.ExtentReports.(ExtentReports.java:362)
    at extentFadi.ex.ExtentManager.Instance(ExtentManager.java:20)

    any help please

    ReplyDelete
  3. hi
    when creating extent instance
    if (extent == null) {
    extent = new ExtentReports(filePath, true);

    i got this error

    java.lang.NoSuchFieldError: VERSION_2_3_23
    at com.relevantcodes.extentreports.HTMLReporter.start(HTMLReporter.java:75)
    at com.relevantcodes.extentreports.Report.attach(Report.java:302)
    at com.relevantcodes.extentreports.ExtentReports.(ExtentReports.java:78)
    at com.relevantcodes.extentreports.ExtentReports.(ExtentReports.java:362)
    at extentFadi.ex.ExtentManager.Instance(ExtentManager.java:20)

    any help please

    ReplyDelete
  4. I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing.
    2021 Jamb runz

    ReplyDelete