Thursday, September 19, 2019

Post#75.Extent Report version 3


Generate Extent Reports in Selenium WebDriver:

Selenium provides inbuilt reports using frameworks such as JUnit and TestNG.
Although the built-in reports provide information on the steps that are executed as part of the test case, they need more customization to be shared with all the major project stakeholders.

Extent Reports is a customizable HTML report which can be integrated into Selenium WebDriver using JUnit and TestNG frameworks.
Below is the snapshot of a sample Extent Report in Pie Chart Representation



Advantages of using Extent Reports

There are several advantages of Extent Reports and few of them are discussed below.
  • Customizable HTML report with stepwise and pie chart representation.
  • Displays the time taken for test case execution within the report.
  • Each test step can be associated with a screenshot.
  • Multiple test case runs within a single suite can be tracked easily.
  • Can be easily integrated with TestNG and JUnit frameworks.

Using Extent Reports version 3 in Selenium WebDriver

Extent Reports contain three major classes that are used frequently.
1.      ExtentHtmlReporter class
2.      ExtentReports class
3.      ExtentTest class

ExtentHtmlReporter class is used to generate an HTML report on the user-specified path. 
Example:
// initialize the HtmlReporter
ExtentHtmlReporter reporter = new ExtentHtmlReporter(System.getProperty("user.dir")
+"/TestResults/ExtentReport.html");

ExtentReports class is used to starting reporters and building reports. (Creating Tests)
Example:
// initialize ExtentReports, attach the HtmlReporter and create Test
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
extent.createTest("Test Case 001");
The above createTest() method will create a Test named 'Test Case 001' and will return a ExtentTest object. So you can write above statement as
ExtentTest test = extent.createTest("Test Case 001");
ExtentTest class is used to add steps in the created Test. Using the ExtenTest reference variable (test) you can add steps for the Test. The ExtentTest class can be used with the frequently used built-in methods that are stated below.
·       assignCategory
·       Log
assignCategory()  method is used to add a category to your Test. For example you can add Smoke, regression etc.
Example: test.assignCategory("Regression");
Log() method is used to log the status of each test step onto the resultant HTML report.
Test Status can be any of the following values:
·       PASS
·       FAIL
·       SKIP
·       INFO
Example:
test.log(Status.PASS, "userName Entered successfully");
test.log(Status.PASS, "password Entered successfully");
test.log(Status.PASS, "Clicked on Login button successfully");

After adding Tests and Steps you can write all the information in the HTML file by using flush() method of ExtentReports class.

Example: extent.flush();  This will writes everything to the HTML report file.

Steps to Generate Extent Report with Selenium Webdriver

Mentioned below are the sequence of steps to use Extent Reports in Selenium Webdriver

Step#1 Start browser and Navigate to http://extentreports.com/  and Click on Community Edition
Step#2 Click on version 3

Step#3 Use Maven Dependency- Suggested way to use
If you are using normal Java project then you can download the jar manually and add in project.
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.1.5</version>
</dependency>

Step#4  Create Java Program which will generate report.

package pack2;
import java.io.IOException;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
public class ExtentReportEx{
             
               public static void main(String[] args) throws IOException {
                              
                             // initialize the HtmlReporter
                              ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/TestResults/ExtentReportEx.html");
                              htmlReporter.config().setDocumentTitle("Automation Execution Report");
                              htmlReporter.config().setReportName("End to End Test");
                              
                              // initialize ExtentReports and attach the HtmlReporter
                              ExtentReports extent = new ExtentReports();
                              extent.setSystemInfo("OS", "Windows");
                              extent.attachReporter(htmlReporter);
                              
                             // creating tests
                              ExtentTest test = extent.createTest("Test Case 001");
                              
                              //Assign Categories to Tests
                              
                              test.assignCategory("Regression");
                              test.log(Status.PASS, "userName Entered successfully");
                              test.log(Status.PASS, "password Entered successfully");
                              //Adding Screenshot
                              test.log(Status.INFO, "Login Credentials Entered Successfully",MediaEntityBuilder.createScreenCaptureFromPath("/Users/sujoy/Desktop/logo.jpg").build());

                              test.log(Status.PASS, "Clicked on Login button successfully");
                            
                              // calling flush writes everything to the log file
                            extent.flush();
                              System.out.println("Done !!!");
               }

}

Above code will generate the report in specific directory.


No comments:

Post a Comment