Extent Reports provides the interactive reports and has more user-friendly UI for displaying the test cases report in selenium. TestNg generates the default report in selenium for the test cases whether they are passes or not . But TestNg report are not that much interactive and not that user friendly. Extent reports are introduced and work with TestNg framework to generate the interactive reports , generate PIE charts showing the percentages of test case fail or passed. Extent reports generated in HTML form and can be viewed in the browsers , it keep the logs for the debugging of failed test cases and also captures the screen shot within the report itself.
Steps to create the Extent Report
Configure the Extent report Jar in your project
1) If you are working in maven project then go the this mvn repository link.
2) Click on the latest version( 2.41.1 )
3) Click on Jar and download the jar file and configure to your project . Read how to configure the jar file in eclipse project to configure the extent report jar file.
4) If you are working in maven project then copy the dependency as in screen shot to POM.xml file and update the project.
<dependency> <groupId>com.relevantcodes</groupId> <artifactId>extentreports</artifactId> <version>2.41.2</version> </dependency>
Now we have configured the Extent report jar in our project . Now we will create the two classes in our project – library.java and TestCases.Java.
In library.Java – define the function to capture the screen shot and return the absolute path to attach the screenshot in extent report. Comments are provided in below program for clear understanding.
package com.extentreport; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; public class Library { // this function can be used to take screen shot // by passing the driver as a parameter public static String get_screen_shot(WebDriver driver, String screenshot_name) { // Convert webdriver object to Takescreenshot TakesScreenshot ts = ((TakesScreenshot)driver); // call get screen shot method and pass the parameter as file File src= ts.getScreenshotAs(OutputType.FILE); // date time stamp for unique screen shot name in milliseconds DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy hh-mm-ss.SSS"); Date date = new Date(); String file_path = ".\\ScreenShots\\" + screenshot_name + dateFormat.format(date) + ".png"; File destination_file = new File(file_path) ; try { // now copy the screenshot to desired location using copyFile // we used relative path for screen shot FileUtils.copyFile(src, destination_file); } catch (IOException e) { System.out.println(e.getMessage()); } // returning the absolute file path for extent reports // to attach the screen shot in extent report return destination_file.getAbsolutePath(); } }
TestCase.Java – we have 3 test cases in this class . Test case1 will pass , Test case 2 will fail , Test case 3 will skipped.
Comments are provided in program for each statement. Logger is used to create logs in extent report.
package com.extentreport; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.ITestResult; import org.testng.SkipException; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; public class TestCases { // create extent report reference ExtentReports report; // create extent test reference ExtentTest logger; // create webdriver reference WebDriver driver; // it will execute before class @BeforeClass public void open_Browser() { // creating extent report object and passing path where extent report should generate // relative path is passed here report = new ExtentReports(".\\Reports\\extent-report.html", true); // opening fire fox browser System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); driver=new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } // Test case 1 is passed @Test public void VerifyGoogleTitle() { // this statement is used to create logs to start test and is must logger=report.startTest("verify google title"); // adding the information logs logger.log(LogStatus.INFO,"Google Url launched"); // launched google url driver.get("https://www.google.co.in/"); // actual title is google String Actual_Title = driver.getTitle(); // expected title is google String Expected_Title = "Google"; // test case is passes actual = expected Assert.assertEquals(Actual_Title, Expected_Title); //adding the information logs to report logger.log(LogStatus.PASS, "Test case is passed and Title verified"); } // This test case will fail @Test public void Verify_Yahoo_Title() { // this statement is used to create logs to start test logger=report.startTest("verify yahoo title"); // adding the information logs logger.log(LogStatus.INFO,"yahoo Url launched"); // launched yahoo url driver.get("https://in.yahoo.com/"); // actual title is yahoo String actual_Title= driver.getTitle(); // expected title is google String expected_Title ="Google"; // this test case will fail as expected is google and actual is yahoo Assert.assertEquals(actual_Title, expected_Title); //this log will not execute as test case fails this time logger.log(LogStatus.PASS, "Test case is passed and Title verified"); } // This test case is skipped @Test public void Verify_skip_Title() { // this statement is used to create logs to start test logger=report.startTest("verify skip title"); // adding the information logs logger.log(LogStatus.SKIP, "Test case is skipped"); // skipping the test case throw new SkipException("Test case is skipped"); } // This method will run after every method @AfterMethod public void Check_status(ITestResult result) { // fetching the fail result status with ItestResult // it will execute only if test case fails if (result.getStatus()==ITestResult.FAILURE) { // getting the screen shot path from get_screen_shot function defined in library.java String screenshot_path = Library.get_screen_shot(driver, result.getName()); // This function is used to fetch html image tag for the extent report String image= logger.addScreenCapture(screenshot_path); // informational log with adding screen shot as test case fail logger.log(LogStatus.FAIL, "Title verification", image); } // it will execute whether test fails or pass // it is to end the log report report.endTest(logger); } // This test case is executed after class @AfterClass public void Close_Browser() { // close the browser driver.quit(); // destroy report object report.flush(); } }
Run the TestNg.xml File
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="com.extentreport.TestCases"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
Below is the Project screen shot after running the above project.
Below is the extent report of the above test case
Note : If you get this error “java.lang.NoClassDefFoundError: freemarker/template/TemplateModelException”then download free maker jar here and configure it and if still it is giving some other error then downgrade the extent report version and try.