Selenium Webdriver Tutorial To Capture Screenshot With Example

Capturing screenshot of software web application page is very easy in selenium webdriver. As we knows, It is very basic required thing in software automation tools to capture screenshot on test case failure or whenever required during test case execution. VIEW MORE SELENIUM WEBDRIVER BASIC COMMANDS for your webdriver knowledge improvement and use them in your software web application test case creation.

If you remember, We can use “captureEntirePageScreenshot” command in selenium IDE to capture the screenshot on software web application current page. In Selenium WebDriver/Selenium 2, we need to capture screenshot of web page using bellow syntax.
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Then we have to store it in our local drive using bellow given syntax. You can change/provide your own file destination path and name.
FileUtils.copyFile(screenshot, new File("D:\screenshot.jpg"));

Now we need to import bellow given header files to get support of capture screenshot in webdriver.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

Copy bellow given @Test method part of Capture screen shot webdriver example and replace it with the @Test method part of example given on THIS PAGE. Don’t forget to import above given header files in your eclipse test.(Note : @Test method is marked with pink color in that linked page).

@Test
 public void test () throws InterruptedException, IOException 
 { 
  //Capture entire page screenshot and then store it to destination drive
  File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(screenshot, new File("D:\screenshot.jpg"));
  System.out.print("Screenshot is captured and stored in your D: Drive");
 }

You can put above syntax under catch block of  try to capture screenshot on failure. You can view THIS POST If you wants to record video for your selenium webdriver software test execution.

12 thoughts on “Selenium Webdriver Tutorial To Capture Screenshot With Example

  1. How can i make my program do multiple screenshots?
    Doing this example, is replacing image in specific directory.
    How to made it to do next file each time some event happens?

  2. Is there any way to make captured screenshots as separate jpg's?

    I'm making big test with possibility of many failures, and i want screenshot of every exception.(Junit)
    For now it is replacing existing screenshots.

Leave a Reply

Your email address will not be published. Required fields are marked *