Monday 5 March 2018

Extent Report with Selenium Wrapper Automation

Extent report is very interesting feature in selenium web driver. When we want to create any extent report then we need to create object of 'ExtentReports' and 'ExtentTest' class. We also need to set all the basic information related to test case like test case name, author name, Configuration file etc.

This all actions are basic and common actions for all extent report. So, we can out all this action in one separate class and create separate methods for all the action. It will be very helpful when we required more that one test report in single test case.

Just we need use object of that class and use different methods as per needed. If we declare all the method static then we don't need to create object of that class. we can access all the methods with class name.

If you don't know how to configure Extent Report then go to below URL.
Configuration

I have created one class 'ExtentReportUtility' for selenium wrapper automation.

Inside this class, I have created different methods for create report, set test case, write log for test step etc.

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import com.relevantcodes.extentreports.NetworkMode;


public class ExtentReportUtility 
{
 
 public ExtentReports extentReport;
 public ExtentTest extentTest;
 
 public ExtentReports mSetupExtentReport(String vReportName)
 {
  String vReportPath  = "\\ path of where you want to store report";
  String vConfigPath = "\\ path of extent config file";   
  
  extentReport = new ExtentReports(vReportPath + vReportName + ".html", true, NetworkMode.OFFLINE);
  extentReport.loadConfig(new File(vConfigPath));
  
  return extentReport;
 }
 
 public ExtentTest mSetupTestCase(ExtentReports Report, String vTestcaseName, String vDescription, String vAuthor)
 {
  extentTest = Report.startTest(vTestcaseName, vDescription);
  extentTest.assignAuthor(vAuthor);
    
  return extentTest;
 }
 
 public ExtentTest mWriteLog(ExtentTest Test, String vStatus, String vDescription)
 {
  switch(vStatus.toLowerCase())
  {  
  case "pass":
   
   Test.log(LogStatus.PASS, vDescription);   
   break;
   
  case "fail":
   
   Test.log(LogStatus.FAIL, vDescription);   
   break;
   
  case "error":
   
   Test.log(LogStatus.ERROR, vDescription);   
   break;
   
  case "fatal":
   
   Test.log(LogStatus.FATAL, vDescription);   
   break;
   
  case "info":
   
   Test.log(LogStatus.INFO, vDescription);   
   break;
   
  case "warning":
   
   Test.log(LogStatus.WARNING, vDescription);   
   break;
   
  case "skip":
   
   Test.log(LogStatus.SKIP, vDescription);   
   break;
   
  case "unknown":
   
   Test.log(LogStatus.UNKNOWN, vDescription);   
   break;
  
  }
  
  return Test; 
 }
 
 public ExtentTest mAppendChild(ExtentTest ParentTest, ExtentTest ChildTest)
 {  
  ParentTest.appendChild(ChildTest);
  
  return ParentTest;
 }

}

If you want to see that how to use all methods of these class then I have also created one small class for demo purpose.

Please see below is code. You can see I have used object of 'ExtentReportUtility' class to create report, set up test case and other stuff.


public class ExtentReport 
{
 WebDriver driver = null;
 
 public ExtentReports extentReport;
 public ExtentTest extentTest;
 
 @Test
 public  void mExtentReportDemo() throws Exception
 {
  ExtentReportUtility objExtentReportUtility = new ExtentReportUtility();
  
  extentReport = objExtentReportUtility.mSetupExtentReport("Test Automation Report");
  
  extentTest = objExtentReportUtility.mSetupTestCase(extentReport, "First Test Case", "First Test Case", "Sameer");
    
  GetDriver objGetDriver = new GetDriver();  
  driver = objGetDriver.mGetDriver("chrome", false);
    
  objExtentReportUtility.mWriteLog(extentTest, "pass", "Browser launched successfully.");
  
  
  // Go to Google in Browser
  driver.get("https://www.google.co.in");
  Thread.sleep(2000);
          
  objExtentReportUtility.mWriteLog(extentTest, "pass", "Google is open successfully.");
  
  // Search for Selenium in Google
  driver.findElement(By.id("lst-ib")).sendKeys("test");
  
  // 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())
   {    
    objExtentReportUtility.mWriteLog(extentTest, "pass", "First link is correct.");
   }  
  }
  catch (Exception E)
  {   
   objExtentReportUtility.mWriteLog(extentTest, "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();  
    }

}


Please add comment if you have any question.

No comments:

Post a Comment

Popular