Monday, December 15, 2014


This article shows you how to take screenshot using Selenium Webdriver. Screen shot may help you to identify and debug the problem.





Take Screenshot Using Selenium Webdriver

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;

public class Training {
 static WebDriver driver;

 public static void main(String[] args) {

  driver = new FirefoxDriver();
  driver.get("http://www.google.com");
  driver.manage().window().maximize();
  try {
   driver.findElement(
     By.xpath("//*[@id='Blog1]"))
     .sendKeys("test");

  } catch (Exception e) {
   System.out.println("Please look at D:\\screenshot.png for screenshot!");
   try {
    getscreenshot();
   } catch (Exception e1) {
    System.out.println(e1);
   }
  }
 }

 public static void getscreenshot() throws Exception {
  File scrFile = ((TakesScreenshot) driver)
    .getScreenshotAs(OutputType.FILE);

  FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
 }

}