Skip to content

Commit 434b226

Browse files
committed
Update full page screenshot example
1 parent 4951a97 commit 434b226

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

cdt-examples/src/main/java/com/github/kklisura/cdt/examples/FullPageScreenshotExample.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.github.kklisura.cdt.examples;
22

33
import com.github.kklisura.cdt.launch.ChromeLauncher;
4+
import com.github.kklisura.cdt.protocol.commands.Emulation;
45
import com.github.kklisura.cdt.protocol.commands.Page;
56
import com.github.kklisura.cdt.protocol.types.page.CaptureScreenshotFormat;
67
import com.github.kklisura.cdt.protocol.types.page.LayoutMetrics;
78
import com.github.kklisura.cdt.protocol.types.page.Viewport;
89
import com.github.kklisura.cdt.services.ChromeDevToolsService;
910
import com.github.kklisura.cdt.services.ChromeService;
1011
import com.github.kklisura.cdt.services.types.ChromeTab;
12+
1113
import java.io.File;
1214
import java.io.FileOutputStream;
1315
import java.io.IOException;
@@ -21,9 +23,15 @@
2123
*/
2224
public class FullPageScreenshotExample {
2325

24-
private static void captureFullPageScreenshot(Page page, String outputFilename) {
26+
private static void captureFullPageScreenshot(ChromeDevToolsService devToolsService, Page page, String outputFilename) {
2527
final LayoutMetrics layoutMetrics = page.getLayoutMetrics();
2628

29+
final double width = layoutMetrics.getContentSize().getWidth();
30+
final double height = layoutMetrics.getContentSize().getHeight();
31+
32+
final Emulation emulation = devToolsService.getEmulation();
33+
emulation.setDeviceMetricsOverride(Double.valueOf(width).intValue(), Double.valueOf(height).intValue(), 1.0d, Boolean.FALSE);
34+
2735
Viewport viewport = new Viewport();
2836
viewport.setScale(1d);
2937

@@ -32,8 +40,8 @@ private static void captureFullPageScreenshot(Page page, String outputFilename)
3240
viewport.setY(0d);
3341

3442
// Set a width, height of a page to take screenshot at
35-
viewport.setWidth(layoutMetrics.getContentSize().getWidth());
36-
viewport.setHeight(layoutMetrics.getContentSize().getHeight());
43+
viewport.setWidth(width);
44+
viewport.setHeight(height);
3745

3846
dump(
3947
outputFilename,
@@ -60,7 +68,7 @@ public static void main(String[] args) {
6068
event -> {
6169
System.out.println("Taking screenshot...");
6270

63-
captureFullPageScreenshot(page, "screenshot.png");
71+
captureFullPageScreenshot(devToolsService, page, "screenshot.png");
6472

6573
System.out.println("Done!");
6674

0 commit comments

Comments
 (0)