0
# Screenshot Capabilities
1
2
Full-page screenshot functionality beyond standard WebDriver screenshot capabilities. Firefox WebDriver provides enhanced screenshot features for capturing complete page content.
3
4
## Capabilities
5
6
### HasFullPageScreenshot Interface
7
8
Interface for taking full-page screenshots that capture entire page content beyond the viewport.
9
10
```java { .api }
11
/**
12
* Interface for taking full-page screenshots.
13
* Extends standard WebDriver screenshot functionality to capture complete page content.
14
*/
15
@Beta
16
public interface HasFullPageScreenshot {
17
18
/**
19
* Takes a full-page screenshot of the current page.
20
* Captures the entire page content, not just the visible viewport.
21
* @param outputType Output format for the screenshot (FILE, BYTES, BASE64)
22
* @param <X> Return type determined by OutputType
23
* @return Screenshot in the specified format
24
*/
25
<X> X getFullPageScreenshotAs(OutputType<X> outputType);
26
}
27
```
28
29
**Usage Examples:**
30
31
```java
32
import org.openqa.selenium.firefox.FirefoxDriver;
33
import org.openqa.selenium.firefox.HasFullPageScreenshot;
34
import org.openqa.selenium.OutputType;
35
import java.io.File;
36
37
FirefoxDriver driver = new FirefoxDriver();
38
HasFullPageScreenshot screenshotCapability = driver; // FirefoxDriver implements HasFullPageScreenshot
39
40
driver.get("https://example.com/long-page");
41
42
// Take full-page screenshot as file
43
File screenshotFile = screenshotCapability.getFullPageScreenshotAs(OutputType.FILE);
44
System.out.println("Full-page screenshot saved to: " + screenshotFile.getAbsolutePath());
45
46
// Take full-page screenshot as byte array
47
byte[] screenshotBytes = screenshotCapability.getFullPageScreenshotAs(OutputType.BYTES);
48
System.out.println("Screenshot size: " + screenshotBytes.length + " bytes");
49
50
// Take full-page screenshot as Base64 string
51
String screenshotBase64 = screenshotCapability.getFullPageScreenshotAs(OutputType.BASE64);
52
System.out.println("Base64 screenshot length: " + screenshotBase64.length());
53
```
54
55
### OutputType Support
56
57
The full-page screenshot functionality supports all standard WebDriver OutputType formats.
58
59
```java { .api }
60
// Standard OutputType options for screenshots
61
public final class OutputType<T> {
62
public static final OutputType<File> FILE; // Save to temporary file
63
public static final OutputType<byte[]> BYTES; // Return as byte array
64
public static final OutputType<String> BASE64; // Return as Base64 string
65
}
66
```
67
68
### Comparison with Standard Screenshots
69
70
Understanding the difference between standard WebDriver screenshots and full-page screenshots:
71
72
**Standard WebDriver Screenshot (TakesScreenshot interface):**
73
```java
74
import org.openqa.selenium.TakesScreenshot;
75
76
// Standard viewport screenshot
77
TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
78
File viewportScreenshot = takesScreenshot.getScreenshotAs(OutputType.FILE);
79
// Only captures visible viewport area
80
```
81
82
**Full-Page Screenshot (HasFullPageScreenshot interface):**
83
```java
84
import org.openqa.selenium.firefox.HasFullPageScreenshot;
85
86
// Full-page screenshot
87
HasFullPageScreenshot fullPageCapability = (HasFullPageScreenshot) driver;
88
File fullPageScreenshot = fullPageCapability.getFullPageScreenshotAs(OutputType.FILE);
89
// Captures entire page content, including content below the fold
90
```
91
92
### Practical Usage Examples
93
94
**Basic Full-Page Screenshot:**
95
96
```java
97
import org.openqa.selenium.firefox.FirefoxDriver;
98
import org.openqa.selenium.OutputType;
99
import java.io.File;
100
import java.io.IOException;
101
import java.nio.file.Files;
102
import java.nio.file.StandardCopyOption;
103
104
FirefoxDriver driver = new FirefoxDriver();
105
106
// Navigate to page with long content
107
driver.get("https://example.com/documentation");
108
109
// Take full-page screenshot
110
File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE);
111
112
// Copy to permanent location with meaningful name
113
File permanentLocation = new File("/screenshots/documentation-full-page.png");
114
Files.copy(screenshot.toPath(), permanentLocation.toPath(),
115
StandardCopyOption.REPLACE_EXISTING);
116
117
System.out.println("Full-page screenshot saved to: " + permanentLocation);
118
```
119
120
**Screenshot Comparison Testing:**
121
122
```java
123
import org.openqa.selenium.firefox.FirefoxDriver;
124
import org.openqa.selenium.OutputType;
125
import java.io.File;
126
import java.io.FileOutputStream;
127
import java.io.IOException;
128
129
FirefoxDriver driver = new FirefoxDriver();
130
driver.get("https://example.com");
131
132
// Take both types of screenshots for comparison
133
byte[] viewportScreenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
134
byte[] fullPageScreenshot = driver.getFullPageScreenshotAs(OutputType.BYTES);
135
136
// Save both screenshots
137
try (FileOutputStream viewport = new FileOutputStream("viewport.png");
138
FileOutputStream fullPage = new FileOutputStream("fullpage.png")) {
139
140
viewport.write(viewportScreenshot);
141
fullPage.write(fullPageScreenshot);
142
143
System.out.println("Viewport screenshot size: " + viewportScreenshot.length);
144
System.out.println("Full-page screenshot size: " + fullPageScreenshot.length);
145
}
146
```
147
148
**Automated Screenshot Documentation:**
149
150
```java
151
import org.openqa.selenium.firefox.FirefoxDriver;
152
import org.openqa.selenium.OutputType;
153
import java.io.File;
154
import java.time.LocalDateTime;
155
import java.time.format.DateTimeFormatter;
156
import java.util.List;
157
158
public class DocumentationScreenshots {
159
private FirefoxDriver driver;
160
private String screenshotDir;
161
162
public DocumentationScreenshots() {
163
this.driver = new FirefoxDriver();
164
this.screenshotDir = "/documentation/screenshots/";
165
}
166
167
public void capturePageDocumentation(List<String> urls) {
168
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
169
170
for (String url : urls) {
171
try {
172
driver.get(url);
173
174
// Wait for page to fully load
175
Thread.sleep(2000);
176
177
// Generate filename from URL and timestamp
178
String filename = url.replaceAll("[^a-zA-Z0-9.-]", "_") +
179
"_" + LocalDateTime.now().format(formatter) + ".png";
180
181
// Take full-page screenshot
182
File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE);
183
184
// Move to documentation directory
185
File destination = new File(screenshotDir + filename);
186
screenshot.renameTo(destination);
187
188
System.out.println("Captured: " + url + " -> " + filename);
189
190
} catch (Exception e) {
191
System.err.println("Failed to capture " + url + ": " + e.getMessage());
192
}
193
}
194
}
195
196
public void close() {
197
if (driver != null) {
198
driver.quit();
199
}
200
}
201
}
202
203
// Usage
204
DocumentationScreenshots capture = new DocumentationScreenshots();
205
List<String> pages = Arrays.asList(
206
"https://example.com/",
207
"https://example.com/products",
208
"https://example.com/documentation"
209
);
210
211
capture.capturePageDocumentation(pages);
212
capture.close();
213
```
214
215
**Screenshot with Custom Handling:**
216
217
```java
218
import org.openqa.selenium.firefox.FirefoxDriver;
219
import org.openqa.selenium.OutputType;
220
import javax.imageio.ImageIO;
221
import java.awt.image.BufferedImage;
222
import java.io.ByteArrayInputStream;
223
import java.io.File;
224
import java.io.IOException;
225
226
FirefoxDriver driver = new FirefoxDriver();
227
driver.get("https://example.com");
228
229
// Get screenshot as bytes for processing
230
byte[] screenshotBytes = driver.getFullPageScreenshotAs(OutputType.BYTES);
231
232
// Convert to BufferedImage for manipulation
233
try (ByteArrayInputStream bis = new ByteArrayInputStream(screenshotBytes)) {
234
BufferedImage image = ImageIO.read(bis);
235
236
System.out.println("Screenshot dimensions: " +
237
image.getWidth() + "x" + image.getHeight());
238
239
// Save in different format
240
ImageIO.write(image, "jpg", new File("screenshot.jpg"));
241
242
// Could also resize, crop, or apply other transformations here
243
244
} catch (IOException e) {
245
System.err.println("Error processing screenshot: " + e.getMessage());
246
}
247
```
248
249
**Integration with Test Frameworks:**
250
251
```java
252
import org.junit.jupiter.api.Test;
253
import org.junit.jupiter.api.AfterEach;
254
import org.openqa.selenium.firefox.FirefoxDriver;
255
import org.openqa.selenium.OutputType;
256
import java.io.File;
257
258
public class FullPageScreenshotTest {
259
private FirefoxDriver driver;
260
261
@Test
262
public void testFullPageCapture() {
263
driver = new FirefoxDriver();
264
driver.get("https://example.com/long-content");
265
266
// Test specific functionality
267
// ...
268
269
// Capture full-page screenshot for verification
270
File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE);
271
272
// Verify screenshot was created
273
assert screenshot.exists();
274
assert screenshot.length() > 0;
275
276
System.out.println("Test screenshot: " + screenshot.getAbsolutePath());
277
}
278
279
@AfterEach
280
public void tearDown() {
281
if (driver != null) {
282
driver.quit();
283
}
284
}
285
}
286
```