Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Capture Screenshot: How Selenium WebDriver Takes Screenshot?

Once upon a time, I was a manual functional tester. So, I had to keep proof of all the testing activities in the form of screenshots. Functional testing was quite simple to do with ease, but, taking screenshots of each step and saving them in doc format was cumbersome activity. I really hate to Capture Screenshot; the main reason was the system was quite slow in VDI and we need to prepare QA doc.

Later, I moved to Automation testing with Selenium WebDriver. It really fascinated me when I came across that Selenium could Capture screenshot of automation testing activities. I took a deep breath and whispered thank you Selenium for saving me.

Further with the advancement in Automation framework the purpose to capture screenshot varies, so let’s discuss them below:

Don’t Miss: How to handle date picker Calendar in Selenium WebDriver? 

Purpose to capture screenshot in Selenium WebDriver

  • Taking a screenshot of failed test cases or steps
  • To capture screenshot of the entire testing processes
  • Capturing the screenshot of the important checkpoints
  • To capture screenshot when the server has an awkward response
  • It could be done when there are assertion failure and application issues
  • When timeout exceptions occur then Selenium could capture screenshot
  • Selenium capture screenshot when WebDriver is unable to find the web elements

Taking screenshot has the major advantage as in the form of failure analysis. It could be bug analysis, downtime analysis, etcetera.

Concepts needed to implement capture screenshot command with WebDriver

Initially, the automation engineer tries to memorize the commands and later they get confused. Therefore, I would suggest you go through the basic concept so that you don’t find any difficulties while implementation.

Java Type Casting and File creation is the basic concept behind the screenshot capture command of Selenium. Just glued to these two concepts then the command will be automatically stored in your mind.

There is no connection of this image with the content

Takes Screenshot command to capture screenshot in Selenium

Here is the command with their explanation to capture screenshot in WebDriver.

Step 1: Type cast WebDriver interface

TakesScreenshot scShot = (TakesScreenshot)driver;

This command basically performs typecasting of WebDriver interface to TakesScreenshot interface.

Step 2: Declaration of the source file

File sourceFile = scShot.getScreenshotAs(OutputType.FILE);

We declare source file by calling the getScreenshotAs method and we put File type as its argument. This method resides within the TakesScreenshot interface.

Step 3: Declaration of the destination file

File destFile = new File("C:\\Selscreenshot\\screenshot.png");

Now we should define the local destination folder to copy the screenshot from a source file.

Step 4: Copy source file to destination file (folder)

FileUtils.copyFile(sourceFile, destFile);

Above command is basically copying the source file to the local destination folder.

If you develop the concept like this, then you don’t have to mug up everything. Let’s implement capture screenshot scenario in a simple test case.

Implementation of TakesScreenshot to capture screenshot in Selenium WebDriver

Scenario: This test case is to verify the proper loading of homepage.

Sample code

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class CaptureScreenshot {

	public static void main(String[] args)  {
		 System.setProperty("", "C:\\Selenium\\chromedriver.exe");
		  WebDriver driver = new ChromeDriver();
		  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  //Screenshot Capture 
		  TakesScreenshot scShot = (TakesScreenshot)driver;
		  File sourceFile = scShot.getScreenshotAs(OutputType.FILE);
		  File destFile = new File("C:\\Selscreenshot\\screenshot.png");
			  FileUtils.copyFile(sourceFile, destFile);
		  }catch(IOException e){
			  System.out.println("File Exception- "+e.getMessage());




WebDriver simple opens the homepage and wait for the elements to load. Once homepage is loaded then it takes the screenshot and stores to the destination folder.

I believe this article will help you when you need to implement screenshot capture command in your automation test cases. If you have any queries, then don’t hesitate to share in our Facebook group. If you have not joined yet, then click on below image to join now.

The post Capture Screenshot: How Selenium WebDriver Takes Screenshot? appeared first on Inviul.

This post first appeared on Inviul, please read the originial post: here

Share the post

Capture Screenshot: How Selenium WebDriver Takes Screenshot?


Subscribe to Inviul

Get updates delivered right to your inbox!

Thank you for your subscription