How to take Screenshot of entire page in Selenium WebDriver?

Why to take screenshot is required in automation?

  • Screenshot helps us to understand the flow of application whether application is behaving correctly  or not.
  • It helps as while doing cross browser testing.
  •  Another use is, while doing headless testing, you will not see actual GUI screen, but it will run on backend (ex – by using HTMLUnitBrowser ), in that case, we can track failed tests.
  •    Screen shot is required in failure, so that we will get to know why the test case is failed.

To take screen shot in selenium, we have one predefined interface known as TakesScreenshot, we can not create object of an interface in java. So we need to typecast it to Webdriver object.
                          TakesScreenshot ts = (TakesScreenshot)driver;      
Explanation:    We can’t create object directly so typecast and then create.
                          File srcFile = ts.getScreenShotAs(OutPutType.FILE);   
Explanation:    Use the object created, then get screenshot as file & assign it to the File variable.
                        FileUtils.copyFile(srcFile, new File(“./ScreenShots/image1.png”);     
 Explanation:    Now you need to copy the srcFile, so use FileUtils class, then give source file & destination file. In destination file “.” (dot) refers to the current working directory.
Below is the sample piece of code which shows how to take a screenshot in selenium.

package basicsOfSelenium;



import java.util.concurrent.TimeUnit;


import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class HowToGetScreenShotInSelenium {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//to take screenshot - observe below code

TakesScreenshot ts = (TakesScreenshot)driver; // We can’t create object of an interface directly so typecast and then create.
File srcFIle = ts.getScreenshotAs(OutputType.FILE); //Use the object created, then get screenshot as file & assign it to the File variable.

try {

FileUtils.copyFile(srcFIle, new File("./ScreenShots/test1.jpg"));

} catch (IOException e) {





 To know how to take screenshot of failed test case only, refer below post.

Refer – How to take screen shot of failed test case only?   <Link>

Hope this helps !!!!!

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

Get the Framework code at Github Repo: