How to Generate Extent Reports in Selenium WebDriver:
Selenium provides inbuilt reports using frameworks such as JUnit and TestNG.
Although the built-in reports provide information on the steps that are executed as part of the test case, they need more customization to be shared with all the major project stakeholders.
Extent Reports is a customizable HTML report developed by Anshoo Arora which can be integrated into Selenium WebDriver using JUnit and TestNG frameworks.
This tutorial will give you a complete step-by-step guide on how to generate Extent Reports in Selenium WebDrive with example codes.
Below is the snapshot of the built-in report provided by TestNG framework:
Extent Reports offer several advantages when compared to the built-in reports that are generated through JUnit and TestNG such as pie chart representation, test stepwise report generation, adding screenshots etc., at every test step and a presentable user interface that can be shared with all stakeholders of the project.
Below is the snapshot of a sample Extent Report in Pie Chart Representation
(Note: Click on any image for an enlarged view)
Table of Contents:
Advantages of using Extent Reports
There are several advantages of Extent Reports and few of them are discussed below.
- Customisable HTML report with stepwise and pie chart representation.
- Displays the time taken for test case execution within the report.
- Each test step can be associated with a screenshot.
- Multiple test case runs within a single suite can be tracked easily.
- Can be easily integrated with TestNG and JUnit frameworks.
Using Extent Reports in Selenium Webdriver
Extent Reports contain two major classes that are used frequently.
- ExtentReports class
- ExtentTest class
Syntax:
ExtentReports reports = new ExtentReports(“Path of directory to store the resultant HTML file”, true/false);
ExtentTest test = reports.startTest(“TestName”);
Extent Reports class is used to generate an HTML report on the user-specified path. The Boolean flag indicates if the existing report needs to be overwritten or a new report needs to be created. Value ‘true’ is the default value, which means all the existing data will be overwritten.
Extent Test class is used to log test steps onto the generated HTML report.
The above classes can be used with the frequently used built-in methods that are stated below.
- startTest
- endTest
- Log
- flush
startTest and endTest methods are used to execute preconditions and post-conditions of a test case, while log method is used to log the status of each test step onto the resultant HTML report. Flush method is used to erase any previous data on the report and create a new report.
Test Status can be any of the following values:
- PASS
- FAIL
- SKIP
- INFO
Syntax:
reports.endTest();
test.log(LogStatus.PASS,”Test Passed”);
test.log(LogStatus.FAIL,”Test Failed”);
test.log(LogStatus.SKIP,”Test Skipped”);
test.log(LogStatus.INFO,”Test Info”);
Log method takes in two parameters, the first parameter is the test status and the second parameter is the message to be printed onto the resultant report.
Sample code for Extent Reports
Mentioned below are the sequence of steps to use Extent Reports in Selenium Webdriver in Junit.
Step #1:
Extent Reports can be directly used in selenium WebDriver by importing the JAR file – extentreports-java-2.41.2.jar which can be downloaded here.
Once the ZIP file is downloaded, extract the contents of the ZIP file into a folder.
Step #2:
Add the jar files present in the ZIP file to the project build path using the option Build Path -> Configure Build Path.
Step #3:
Create a new JAVA class with the below code for Extent Reports.
package com.objectrepository.demo; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; public class ExtentDemo { static ExtentTest test; static ExtentReports report; @BeforeClass public static void startTest() { report = new ExtentReports(System.getProperty("user.dir")+"\\ExtentReportResults.html"); test = report.startTest("ExtentDemo"); } @Test public void extentReportsDemo() { System.setProperty("webdriver.chrome.driver", "D:\\SubmittalExchange_TFS\\QA\\Automation\\3rdparty\\chrome\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.google.co.in"); if(driver.getTitle().equals("Google")) { test.log(LogStatus.PASS, "Navigated to the specified URL"); } else { test.log(LogStatus.FAIL, "Test Failed"); } } @AfterClass public static void endTest() { report.endTest(test); report.flush(); } }
Code Explanation
@BeforeClass:
The above code demonstrates the use of Extent Reports. Test case execution begins with the startTest method, which involves initialization of the Extent Reports object. The parameter passed onto the Extent Reports object can be any valid user defined path.
For simplicity sake, the Example uses the current project directory to generate the resultant HTML report. The next statement involves initialization of the ExtentTest object with the return value of the startTest method.
@Test:
Test class includes the following steps:
- Open Chrome browser with this url https://www.google.com
- Validate the page title with the expected value once the page is opened.
- Log the test case status as PASS/FAIL using the log method of Extent Reports.
@AfterClass:
After class includes the code to execute the postconditions of the test case such as ending the test using the endTest method and flushing the report. Please note that the report will not be generated if the flush() method is not used.
Test Summary Report
Pie Chart Graphical Report
Capture Screenshot in Extent Reports
Screen Capture along with test execution will help a tester in debugging the test script if there are any issues encountered during test execution. However, it is advisable to capture screenshot only if a test step fails as the images will consume more memory if captured on every test step.
Screenshots can be captured for each failed step using the below code.
test.log(LogStatus.FAIL,test.addScreenCapture(capture(driver))+ "Test Failed"); public static String capture(WebDriver driver) throws IOException { File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); File Dest = new File("src/../ErrImages/" + System.currentTimeMillis() + ".png"); String errflpath = Dest.getAbsolutePath(); FileUtils.copyFile(scrFile, Dest); return errflpath; }
Code Explanation
Capture Method:
#1) getScreenShotAs() method is used to capture the screenshot of the current WebDriver instance and store it in various output forms.
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
#2) getScreenShotAs method returns a file object which needs to be stored onto a file variable. Please note that casting the web driver instance to Take Screenshot is mandatory if you want to use the method.
#3) File Dest = new File(“src/../ErrImages/” + System.currentTimeMillis()+ “.png”);
#4) The above statement creates a folder named ‘ErrImages’ within the ‘src’ folder and stores the file name as the current system time.
#5) String errflpath = Dest.getAbsolutePath();
FileUtils.copyFile(scrFile, Dest);
returnerrflpath;
#6) The above statements copy the error images to the destination folder.
Log Method:
Log method uses the built-in method, addScreenCapture of Extent Test class to fetch the screenshot and append it to the Extent Report.
test.log(LogStatus.FAIL,test.addScreenCapture(capture(driver))+ “Test Failed”);
Message recorded on the log method can include detailed message including expected and actual results for debugging purpose.
Output:
Conclusion
Extent Reports are one of the best built-in ways to generate customizable HTML reports with a pleasing user interface in Selenium web driver.
It is an open source library that can be easily configured with Selenium, thereby making it the best choice for automation testers.
Hope you would have got a clear idea about Extent Reports now.