How to Take A Screenshot in Selenium WebDriver

How to Take A Screenshot in Selenium WebDriver?

Last updated on 22nd Sep 2020, Artciles, Blog

About author

Pradeep (Sr Testing Manager )

He is a TOP-Rated Domain Expert with 6+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 3 Years & Share's this Informative Articles For Freshers

(5.0) | 14557 Ratings 332

Taking Screenshot using Webdriver

It’s very important to take a screenshot when we execute a test script. When we execute a huge number of test scripts, and if some test fails, we need to check why the test has failed.

It helps us to debug and identify the problem by seeing the screen shot.

In selenium webdriver, we can take the screen shot using the below command.

  • File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Check the framework example of Taking ScreenShot for ONLY Failed Tests using TestNG

The below example explains how to take the screenshot when the test fails.

Subscribe For Free Demo
[contact-form-7 404 "Not Found"]
  • 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;
  • import org.testng.annotations.Test;
  • public class takeScreenShotExample{
  • public WebDriver driver;
  • @Test
  • public void openBrowser() throws Exception {
  • driver = new FirefoxDriver();
  • driver.manage().window().maximize();
  • driver.get(“http://www.google.com”);
  • try{

//the below statement will throw an exception as the element is not found, Catch block will get executed and takes the screenshot.

  • driver.findElement(By.id(“testing”)).sendKeys(“test”);
  • //if we remove the comment below, it will not return an exception and the screen shot method will not get executed.
  • //driver.findElement(By.id(“gbqfq”)).sendKeys(“test”);
  • }
  • catch (Exception e){
  • System.out.println(“I’m in exception”);
  • //calls the method to take the screenshot.
  • getscreenshot();
  • }
  • }
  • public void getscreenshot() throws Exception 
  • {
  • File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  • //The below method will save the screenshot in d drive with
  • FileUtils.copyFile(scrFile, new File(“D:\\screenshot.png”));
  • }
  • }

Explanation Of The Above Code

  • WebDriver driver = new ChromeDriver();

In the above statement we have upcasted the chromedriver object to webdriver, but for capture the screenshot we need to downcast the chromedriver object driver to TakesScreenshot interface.

  • TakesScreenshot ts= (TakesScreenshot)driver;
  • File screenshotSRC= ts.getScreenshotAs(OutputType.FILE);

In the above statement, the OutputType is an interface which can provide options to take the screenshot in different types such as FILE, BASE64, BYTES, and class. But out of those types, the file type is mostly used.

  • path=System.getProperty(“user.dir”)+”/ScreenCapturesPNG/” +testStepsName+System.currentTimeMillis()+”.png”;

When webdriver takes the screenshot, it stores in the temp folder. Once the execution is over the file also got deleted. That’s why we need to copy the screenshot in a permanent folder, so for that in the above statement we are mentioned the permanent folder which is ScreenCapturesPNG with the file name.

  • File screenshotDest= new File(path);

In this statement, we are creating a file where we mentioned where it should be stored with a specific name.

selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

How Do You Take Screenshot of The Entire Page?

For taking the full page screenshot, we can take the help of the third-party tools like AShot to take the whole page screenshot or a specific element screenshot. Now we are going to discuss the AShot tools, and these tools have a rich feature like:

  • Take a specific area of a page
  • Do scroll and take a screenshot
  • In case you have an infinite scroll page, you can set timeouts
  • Ignore are during the screenshot
  • Change orientation or change resolution

Are you looking training with Right Jobs?

Contact Us

Popular Courses