How to take screenshots using Selenium WebDriver

Capture screenshot in Selenium:

The screenshot is one of the most frequent activities of testing. The screenshot shot is always taken to capture important details or record specific event during testing. Generally, most of us capture a screenshot on test failure. This tutorial explains how to capture screenshot and store at the specific directory.
This tutorial provides step by step detailed explanation of each step and details of methods provided by Selenium WebDriver. Refer below for step by step implementation.

Pre-requisite:

  1. Selenium Project should be set up. ( If Selenium Project is not set up then refer this link)

TakesScreenshot Interface to take or capture a screenshot:

Selenium Interface: TakesScreenshot interface provide method to capture screenshot and store in different ways.

  • a) OutputType.FILE – Obtain the screenshot as a temporary file and deleted once JVM exits.
  • b) OutputType.BASE64– Obtain screenshot as base64 output.
  • c) OutputType. BYTES– Obtain screenshot as raw bytes.
Screenshots output type

How does a user take a screenshot in Selenium?

Steps to take or capture screenshots in selenium:

  • Step 1: Open Selenium Project and create one class to write screenshot capture code.
  • Step 2: Write code to instantiate WebDriver and navigate to URL :

In the below code, we have used WebDriverManager to manage browser binaries. (If you want to learn about WebDriverManager then refer this tutorial- Click Here).

public static void main(String [] args) throws IOException {
WebDriverManager.chromedriver().version("83.0").setup();
WebDriver driver = new ChromeDriver();
driver.get("https://thoughtcoders.com/blog/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
  • Step 3: Write code to capture a screenshot: TakesScreenshot interface provide a method to capture screenshot and return screenshot as a file. TakesScreenshot capture screenshot of the viewable screen if you want to capture screenshot of the full screen then refer next tutorial.
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  • Step 4: Use Apache Commons IO API to copy file method to copy screenshot in a specific directory.
FileUtils.copyFile(sourceFile, destination directory)

Destination Directory: System.getProperty(“user.dir”)
Screenshot name: screenshot.png

FileUtils.copyFile(file, new File(System.getProperty("user.dir")+"\screenshot.png"));
package com.thoughtcoder;
import io.github.bonigarcia.wdm.WebDriverManager;
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.chrome.ChromeDriver;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class screensotCapture {
    public static void main(String [] args) throws IOException {
        WebDriverManager.chromedriver().version("83.0").setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://thoughtcoders.com/blog/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File(System.getProperty("user.dir")+"\\screenshot.png"));
        driver.close();
    }
}
  • Step 5: Run the complete code and check the project root directory:
Captured screenshot using selenium

Cool! Now you have completed basics of screenshot capture and implemented code for it. Hope you successfully implemented this still if you face issue then feel free to write us on query@thoughtcoders.com. Also, refer next tutorial on how to capture a screenshot on test failure and how to capture screenshot of full screen and specific web element.

Subscribe to our Newsletter:

5 1 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact