How to Generate Extent Reports in Selenium

In this article, We will learn How to generate Extent Reports in Selenium WebDriver.

What are Extent Reports?

Extent Reports is an open-source reporting library useful for test robotization. It can be fluently integrated with major testing fabrics like JUnit, NUnit, TestNG, etc. These reports are HTML documents that depict results as pie maps. They also allow the generation of custom logs, shots, and other customized details.

Boost Customer Satisfaction, Find out Hidden Bugs In Your Software

Once an automated test script runs successfully, testers need to induce a test prosecution report. While TestNG does give a dereliction report, they don’t give the details.

Using Extent Reports in Selenium

Selenium range reports contain two important classes that are commonly used.

  • 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");

The ExtentReports class generates HTML reports based on the path specified by the verifier. Depending on the boolean flag, an existing report should be overwritten or a new report should be created. “True” is the default, which means that all existing data will be overwritten.

The ExtentTest class saves the test steps to a previously created HTML report.

Both classes can be used in the following integrated ways:

  • startTest: Prerequisites for running the test case.
  • endTest: Fulfill the following conditions of a test case.
  • Log: Record the status of each test step in the HTML report being generated.
  • Flush: Delete all previous data on a relevant report and create a brand new one.

The test state can be represented by the following values:

  • PASS
  • FAIL
  • SKIP
  • INFO

Syntax:

extent reports in selenium

Report methods take into account two parameters. The first is the test status and the second is the message printed in the generated report.

How to generate Extent Reports in Selenium?

  1. Import the JAR file: degreereports-java-2.1.2.jar. After downloading the ZIP file, extract its contents to a folder.
  2. Add the JAR file to the build path of the project using the Build Path -> Set Build Path option.
  3. Create a new JAVA class for Scope Report with the following code.
package com.browserstack.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_TFSQAAutomation3rdpartychromechromedriver.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();
}
}

Runs from the startTest method. It also initializes the Scope Reports object. Any valid custom path can be passed as a parameter to the Scope Reports object.

@Test: This class automatically does the following:

  • Open the Chrome browser with the URL https://www.google.com.
  • When you open the page, check that the page title is as expected.
  • Record the test case status as PASS/FAIL using the logging method described above.

@AfterClass: Post the condition to run the test case: end the test (using the endTest method) and clear the report.

How to generate Extent Reports in Selenium using NUnit?

Using setup fixture:

[SetUpFixture]
public abstract class Base
{
protected ExtentReports _extent;
protected ExtentTest _test;

[OneTimeSetUp]
protected void Setup()
{
var dir = TestContext.CurrentContext.TestDirectory + "\\";
var fileName = this.GetType().ToString() + ".html";
var htmlReporter = new ExtentHtmlReporter(dir + fileName);

_extent = new ExtentReports();
_extent.AttachReporter(htmlReporter);
}

[OneTimeTearDown]
protected void TearDown()
{
_extent.Flush();
}

[TestFixture]
public class TestInitializeWithNullValues : Base
{
[Test]
public void TestNameNull()
{
Assert.Throws(() => testNameNull());
}
}

[SetUp]
public void BeforeTest()
{
_test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
}

[TearDown]
public void AfterTest()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
? ""
: string.Format("{0}", TestContext.CurrentContext.Result.StackTrace);
Status logstatus;

switch (status)
{
case TestStatus.Failed:
logstatus = Status.Fail;
break;
case TestStatus.Inconclusive:
logstatus = Status.Warning;
break;
case TestStatus.Skipped:
logstatus = Status.Skip;
break;
default:
logstatus = Status.Pass;
break;
}

_test.Log(logstatus, "Test ended with " + logstatus + stacktrace);
_extent.Flush();
}
}

How to capture screenshots in Extent Report?

By taking screenshots, testers can better identify what went wrong when the software malfunctioned during the test. Take screenshots only if the test fails, as they consume a lot of memory.

Try taking screenshots with the code below:

how to capture screenshot in extent report

getScreenShotAs(): Takes a screenshot of the current WebDriver instance and saves it in various output formats.

getScreenShotAs

This method returns a file object that is stored in the file variable. The method requires passing an instance of the web driver to the Take Screenshot function.

file dest screenshot here

This statement creates a folder named “BStackImages” in the “src” folder and stores the file name according to the current system time.

bstackimage

These statements copy all error images to the target directory.

Use the log method because it uses the addScreenCapture method of the Extent Test class to capture the screenshot and add it to the extent report.

addscreencapture method

Benefits of using Extent Reports

  • They can be integrated with TestNG and JUnit.
  • If needed, screenshots can be captured and displayed for each of the steps of the test.
  • They allow testers to track multiple test cases in a single test suite.
  • They indicate the time required to complete the test.
  • They can be customized to graphically represent each stage of the test.

Read Also:

1) 11 Best Selenium Alternatives

2) Selenium 4 Relative Locators

3) CSS Selectors in Selenium

4) TestNG in Selenium – A Brief Guide

5) Selenium 3 Vs Selenium 4 – What’s the Difference

Preeti Singh

Preeti works as a QA expert in Salesforce and other commerce and E-learning at QACraft. She is a computer science engineer with a degree and has 3 years of experience in Manual testing as well as Automation testing. In her free time, she loves to sing.

https://qacraft.com/author/preeti-singh/