Friday, 6 January 2017

How to Capture ScreenShot with Selenium WebDriver

Taking screenshot while testing is very important for automation so that you can point out and catch the exact bug. In this post we will learn about how to take screen shot when selenium test case fails. It is more important to have a screenshot when the selenium test fail but you can add the code if you want to capture screen shot when selenium test passes.
Once you know how to use it then you can use it anywhere you want. So lets get started . . .

Take ScreenShot with Selenium WebDriver

Video Tutorial Is Also Available -




import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

public class ScreenshootGoogle {

 public void TestJavaS1()
// Open chrome browser
 System.setProperty("", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();

// Maximize the window

// Pass the url

// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/error.png"));

catch (IOException e)



  1. Hello,
    The Article on Take ScreenShot with Selenium WebDriver is informative give detail information about it.Thanks for Sharing the information about program taking a ScreenShot with Selenium WebDriver. Software Testing Services

  2. This comment has been removed by a blog administrator.

  3. Nice information Thanks a lot for such a great information.