Skip to content

Commit 0aee152

Browse files
committed
Update examples
1 parent 1b49f2c commit 0aee152

File tree

4 files changed

+116
-7
lines changed

4 files changed

+116
-7
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.github.kklisura.cdt.services.ChromeDevToolsService;
1010
import com.github.kklisura.cdt.services.ChromeService;
1111
import com.github.kklisura.cdt.services.types.ChromeTab;
12-
1312
import java.io.File;
1413
import java.io.FileOutputStream;
1514
import java.io.IOException;
@@ -23,14 +22,16 @@
2322
*/
2423
public class FullPageScreenshotExample {
2524

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

2929
final double width = layoutMetrics.getContentSize().getWidth();
3030
final double height = layoutMetrics.getContentSize().getHeight();
3131

3232
final Emulation emulation = devToolsService.getEmulation();
33-
emulation.setDeviceMetricsOverride(Double.valueOf(width).intValue(), Double.valueOf(height).intValue(), 1.0d, Boolean.FALSE);
33+
emulation.setDeviceMetricsOverride(
34+
Double.valueOf(width).intValue(), Double.valueOf(height).intValue(), 1.0d, Boolean.FALSE);
3435

3536
Viewport viewport = new Viewport();
3637
viewport.setScale(1d);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.github.kklisura.cdt.examples;
2+
3+
import com.github.kklisura.cdt.launch.ChromeLauncher;
4+
import com.github.kklisura.cdt.protocol.commands.*;
5+
import com.github.kklisura.cdt.protocol.types.dom.RGBA;
6+
import com.github.kklisura.cdt.protocol.types.overlay.HighlightConfig;
7+
import com.github.kklisura.cdt.services.ChromeDevToolsService;
8+
import com.github.kklisura.cdt.services.ChromeService;
9+
import com.github.kklisura.cdt.services.types.ChromeTab;
10+
11+
/**
12+
* Highlights an elements same way the Chrome inspector does.
13+
*
14+
* @author Kenan Klisura
15+
*/
16+
public class HighlightElementExample {
17+
private static void highlightNode(
18+
ChromeDevToolsService devToolsService, HighlightConfig highlightConfig, String selector) {
19+
final DOM dom = devToolsService.getDOM();
20+
final Overlay overlay = devToolsService.getOverlay();
21+
22+
final Integer nodeId =
23+
dom.querySelector(devToolsService.getDOM().getDocument().getNodeId(), selector);
24+
overlay.highlightNode(highlightConfig, nodeId, null, null, null);
25+
}
26+
27+
public static void main(String[] args) {
28+
// Create chrome launcher.
29+
final ChromeLauncher launcher = new ChromeLauncher();
30+
31+
// Launch chrome either as headless (true) or regular (false).
32+
final ChromeService chromeService = launcher.launch(false);
33+
34+
// Create empty tab ie about:blank.
35+
final ChromeTab tab = chromeService.createTab();
36+
37+
// Get DevTools service to this tab
38+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
39+
40+
// Get individual commands
41+
final Page page = devToolsService.getPage();
42+
final Network network = devToolsService.getNetwork();
43+
final Performance performance = devToolsService.getPerformance();
44+
45+
devToolsService.getDOM().enable();
46+
devToolsService.getOverlay().enable();
47+
48+
performance.enable();
49+
network.onLoadingFinished(
50+
event -> {
51+
HighlightConfig highlightConfig = new HighlightConfig();
52+
53+
highlightConfig.setBorderColor(rgba(255, 229, 153, 0.66));
54+
highlightConfig.setContentColor(rgba(111, 168, 220, 0.66));
55+
highlightConfig.setCssGridColor(rgb(75, 0, 130));
56+
highlightConfig.setEventTargetColor(rgba(255, 196, 196, 0.66));
57+
highlightConfig.setMarginColor(rgba(246, 178, 107, 0.66));
58+
highlightConfig.setPaddingColor(rgba(147, 196, 125, 0.55));
59+
highlightConfig.setShapeColor(rgba(96, 82, 117, 0.8));
60+
highlightConfig.setShapeMarginColor(rgba(96, 82, 127, 0.6));
61+
62+
highlightConfig.setShowExtensionLines(true);
63+
highlightConfig.setShowInfo(true);
64+
highlightConfig.setShowRulers(true);
65+
highlightConfig.setShowStyles(false);
66+
67+
highlightNode(devToolsService, highlightConfig, "h1.h000-mktg");
68+
69+
sleep();
70+
});
71+
72+
// Enable network events.
73+
network.enable();
74+
75+
// Navigate to github.com.
76+
page.navigate("https://www.github.com");
77+
78+
devToolsService.waitUntilClosed();
79+
}
80+
81+
private static void sleep() {
82+
try {
83+
Thread.sleep(100000);
84+
} catch (InterruptedException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
89+
private static RGBA rgba(int r, int g, int b, double a) {
90+
RGBA result = new RGBA();
91+
result.setR(r);
92+
result.setG(g);
93+
result.setB(b);
94+
result.setA(a);
95+
return result;
96+
}
97+
98+
private static RGBA rgb(int r, int g, int b) {
99+
RGBA result = new RGBA();
100+
result.setR(r);
101+
result.setG(g);
102+
result.setB(b);
103+
return result;
104+
}
105+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.github.kklisura.cdt.services.ChromeDevToolsService;
99
import com.github.kklisura.cdt.services.ChromeService;
1010
import com.github.kklisura.cdt.services.types.ChromeTab;
11-
1211
import java.util.List;
1312

1413
/**

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.github.kklisura.cdt.launch.ChromeLauncher;
44
import com.github.kklisura.cdt.protocol.commands.Page;
5+
import com.github.kklisura.cdt.protocol.types.page.PrintToPDF;
6+
import com.github.kklisura.cdt.protocol.types.page.PrintToPDFTransferMode;
57
import com.github.kklisura.cdt.services.ChromeDevToolsService;
68
import com.github.kklisura.cdt.services.ChromeService;
79
import com.github.kklisura.cdt.services.types.ChromeTab;
@@ -59,6 +61,7 @@ public static void main(String[] args) {
5961
String headerTemplate = "";
6062
String footerTemplate = "";
6163
Boolean preferCSSPageSize = false;
64+
PrintToPDFTransferMode mode = PrintToPDFTransferMode.RETURN_AS_BASE_64;
6265

6366
dump(
6467
outputFilename,
@@ -79,7 +82,8 @@ public static void main(String[] args) {
7982
ignoreInvalidPageRanges,
8083
headerTemplate,
8184
footerTemplate,
82-
preferCSSPageSize));
85+
preferCSSPageSize,
86+
mode));
8387

8488
System.out.println("Done!");
8589
devToolsService.close();
@@ -88,12 +92,12 @@ public static void main(String[] args) {
8892
devToolsService.waitUntilClosed();
8993
}
9094

91-
private static void dump(String fileName, String data) {
95+
private static void dump(String fileName, PrintToPDF printToPDF) {
9296
FileOutputStream fileOutputStream = null;
9397
try {
9498
File file = new File(fileName);
9599
fileOutputStream = new FileOutputStream(file);
96-
fileOutputStream.write(Base64.getDecoder().decode(data));
100+
fileOutputStream.write(Base64.getDecoder().decode(printToPDF.getData()));
97101
} catch (IOException e) {
98102
e.printStackTrace();
99103
} finally {

0 commit comments

Comments
 (0)