Wednesday, September 12, 2018

How to add screenshot in extent report using Selenium

Hello Techienthu! In this post, I would like to explain how to append a screenshot into Extent Report using Selenium


If you are new to Extent Reports, Please go through this Link. Then follow this post.

In Extent reports, there we have a method which accepts a screenshot link and that method can append that image into final report.

Below is a sample code to generate a report with two suites, one testcase under each suite and all available log statuses in extent reports


import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class ExtentDemo {
 static WebDriver driver = null;
 static ExtentReports extent = new ExtentReports("d://report.html",true);
 static ExtentTest suiteName = null;
 static ExtentTest testName = null;
 public static void main(String h[]){
  driver = new FirefoxDriver();
  extent.loadConfig(new File("extent-config.xml"));
  suiteName = extent.startTest("SuiteName1");
  testName = extent.startTest("Testcase name1");
  suiteName.appendChild(testName);
  driver.get("http://www.google.com/");
  testName.log(LogStatus.PASS, testName.addScreenCapture(captureScreenShot()));
  driver.close();
  extent.endTest(suiteName);
  extent.flush();

  driver = new FirefoxDriver();
  suiteName = extent.startTest("SuiteName2");
  testName = extent.startTest("Testcase name2");
  suiteName.appendChild(testName);
  driver.navigate().to("http://www.google.com/");
  testName.log(LogStatus.PASS, testName.addScreenCapture(captureScreenShot()));
  driver.quit();
  extent.endTest(suiteName);
  extent.flush();
  extent.close();
 }

 static String captureScreenShot(){
  try{
   String filePath = "d://"+System.currentTimeMillis()+".png";
   TakesScreenshot screen = (TakesScreenshot)driver;
   File screenShot = screen.getScreenshotAs(OutputType.FILE);
   FileUtils.copyFile(screenShot, new File(filePath));
   return filePath;
  }
  catch(IOException e){

  }
  return null;
 }

}

Explanation for the above code

static String captureScreenShot(){
  try{
   String filePath = "d://"+System.currentTimeMillis()+".png";
   TakesScreenshot screen = (TakesScreenshot)driver;
   File screenShot = screen.getScreenshotAs(OutputType.FILE);
   FileUtils.copyFile(screenShot, new File(filePath));
   return filePath;
  }
  catch(IOException e){

  }
  return null;
 }
This method is from Selenium API and available in TakeScreenShot interface. The above method returns the screenshot file path, we use that path in below mentioned extent report method.
testName.log(LogStatus.PASS, testName.addScreenCapture(captureScreenShot()));
This method is used to append screenshot in Extent Report log file.
  • log status || PASS/FAIL/FATAL/SKIP/INFO/WARNING
  • screenshot path and name

Sample Extent Report which is generated by above code


No comments:

Post a Comment