Generate Extent Report in Selenium WebDriver

In this article, we see one of the most Selenium Reporting Tool (Extent Report). This report shows more customization to be shared with all the major project stakeholders. It is open source library for rich HTML reporting for TestNG Tests.

In this article, we see one of the most Selenium Reporting Tool (Extent Report). This report shows more customization to be shared with all the major project stakeholders. It is open source library for rich HTML reporting for TestNG Tests.

This tutorial will give you complete step by step process to How to Generate Extent Report in Selenium WebDriver.

You can see in below image, extent report look. It shows the pie chart representation, test stepwise report generation, adding a screenshot of failed test cases etc.

Extent report in Selenium

Extent Report Classes: Extent report uses three major classes that are used frequently.

ExtentReports: By using this class, we will collect the data which require to generate the report.

ExtentHtmlReporter: It generates the HTML type of file and collect the data and to set the path of where we want to generate the report.

ExtentTest: By using this class we will generate the logs in the report.

Configure Extent Report:

To configure the extent report in your Project. You need to follow the below mentioned steps:

  1. Download Extent  Report Jars (Click Here to Download)
  2. Add Extent Report Jars in your Test Project.
  3. Design Test using TestNG Framework.

Code:

package extentRepo;

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
//import com.utility.CaptureScreenShot;

public class ExtentReport {

            ExtentReports reports;
            ExtentHtmlReporter htmlReporter;
            ExtentTest test;
            WebDriver driver;

            @BeforeTest
            public void startTest() {
                        reports = new ExtentReports();

                        htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") + "//test-output//Extentreport.html");
                        reports.attachReporter(htmlReporter);

                        reports.setSystemInfo("Machine", "Ajeet-772G");
                        reports.setSystemInfo("Env", "DevOps");
                        reports.setSystemInfo("Build", "Integration");
                        reports.setSystemInfo("Browser", "IE");
            }

            @BeforeMethod
            public void openApplication() {
                        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Jars\\chromedriver_win32\\chromedriver.exe");
                        driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        driver.get("https://www.facebook.com");
            }

            @Test
            public void verifyTittleTest() {
                        test = reports.createTest("verifyTittleTest");
                        String expetedTitle = "Facebook – log in or sign up";
                        String pageTitle = driver.getTitle();
                        Assert.assertEquals(pageTitle, expetedTitle);
            }

            @Test
            public void fillRegistrationformTest() {
                        test = reports.createTest("fillRegistrationformTest");

                        WebElement firstName = driver.findElement(By.cssSelector("input[id='u_0_2']"));
                        WebElement lastName = driver.findElement(By.name("lastname"));
                        WebElement submitButton = driver.findElement(By.id("u_0_m"));

                        firstName.sendKeys("Ajeet");
                        lastName.sendKeys("Maurya");
                        submitButton.click();
                        String expectedTitle = "FaceBook Home";
                        String pageTitle = driver.getTitle();
                        Assert.assertEquals(pageTitle, expectedTitle);
            }

            @AfterMethod
            public void setTestResult(ITestResult result) throws IOException {

                        //String screenShot = CaptureScreenShot.captureScreen(driver);

                        if (result.getStatus() == ITestResult.FAILURE) {
                                    test.log(Status.FAIL, result.getName());
                                    test.log(Status.FAIL,result.getThrowable());
                        //          test.fail("Screen Shot : " + test.addScreenCaptureFromPath(screenShot));
                        } else if (result.getStatus() == ITestResult.SUCCESS) {
                                    test.log(Status.PASS, result.getName());
                                    //test.pass("Screen Shot : " + test.addScreenCaptureFromPath(screenShot));
                        } else if (result.getStatus() == ITestResult.SKIP) {
                                    test.skip("Test Case : " + result.getName() + " has been skipped");
                        }

                        reports.flush();
                        driver.close();
            }

            @AfterTest
            public void endTest() {
                       
                        reports.flush();
            }

}

Code Explanation:

Import three classes ExtentReports, ExtentHtmlReporter and ExtentTest.

  • ExtentReports: By using this class, we will collect the data which require to generate the report.
  • ExtentHtmlReporter: It generates the HTML type of file and collect the data and to set the path of where we want to generate the report.
  • ExtentTest: By using this class we will generate the logs in the report.

 

We are using different TestNG annotations like @BeforeTest, @BeforeMethod, @Test, @AfterMethod, @AfterTest to write the test cases.

  • @BeforeTest: We set the path of the report where we want to store report and set the system information.
  • @BeforeMethod: We write the code of open the browser and navigate on given URL.
  • @Test: Create the test to insert into the report and write actual test.
  • @AfterTest: In this, we check the result of the test case and describe this result into the report by using ITestResult class.

So in this tutorial, you have learned How to generate Extent Report in Selenium WebDriver. I hope you have cleared about Extent Report. If you have any question please comment on this post.

 

I hope this tutorial will be helpful for you. Happy Learning 🙂

Leave a Comment