Implementation:SeleniumHQ Selenium OutputType
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Browser_Automation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for defining screenshot output formats provided by the Selenium WebDriver library.
Description
The OutputType<T> interface defines how screenshot data captured by TakesScreenshot is converted and returned. It is a generic interface parameterized by the desired output type, with two conversion methods: one from a base64-encoded PNG string and one from raw PNG byte data. The interface ships with three built-in constants: BASE64 (returns a base64-encoded String), BYTES (returns a raw byte[]), and FILE (writes to a temporary file and returns a File handle that is automatically deleted on JVM exit).
Usage
Import OutputType when calling TakesScreenshot.getScreenshotAs() to specify in which format the screenshot data should be returned. Use the provided constants (OutputType.FILE, OutputType.BASE64, OutputType.BYTES) or implement the interface for a custom output format.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/OutputType.java
Signature
public interface OutputType<T> {
OutputType<String> BASE64; // Returns screenshot as base64 string
OutputType<byte[]> BYTES; // Returns screenshot as raw bytes
OutputType<File> FILE; // Returns screenshot as a temporary File
T convertFromBase64Png(String base64Png);
T convertFromPngBytes(byte[] png);
}
Import
import org.openqa.selenium.OutputType;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| base64Png | String | Yes (for convertFromBase64Png) | A base64-encoded PNG image string to convert to the target type. |
| png | byte[] | Yes (for convertFromPngBytes) | Raw PNG image bytes to convert to the target type. |
Outputs
| Name | Type | Description |
|---|---|---|
| BASE64 result | String | The screenshot data as a base64-encoded PNG string. |
| BYTES result | byte[] | The screenshot data as raw PNG byte array. |
| FILE result | File | A temporary file containing the PNG screenshot data. The file is marked for deletion on JVM exit; users should copy it if persistence is needed. |
Usage Examples
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
WebDriver driver = // ... obtain driver instance
TakesScreenshot screenshotDriver = (TakesScreenshot) driver;
// Capture screenshot as a temporary file and copy to a permanent location
File tempScreenshot = screenshotDriver.getScreenshotAs(OutputType.FILE);
Files.copy(tempScreenshot.toPath(),
new File("screenshot.png").toPath(),
StandardCopyOption.REPLACE_EXISTING);
// Capture screenshot as base64 for embedding in reports
String base64Screenshot = screenshotDriver.getScreenshotAs(OutputType.BASE64);
System.out.println("Base64 length: " + base64Screenshot.length());
// Capture screenshot as raw bytes for further processing
byte[] screenshotBytes = screenshotDriver.getScreenshotAs(OutputType.BYTES);
System.out.println("Screenshot size: " + screenshotBytes.length + " bytes");