[go: up one dir, main page]

0% found this document useful (0 votes)
116 views5 pages

How To Generate Extent Reports in Selenium Webdriver

The document discusses how to generate extent reports in Selenium WebDriver. Extent reports provide a customizable and graphical way to generate detailed test reports in Selenium. The key steps include: 1. Adding the ExtentReports jar files to the project 2. Creating a test class and initializing an ExtentReports object to specify the report file path 3. Using ExtentTest to log test steps, results and screenshots to the report 4. Closing the report once all tests have finished to generate the HTML file

Uploaded by

sandeep kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views5 pages

How To Generate Extent Reports in Selenium Webdriver

The document discusses how to generate extent reports in Selenium WebDriver. Extent reports provide a customizable and graphical way to generate detailed test reports in Selenium. The key steps include: 1. Adding the ExtentReports jar files to the project 2. Creating a test class and initializing an ExtentReports object to specify the report file path 3. Using ExtentTest to log test steps, results and screenshots to the report 4. Closing the report once all tests have finished to generate the HTML file

Uploaded by

sandeep kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

How to Generate Extent Reports in Selenium Webdriver

-Graphical Representation of Report .

Beautiful way for generating report.

Example-

What are the Extent Reports?


ExtentReports is an open-source reporting library used in selenium test automation. Extent
reports become the first choice of Selenium Automation Testers, even though Selenium comes with
inbuilt reports using frameworks like JUnit and TestNG. With extent reports, you can offer a more
extensive and insightful perspective on the execution of your automation scripts. So let’s see why
Automation testers prefer Extent reports to others.

Advantages of Extent Reports:


Some of the advantages of Extent Reports are as follows

 Extent reports are more customizable than others


 Extent API can produce more interactive reports, a dashboard view, graphical view, capture
screenshots at every test step, and emailable reports
 It can be easily integrated with frameworks like JUnit, NUnit, & TestNG
 It displays the time taken for test case execution

Generating Extent Reports In Selenium WebDriver


The main scope of this article is to show the Extent Reports. This is the most popular and widely used
Selenium Reporting tool in the current market.

Pre-requisites to Generate Extent Reports:

1. Java should be installed – Install and setup Java


2. TestNG should be installed – Install TestNG
3. Extent Report Jars (Version 2.41.2) – Download or if using maven then add Extent report dependency
in POM
4. extent-config.xml – It allows to configure HTML Report
Steps To Generate Extent Reports:

1. Firstly, create a TestNG project in eclipse


2. Add the downloaded library files to your project
3. Create a java class say ‘ExtentReportsClass’ and add the following code to it

Code Explanation:

i. Imported two classes ExtentReports and ExtentTest.

ExtentReports: By using this class we set the path where our reports need to
generate.

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

ii. Took three methods with @Test annotation such


as passTest, failTest and skipTest and a method startTest with @BeforeTest
annotation and another method endReport with @AfterMethod annotation

Here my intention is to generate a report with all the three types of results
such as Pass, Fail and Skip.

iii. Used object of ExtentReports class (i.e., extent) in the startReport method


which was assigned to @BeforeTest annotation to generate the HTML report in
the required path

iv. Used object of ExtentTest class (i.e., logger) in the remaining methods to


write logs in the report.

v. Used ITestResult class in the @AfterMethod to describes the result of a test.

Program---
import java.io.File;
 
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
 
public class ExtentReportsClass{
ExtentReports extent;
ExtentTest logger;
@BeforeTest
public void startReport(){
//ExtentReports(String filePath,Boolean replaceExisting)
//filepath - path of the file, in .htm or .html format - path where your report needs to generate.
//replaceExisting - Setting to overwrite (TRUE) the existing file or append to it
//True (default): the file will be replaced with brand new markup, and all existing data will be lost. Use this option to create a
brand new report
//False: existing data will remain, new tests will be appended to the existing report. If the the supplied path does not exist, a
new file will be created.
extent = new ExtentReports (System.getProperty("user.dir") +"/test-output/STMExtentReport.html", true);
//extent.addSystemInfo("Environment","Environment Name")
extent
                .addSystemInfo("Host Name", "SoftwareTestingMaterial")
                .addSystemInfo("Environment", "Automation Testing")
                .addSystemInfo("User Name", "Rajkumar SM");
                //loading the external xml file (i.e., extent-config.xml) which was placed under the base directory
                //You could find the xml file below. Create xml file in your project and copy past the code mentioned below
                extent.loadConfig(new File(System.getProperty("user.dir")+"\\extent-config.xml"));
}
@Test
public void passTest(){
//extent.startTest("TestCaseName", "Description")
//TestCaseName – Name of the test
//Description – Description of the test
//Starting test
logger = extent.startTest("passTest");
Assert.assertTrue(true);
//To generate the log when the test case is passed
logger.log(LogStatus.PASS, "Test Case Passed is passTest");
}
@Test
public void failTest(){
logger = extent.startTest("failTest");
Assert.assertTrue(false);
logger.log(LogStatus.PASS, "Test Case (failTest) Status is passed");
}
@Test
public void skipTest(){
logger = extent.startTest("skipTest");
throw new SkipException("Skipping - This is not ready for testing ");
}
@AfterMethod
public void getResult(ITestResult result){
if(result.getStatus() == ITestResult.FAILURE){
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
}else if(result.getStatus() == ITestResult.SKIP){
logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
}
// ending test
//endTest(logger) : It ends the current test and prepares to create HTML report
extent.endTest(logger);
}
@AfterTest
public void endReport(){
// writing everything to document
//flush() - to write or update test information to your report.
                extent.flush();
                //Call close() at the very end of your session to clear all resources.
                //If any of your test ended abruptly causing any side-affects (not all logs sent to ExtentReports, information
missing), this method will ensure that the test is still appended to the report with a warning message.
                //You should call close() only once, at the very end (in @AfterSuite for example) as it closes the underlying
stream.
                //Once this method is called, calling any Extent method will throw an error.
                //close() - To close all the operation
                extent.close();
    }
}

We have to Create extent-config.xml file .

We can copy contains of above xml file from –

MavenDependencies->extentsReports -2.4.2.jars-
>com.relevantcodes.extentreports.resource-> extent-config.xml

By using this external XML file (extent-config.xml), we could change the details
such as Report Theme (either standard or dark), Report Title, Document Title
etc.,

Console Output:

1===============================================
2Default suite
3Total tests run: 3, Failures: 1, Skips: 1
4===============================================
Refresh the project after execution of above ExtentReportsClass.java file. You could find an HTML file
named “STMExtentReport.html” in your test-output folder. Copy the location of the
STMExtentReport.html file and open it by using any browser. Once you open the report, you will see
beautiful high rich HTML test results as shown below.

You might also like