How to Generate Extent Reports in Selenium WebDriver
In this article, We will learn How to generate Extent Reports in Selenium WebDriver. For more, read the full article now.
Join the DZone community and get the full member experience.
Join For FreeWe will learn how to generate Extent Reports in Selenium WebDriver in this tutorial.
What Exactly Are Extent Reports?
Extent Reports is an open-source reporting framework that can be used for test automation. It may be seamlessly connected with popular testing fabrics like JUnit, NUnit, TestNG, and others. These reports are HTML documents with pie maps that show the results. They also enable the creation of customized logs, pictures, and other data.
When an automated test script completes successfully, testers must generate a test prosecution report. While TestNG provides a dereliction report, it does not provide information.
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 route supplied by the verifier. Depending on the boolean flag, an existing report should be rewritten, or a new report should be prepared. "True" is the default, which means that all existing data will be rewritten.
The ExtentTest class saves the test steps to a previously produced HTML report.
Both classes can be combined in the following ways:
- startTest: Prerequisites for starting the test case.
- endTest: Meet the following test case conditions.
- Log: Record the status of each test step in the HTML report that is being generated.
- Flush: Delete all prior data from a relevant report and produce a fresh one.
The test state can be represented by the following values:
- PASS
- FAIL
- SKIP
- INFO
Syntax:
Two parameters are taken into account by report techniques. The first is the status of the test, and the second is the message displayed in the created report.
How to Generate Extent Reports in Selenium?
- Import the JAR file degreereports-java-2.1.2.jar. After downloading the ZIP file, extract its contents to a folder.
- Add the JAR file to the project's build path by selecting Build Path -> Set Build Path.
- Use the following code to create a new JAVA class for Scope Report.
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();
}
}
Starts the test with the smartest method. It also sets up the Scope Reports object. Any valid custom route can be supplied to the Scope Reports object as a parameter.
@Test: This class automatically does the following:
- Open the Chrome browser with the URL.
- 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?
Taking screenshots allows testers to better determine what went wrong when the software failed during the test. Take screenshots only if the test fails, as they consume a lot of memory.
Try taking screenshots with the code below:
getScreenShotAs(): This method captures a screenshot of the current WebDriver instance and saves it in a variety of output formats.
This method produces a file object, which is saved in the file variable. The method necessitates giving a web driver instance to the Take Screenshot function.
This statement creates a folder called "BStackImages" in the "src" folder and names the files based on the current system time.
All error pictures are copied to the target directory by these statements.
Use the log method because it captures the screenshot and adds it to the extent report using the Extent Test class's addScreenCapture method.
Advantages of Using Extent Reports:
- They are compatible with TestNG and JUnit.
- Screenshots of each phase of the test can be recorded and shown if necessary.
- They enable testers to keep track of various test cases within a single test suite.
- They represent the amount of time needed to complete the test.
- They can be adjusted to depict each level of the test graphically.
Published at DZone with permission of Preeti Singh. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments