Thursday 22 February 2018

Take Screenshot in Selenium

Take screenshot in selenium is very important for test result and bug verification.

It is very simple to take screen shot at particular moment like error page, defect etc.

First of all you have import below packages into your code.

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

Now you have to create below function for take screen shot.

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;


public class TakeScreenshot
{
 public static void mTakeScreenShot(WebDriver driver, String vFileName)
 {
  try
  {
   TakesScreenshot screenShot = ((TakesScreenshot)driver);
   File srcFile = screenShot.getScreenshotAs(OutputType.FILE);
   FileUtils.copyFile(srcFile, new File("lib\\TestResult\\" + vFileName + ".png"));
   
  }
  catch(Exception E)
  {
   System.out.println("Error in take screen shot");
  }
 }

}

For highlighted part, you have to specify path to where you want to save screenshot.

Please share your inputs for this post.

No comments:

Post a Comment

Popular