Taking Screenshot using Webdriver

Its very important to take screenshot when we execute a test script. When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.

It helps us to debug and identify the problem by seeing the screen shot.

In selenium webdriver, we can take the screen shot using the below command.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

NOTE: - If you are using latest version of selenium, please check this updated article Take Screenshot using FileHandler class

Check the framework example of Taking ScreenShot for ONLY Failed Tests using TestNG
The below example explains how to take the screen shot when the test fails.

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
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 takeScreenShotExample{
        public WebDriver driver;
         
      @Test
      public void openBrowser() throws Exception {
    	  driver = new FirefoxDriver();
    	  driver.manage().window().maximize();
    	  driver.get("http://www.google.com");
    	  try{
                //the below statement will throw an exception as the element is not found, Catch block will get executed and takes the screenshot.
    		  driver.findElement(By.id("testing")).sendKeys("test");
                 
                   //if we remove the below comment, it will not return exception and screen shot method will not get executed.
    		  //driver.findElement(By.id("gbqfq")).sendKeys("test");
    	  }
    	  catch (Exception e){
    		  System.out.println("I'm in exception");
//calls the method to take the screenshot.
    		  getscreenshot();
     	  }
      }
      
      public void getscreenshot() throws Exception 
      {
              File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
           //The below method will save the screen shot in d drive with name "screenshot.png"
              FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
      }
 }
Selenium Tutorials: 

Comments

Probably you were importing FileUtils from wrong package.
Import it from:

import org.apache.commons.io.FileUtils;

Brooo That'sYou Imported another Package.class jus Check The Class And

import org.apache.commons.io.FileUtils; Import This 1 again prev u Imported Wrong 1 Im Sure.

For this problem : FileUtils.copyFile(scrFile, new File("D:\\screenshot.png")); Here i got an error saying that copyFile is undefined by FileUtils.

You need to just import the : import org.apache.commons.io.FileUtils;

Bro if I import as you shown above, again it is showing the same error

Hi,
Can I take screenshot of a particular web page after logging into a website? Can I also send it as an email to a user?

hello,
I want to know how to capture screenshot in excel sheet.

was very helpfull

I want to capture scrrenshot before clicking on submit button & new page that appears after clicking on submit button, currently it captures scrreenshot of before clicking on submit button

You should call the function before and after submit button and also rename the file each time . As it may replace the old one

In Selenium it is not allowing to enter Outputtype.FILE throwing error.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.