Monday 5 March 2018

Test Result with Extent Report in Selenium Webdriver

Test result report is very important part in selenium automation. Based on test result report we can verify that script is successfully run or we can verify status of verification point.

Below are selenium webdriver reporting tools.
  • TestNG Report
  • JUnit Report
  • Extent Report
In this article, we will see how to generate extent report in selenium webdriver.

Extent report is third party tool which is used for generate user friendly HTML report in selenium web driver. There is two edition for extent report. Community edition is free ware and Pro is not free ware.  

Configuration:

Extent report is depend on testng library. So first of all you have to add testng jar files into your project.

If you are using maven structure then add below dependency into 'pom.xml' file.

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.1.3</version>
</dependency>

You can use latest version from URL Extent Report Maven Dependency.

If you are not using maven structure the you have to add .jar file into build path of your project.

You can download extent report jar file from below URL.
Extent Report

Now you can use Extent Report features into your project for report generations.

There is main two classes for report generation. 'ExtentReports' and 'ExtentTest'.

ExtentReports class is used for create new report and load configuration file. Configuration file is used for set basic configuration of report like theme, headline, data format, time format etc.

ExtentTest class is used for set author name, test type, test step description etc.

Please see below is sample code for how to generate extent report.


import java.io.File;

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeSuite;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import com.relevantcodes.extentreports.NetworkMode;

import CommonUtilities.GetDriver;


public class ExtentReport 
{
 WebDriver driver = null;
 
 public ExtentReports extentReport;
 public ExtentTest extentTest;
 
 @Test
 public  void mExtentReportDemo() throws Exception
 {
  String vReportPath  = "D:\\Study\\Selenium\\Testing\\GitRepository\\SeleniumAutomation\\lib\\TestResult\\TestResult.html";
  String vConfigPath = "D:\\Study\\Selenium\\Testing\\GitRepository\\SeleniumAutomation\\Configs\\extent-config.xml";
  
  extentReport = new ExtentReports(vReportPath, true, NetworkMode.OFFLINE);
  extentReport.loadConfig(new File(vConfigPath));   
  
  extentTest = extentReport.startTest("First Test", "This is First Test");
  extentTest.assignAuthor("Sameer");
  
  GetDriver objGetDriver = new GetDriver();  
  driver = objGetDriver.mGetDriver("chrome", false);
  
  extentTest.log(LogStatus.PASS, "Browser launched successfully.");
  
  // Go to Google in Browser
  driver.get("https://www.google.co.in");
  Thread.sleep(2000);
        
  extentTest.log(LogStatus.PASS, "Google is open successfully.");
  
  // Search for Selenium in Google
  driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
  
  // Click on Search button
  driver.findElement(By.xpath("//input[@value = 'Google Search']")).click();
  Thread.sleep(3000);  // you can use implicit or explicit wait here. It is good practice.
  
  // Verify First link should be 'Selenium - Web Browser Automation'  
  WebElement link = driver.findElement(By.xpath("//a[text() = 'Selenium - Web Browser Automation']"));  
  try
  {
   if(link.isDisplayed())
   {
    extentTest.log(LogStatus.PASS, "First link is correct.");
   }  
  }
  catch (Exception E)
  {
   extentTest.log(LogStatus.FAIL, "First link is not correct.");
  }
    
 }
 
 @After
    public void setupAfterSuite() 
 {
  driver.close();
  driver.quit();
  extentTest.log(LogStatus.PASS, "Browser closed successfully.");
  extentReport.endTest(extentTest);
  extentReport.flush();
  extentReport.close();  
    }

}


Code Explanation : 


public ExtentReports extentReport;
public ExtentTest extentTest;

It is for create object of ExtentReports and ExtentTest class.

extentReport = new ExtentReports(vReportPath, true, NetworkMode.OFFLINE);

vReportPath : It is for set report path at which place you want to generate report.

true : It will replace report if same report is already existing

NetworkMode.OFFLINE : all report artifacts will be stored locally in %reportFolder%/extentreports with the following structure:
- extentreports/css 
- extentreports/js

extentReport.loadConfig(new File(vConfigPath));

It is for loading configuration file. You can download configuration file from below URL.
Extent Report Configuration File




extentTest = extentReport.startTest("First Test", "This is First Test");

It is for create test case.

extentTest.assignAuthor("Sameer");

Set author of test case.

extentTest.log(LogStatus.PASS, "Browser launched successfully.");

It is for add test step into test case with description.

extentReport.endTest(extentTest);

It is for end test and add all log into Report.

extentReport.flush();
extentReport.close();

It is used for close report.

Please see below screen shot for Extent report.



You can see UI of report is very attractive and user friendly. We do not need to record time stamp of any test step. Extent report automatically handle internally and display in the report.

Please add comment if you have any question.

Extent report with Selenium Wrapper Automation.
Extent Report with Selenium Wrapper Automation


No comments:

Post a Comment

Popular