Skip to content

Commit dc84ebc

Browse files
committed
Add metrics example
1 parent f6c3812 commit dc84ebc

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.github.kklisura.cdt.examples;
2+
3+
import com.github.kklisura.cdt.launch.ChromeLauncher;
4+
import com.github.kklisura.cdt.protocol.commands.Network;
5+
import com.github.kklisura.cdt.protocol.commands.Page;
6+
import com.github.kklisura.cdt.protocol.commands.Performance;
7+
import com.github.kklisura.cdt.protocol.types.performance.Metric;
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+
12+
import java.util.List;
13+
14+
/**
15+
* Metric example
16+
*
17+
* @author Kenan Klisura
18+
*/
19+
public class PerformanceMetricsExample {
20+
public static void main(String[] args) {
21+
// Create chrome launcher.
22+
final ChromeLauncher launcher = new ChromeLauncher();
23+
24+
// Launch chrome either as headless (true) or regular (false).
25+
final ChromeService chromeService = launcher.launch(false);
26+
27+
// Create empty tab ie about:blank.
28+
final ChromeTab tab = chromeService.createTab();
29+
30+
// Get DevTools service to this tab
31+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
32+
33+
// Get individual commands
34+
final Page page = devToolsService.getPage();
35+
final Network network = devToolsService.getNetwork();
36+
final Performance performance = devToolsService.getPerformance();
37+
38+
performance.enable();
39+
network.onLoadingFinished(
40+
event -> {
41+
// Close the tab and close the browser when loading finishes.
42+
List<Metric> metrics = performance.getMetrics();
43+
try {
44+
for (Metric metric : metrics) {
45+
System.out.println(metric.getName() + ": " + metric.getValue());
46+
}
47+
} catch (Exception e) {
48+
System.out.println(e.getMessage());
49+
}
50+
chromeService.closeTab(tab);
51+
launcher.close();
52+
});
53+
54+
// Enable network events.
55+
network.enable();
56+
57+
// Navigate to github.com.
58+
page.navigate("https://www.github.com");
59+
60+
devToolsService.waitUntilClosed();
61+
}
62+
}

0 commit comments

Comments
 (0)