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 comments:

  1. can you give me syntax in case of failure?

    ReplyDelete
    Replies
    1. Hi Arpita use TestNG Listener using this if tc fails it will take screenshot automatically

      Delete
    2. Thanks Mukesh otwani , really hepls your tutorials.

      Delete
    3. Mukesh Can plz Post How design effective key driven Framework using selenium web driver?

      Delete
  2. so fantastic and helpful ........................
    Very appreciating
    Thanks

    ReplyDelete
  3. can you give me syntax in case of failure?


    using junit

    ReplyDelete
  4. Its giving error for FileUtils say method is undefined

    ReplyDelete
  5. use import org.apache.commons.io.FileUtils;

    ReplyDelete
  6. Thanks .. how can we take full page screenshot... i mean without scroll full page screenshot

    ReplyDelete
  7. Any tutorial on capturing full page (with scrolling) screenshot? not just the visible part screenshot

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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?

    ReplyDelete