Take Screenshot using Selenium Web Driver -Java

Most of the times you need to take a screenshot of a web page after you have performed a test on a page .This helps up if you have to send in a report where you have to include a screen shot in case a test case has failed.Selenium has TakeScreenshot class which will help you get the job done.
Below is a sample code in java.


package com.selenium_basics;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
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 TakeScreenShot {
 @Test 
 public void TakeScreenShotof() throws InterruptedException, IOException{
  WebDriver driver=new FirefoxDriver();
  driver.get("http://www.learn-selenium.net/");
  driver.manage().window().maximize();
  File ScrnShot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  
  FileUtils.copyFile(ScrnShot,new File("c:\\scrnShots\\screenShot.png")); // Path to save the screen Shot
  
  
  driver.close();
 }

}


Below is the sample output