Capture Screenshots Using Selenium
The screenshot is one of the most frequent activities of testing. The screenshot shot is always taken to capture important details or record another specific event during testing. Generally, most of us capture a screenshot of test failure. Accordingly, this tutorial explains how to capture screenshots and store them in a specific directory.
Above all, this tutorial provides step by step detailed explanation of each step and details of methods provided by Selenium WebDriver. Refer below for a step-by-step implementation of how to capture screenshots using Selenium. As a result, you will become pro in capturing screenshots using Selenium.
Pre-requisite:
- Selenium Project should be set up. ( If Selenium Project is not set up then refer to this link)
TakesScreenshot Interface to take or capture screenshots using selenium
Selenium Interface: TakesScreenshot interface provides a method to capture screenshot and store them in different ways.
- a) OutputType.FILE – Obtain the screenshot as a temporary file and deleted it once JVM exits.
- b) OutputType.BASE64– Obtain screenshot as base64 output.
- c) OutputType. BYTES– Obtain screenshot as raw bytes.
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: Another key point, 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 to 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 provides a method to capture screenshot and return screenshot as a file. TakesScreenshot capture screenshot of the viewable screen if you want to capture a 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:
Cool! Now you have completed the basics of screenshot capture and implemented code for it. Hope you successfully implemented this still if you face any issue then feel free to write us at query@thoughtcoders.com. Also, refer next tutorial on how to capture a screenshot on test failure and how to capture screenshots of full screen and specific web elements.