Skip to content

Commit e45b0de

Browse files
committed
Add full page screenshot example
1 parent 5816a37 commit e45b0de

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.github.kklisura.cdt.examples;
2+
3+
import com.github.kklisura.cdt.launch.ChromeLauncher;
4+
import com.github.kklisura.cdt.protocol.commands.Page;
5+
import com.github.kklisura.cdt.protocol.types.page.CaptureScreenshotFormat;
6+
import com.github.kklisura.cdt.protocol.types.page.LayoutMetrics;
7+
import com.github.kklisura.cdt.protocol.types.page.Viewport;
8+
import com.github.kklisura.cdt.services.ChromeDevToolsService;
9+
import com.github.kklisura.cdt.services.ChromeService;
10+
import com.github.kklisura.cdt.services.types.ChromeTab;
11+
import java.io.File;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.util.Base64;
15+
16+
/**
17+
* Takes a full page screenshot. The output screenshot dimensions will be page width x page height,
18+
* for example when capturing http://github.com the output screenshot image will be 1200 × 6598.
19+
*
20+
* @author Kenan Klisura
21+
*/
22+
public class FullPageScreenshotExample {
23+
24+
private static void captureFullPageScreenshot(Page page, String outputFilename) {
25+
final LayoutMetrics layoutMetrics = page.getLayoutMetrics();
26+
27+
Viewport viewport = new Viewport();
28+
viewport.setScale(1d);
29+
30+
// You can set offset with X, Y
31+
viewport.setX(0d);
32+
viewport.setY(0d);
33+
34+
// Set a width, height of a page to take screenshot at
35+
viewport.setWidth(layoutMetrics.getContentSize().getWidth());
36+
viewport.setHeight(layoutMetrics.getContentSize().getHeight());
37+
38+
dump(
39+
outputFilename,
40+
page.captureScreenshot(CaptureScreenshotFormat.PNG, 100, viewport, Boolean.TRUE));
41+
}
42+
43+
public static void main(String[] args) {
44+
// Create chrome launcher.
45+
final ChromeLauncher launcher = new ChromeLauncher();
46+
47+
// Launch chrome either as headless (true) or regular (false).
48+
final ChromeService chromeService = launcher.launch(false);
49+
50+
// Create empty tab ie about:blank.
51+
final ChromeTab tab = chromeService.createTab();
52+
53+
// Get DevTools service to this tab
54+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
55+
56+
// Get individual commands
57+
final Page page = devToolsService.getPage();
58+
59+
page.onLoadEventFired(
60+
event -> {
61+
System.out.println("Taking screenshot...");
62+
63+
captureFullPageScreenshot(page, "screenshot.png");
64+
65+
System.out.println("Done!");
66+
67+
devToolsService.close();
68+
});
69+
70+
// Enable page events.
71+
page.enable();
72+
73+
// Navigate to github.com.
74+
page.navigate("http://github.com");
75+
76+
devToolsService.waitUntilClosed();
77+
}
78+
79+
private static void dump(String fileName, String data) {
80+
FileOutputStream fileOutputStream = null;
81+
try {
82+
File file = new File(fileName);
83+
fileOutputStream = new FileOutputStream(file);
84+
fileOutputStream.write(Base64.getDecoder().decode(data));
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
} finally {
88+
if (fileOutputStream != null) {
89+
try {
90+
fileOutputStream.flush();
91+
fileOutputStream.close();
92+
} catch (IOException e) {
93+
e.printStackTrace();
94+
}
95+
}
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)