In this article, We will learn How to generate Extent Reports in Selenium WebDriver.
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.
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.
Selenium range reports contain two important classes that are commonly used.
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:
The test state can be represented by the following values:
Report methods take into account two parameters. The first is the test status and the second is the message printed in the generated 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(); } }
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:
@AfterClass: Post the condition to run the test case: end the test (using the endTest method) and clear the report.
[SetUpFixture] public abstract class Base { protected ExtentReports _extent; protected ExtentTest _test; [OneTimeSetUp] protected void Setup() { var dir = TestContext.CurrentContext.TestDirectory + "\;
Gaurang Solanki works as a Automation Tester at QACraft. He has done B.Tech in Computer Science & Engineering and has 1.5+ years of experience in Software Testing. In his free time, he loves to explore more to upgrade his technical and testing skills.
© Copyright 2024 QACraft Pvt. Ltd. All rights reserved.
Contact : +91 9157786796
Gaurang