Built.io Blog

Customized HTML Reporting in Selenium Web Driver

,

Introduction

As automation testers, we need to generate customized reports to analyze test cases and categorize them with the proper status on a daily basis. Selenium, by default, does not provide any reporting mechanisms. To generate deep, insightful reports in Selenium, we can use the open source HTML reporting library called ExtentReport. This library is extremely helpful for testers, and is quite easy to use. It generates reports that show the summary of each step and each test case in a toggle view for quick analysis.

Features

By using ExtentReport, you are able to access many reporting features that would otherwise not be available.

  • Categorizes test cases: We can assign a particular category to a test case depending on our requirements.
  • Simplifies analysis: Classifies the test based on the different statuses and gives a visual summary, which simplifies analyzing the report.
  • Allows insertion of logs: Permits insertion of logs in the HTML report, eliminating the need to generate the logfile separately.
  • Provides a filter option: We can filter the report based on categories and the current status of the test to avoid missing out on any category.
  • Additional information: Allows the addition of extra listings, such as system information and an author name in the report for its ease of use.
  • Interactivity: Screenshots and videos with their description can be easily inserted in the report to enhance the report and make it more user friendly.
  • Parallel execution: Supports parallel execution, that is, it generates a combined report depending on how your tests are structured as per the classes and methods. If you require a single report for run sessions, you can use only one instance of ExtentReports.

Prerequisites

These are the requirements to use the ExtentReport library:

  • Java
  • Eclipse
  • Apache Maven
  • Selenium JAR
  • TestNG

Setup

For Java:

Download the JAR file of the ExtentReport library for the respective version of your Java from http://extentreports.relevantcodes.com/, and add it to your project library.

For Maven:

Add the following Maven dependency to your POM.xml file and save it. Then update your Maven project. This will automatically download the JAR file from the Maven central repository.

<dependency><br> <groupId>com.relevantcodes</groupId><br> <artifactId>extentreports</artifactId><br> <version>2.40.2</version><br> </dependency><br>

Example

Here are two demo test cases for generating the HTML report—one with status as pass and one with fail.

import java.io.File;<br> import java.io.IOException;<br> import org.apache.commons.io.FileUtils;<br> import org.openqa.selenium.By;<br> import org.openqa.selenium.OutputType;<br> import org.openqa.selenium.TakesScreenshot;<br> import org.openqa.selenium.WebDriver;<br> import org.openqa.selenium.firefox.FirefoxDriver;<br> import org.testng.annotations.Test;<br> import com.relevantcodes.extentreports.ExtentReports;<br> import com.relevantcodes.extentreports.ExtentTest;<br> import com.relevantcodes.extentreports.LogStatus;<br> public class ExtentReport {<br> ExtentReports report;<br> ExtentTest test<br> WebDriver driver;<br> //pass scenario<br> @Test(priority = 1, enabled = true)<br> public void verifyHomePageTitle() {<br> report = new ExtentReports(<br> "D:\\ContenstackWorkspace\\Contentstack\\Result\\automationreport.html", true);<br> test = report.startTest("Verify application Title");<br> driver = new FirefoxDriver();<br> driver.manage().window().maximize();<br> test.log(LogStatus.INFO, "Browser started");<br> driver.get("https://www.built.io/");<br> String title = driver.getTitle();<br> System.out.println(title);<br> if (title.equals("Built.io – Build mobile apps fast, Connect your digital business, Deliver content everywhere")) {<br> test.log(LogStatus.PASS, "verify Title of the page");<br> } else {<br> test.log(LogStatus.FAIL, "verify Title of the page");<br> }<br> report.endTest(test);<br> report.flush();<br> }<br> //Fail scenario<br> @Test(priority = 2, enabled = true)<br> public void verifyLogo() throws IOException {<br> test = report.startTest("Verify logo of the application");<br> String src = driver.findElement(By.xpath("//img[@class='img']"))<br> .getAttribute("src");<br> if (src.equals("")) {<br> test.log(LogStatus.PASS, "verify logo");<br> } else {<br> test.log(LogStatus.FAIL, "verify logo");<br> File scrFile = ((TakesScreenshot) driver)<br> .getScreenshotAs(OutputType.FILE);<br> FileUtils.copyFile(scrFile, new<br> File("D:\\ContenstackWorkspace\\ContentStackUIAutomation\\screenshot\\img.jpg"));<br> String image= test.addScreenCapture("D:\\ContenstackWorkspace\\ContentStackUIAutomation\\screenshot\\img.jpg");<br> test.log(LogStatus.FAIL, "verify logo of the application", image);<br> }<br> report.endTest(test);<br> report.flush();<br> driver.quit();<br> }<br> }<br>

Here’s a screenshot elaborating the success and failure of the sample test cases:

Screen-Shot-2016-03-30-at-7.07.02-PM.png

Test and run

Perform the following steps to run the test case using the ExtentReport library:

  • Create a testNg.xml file.
  • Right-click on testNg.xml and navigate to Run As | TestNG suite.

    That's it, you're done! ExtentReport will automatically generate the HTML report of your test case.

    Subscribe to our blog