Skip to content

Commit 9035b0e

Browse files
committed
Add some more examples
1 parent d7445b7 commit 9035b0e

File tree

7 files changed

+305
-2
lines changed

7 files changed

+305
-2
lines changed

cdtp-examples/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<maven.compiler.target>1.8</maven.compiler.target>
1717

1818
<logback.version>1.2.3</logback.version>
19+
<jackson.version>2.9.3</jackson.version>
20+
<common.codec.version>1.11</common.codec.version>
1921
</properties>
2022

2123
<dependencies>
@@ -30,6 +32,18 @@
3032
<artifactId>cdtp-java-client</artifactId>
3133
<version>1.0-SNAPSHOT</version>
3234
</dependency>
35+
36+
<dependency>
37+
<groupId>commons-codec</groupId>
38+
<artifactId>commons-codec</artifactId>
39+
<version>${common.codec.version}</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.fasterxml.jackson.core</groupId>
44+
<artifactId>jackson-core</artifactId>
45+
<version>${jackson.version}</version>
46+
</dependency>
3347
</dependencies>
3448

3549
<build>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.github.kklisura.cdpt.examples;
2+
3+
import com.github.kklisura.cdtp.launch.ChromeLauncher;
4+
import com.github.kklisura.cdtp.protocol.commands.Network;
5+
import com.github.kklisura.cdtp.protocol.commands.Page;
6+
import com.github.kklisura.cdtp.services.ChromeDevToolsService;
7+
import com.github.kklisura.cdtp.services.ChromeService;
8+
import com.github.kklisura.cdtp.services.types.ChromeTab;
9+
import java.util.Arrays;
10+
11+
/**
12+
* Blocks an URLs given a patterns.
13+
*
14+
* @author Kenan Klisura
15+
*/
16+
public class BlockUrlGIvenPatternExample {
17+
public static void main(String[] args) throws InterruptedException {
18+
// Create chrome launcher.
19+
final ChromeLauncher launcher = new ChromeLauncher();
20+
21+
// Launch chrome either as headless (true) or regular (false).
22+
final ChromeService chromeService = launcher.launch(false);
23+
24+
// Create empty tab ie about:blank.
25+
final ChromeTab tab = chromeService.createTab();
26+
27+
// Get DevTools service to this tab
28+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
29+
30+
// Get individual commands
31+
final Page page = devToolsService.getPage();
32+
final Network network = devToolsService.getNetwork();
33+
34+
// Block some urls.
35+
network.setBlockedURLs(Arrays.asList(
36+
"**/*.css",
37+
"**/*.png",
38+
"**/*.svg"
39+
));
40+
41+
// Enable network events
42+
network.enable();
43+
44+
// Wait for on load event
45+
page.onLoadEventFired(
46+
event -> {
47+
// Close devtools.
48+
devToolsService.close();
49+
});
50+
51+
// Enable page events.
52+
page.enable();
53+
54+
// Navigate to github.com.
55+
page.navigate("http://github.com");
56+
57+
// Wait until devtools is closed.
58+
devToolsService.waitUntilClosed();
59+
60+
// Close tab.
61+
chromeService.closeTab(tab);
62+
}
63+
}

cdtp-examples/src/main/java/com/github/kklisura/cdpt/examples/DumpHtmlFromPageExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ public static void main(String[] args) throws InterruptedException {
5454
// Close tab.
5555
chromeService.closeTab(tab);
5656
}
57-
}
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.kklisura.cdpt.examples;
2+
3+
import com.github.kklisura.cdtp.launch.ChromeLauncher;
4+
import com.github.kklisura.cdtp.protocol.commands.Network;
5+
import com.github.kklisura.cdtp.protocol.commands.Page;
6+
import com.github.kklisura.cdtp.protocol.types.network.ErrorReason;
7+
import com.github.kklisura.cdtp.services.ChromeDevToolsService;
8+
import com.github.kklisura.cdtp.services.ChromeService;
9+
import com.github.kklisura.cdtp.services.types.ChromeTab;
10+
11+
/**
12+
* Intercept and block per URL. Since requestIntercepted event is still Experimental it might not
13+
* work on your browser.
14+
*
15+
* @author Kenan Klisura
16+
*/
17+
public class InterceptAndBlockUrlsExample {
18+
public static void main(String[] args) throws InterruptedException {
19+
// Create chrome launcher.
20+
final ChromeLauncher launcher = new ChromeLauncher();
21+
22+
// Launch chrome either as headless (true) or regular (false).
23+
final ChromeService chromeService = launcher.launch(false);
24+
25+
// Create empty tab ie about:blank.
26+
final ChromeTab tab = chromeService.createTab();
27+
28+
// Get DevTools service to this tab
29+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
30+
31+
// Get individual commands
32+
final Page page = devToolsService.getPage();
33+
final Network network = devToolsService.getNetwork();
34+
35+
network.onRequestIntercepted(
36+
event -> {
37+
String interceptionId = event.getInterceptionId();
38+
boolean blocked = isBlocked(event.getRequest().getUrl());
39+
40+
System.out.printf(
41+
"%s - %s%s",
42+
(blocked ? "BLOCKED" : "ALLOWED"),
43+
event.getRequest().getUrl(),
44+
System.lineSeparator());
45+
46+
ErrorReason errorReason = blocked ? ErrorReason.ABORTED : null;
47+
48+
network.continueInterceptedRequest(
49+
interceptionId, errorReason, null, null, null, null, null, null);
50+
});
51+
52+
page.onLoadEventFired(event -> devToolsService.close());
53+
54+
network.setRequestInterceptionEnabled(Boolean.TRUE);
55+
network.enable();
56+
57+
// Enable page events.
58+
page.enable();
59+
60+
// Navigate to github.com.
61+
page.navigate("http://github.com");
62+
63+
devToolsService.waitUntilClosed();
64+
}
65+
66+
public static boolean isBlocked(String url) {
67+
return url.endsWith(".png") || url.endsWith(".css");
68+
}
69+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.kklisura.cdpt.examples;
2+
3+
import com.github.kklisura.cdtp.launch.ChromeLauncher;
4+
import com.github.kklisura.cdtp.protocol.commands.Page;
5+
import com.github.kklisura.cdtp.services.ChromeDevToolsService;
6+
import com.github.kklisura.cdtp.services.ChromeService;
7+
import com.github.kklisura.cdtp.services.types.ChromeTab;
8+
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.util.Base64;
12+
13+
/**
14+
* Takes a page screenshot.
15+
*
16+
* @author Kenan Klisura
17+
*/
18+
public class TakeScreenshotExample {
19+
public static void main(String[] args) throws InterruptedException {
20+
// Create chrome launcher.
21+
final ChromeLauncher launcher = new ChromeLauncher();
22+
23+
// Launch chrome either as headless (true) or regular (false).
24+
final ChromeService chromeService = launcher.launch(false);
25+
26+
// Create empty tab ie about:blank.
27+
final ChromeTab tab = chromeService.createTab();
28+
29+
// Get DevTools service to this tab
30+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
31+
32+
// Get individual commands
33+
final Page page = devToolsService.getPage();
34+
35+
page.onLoadEventFired(
36+
event -> {
37+
System.out.println("Taking screenshot...");
38+
dump("screenshot.png", page.captureScreenshot());
39+
System.out.println("Done!");
40+
41+
devToolsService.close();
42+
});
43+
44+
// Enable page events.
45+
page.enable();
46+
47+
// Navigate to github.com.
48+
page.navigate("http://github.com");
49+
50+
devToolsService.waitUntilClosed();
51+
}
52+
53+
private static void dump(String fileName, String data) {
54+
FileOutputStream fileOutputStream = null;
55+
try {
56+
File file = new File(fileName);
57+
fileOutputStream = new FileOutputStream(file);
58+
fileOutputStream.write(Base64.getDecoder().decode(data));
59+
} catch (IOException e) {
60+
e.printStackTrace();
61+
} finally{
62+
if (fileOutputStream != null) {
63+
try {
64+
fileOutputStream.flush();
65+
fileOutputStream.close();
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
}
71+
}
72+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.github.kklisura.cdpt.examples;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.github.kklisura.cdtp.launch.ChromeLauncher;
5+
import com.github.kklisura.cdtp.protocol.commands.Page;
6+
import com.github.kklisura.cdtp.protocol.commands.Tracing;
7+
import com.github.kklisura.cdtp.services.ChromeDevToolsService;
8+
import com.github.kklisura.cdtp.services.ChromeService;
9+
import com.github.kklisura.cdtp.services.types.ChromeTab;
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.LinkedList;
13+
import java.util.List;
14+
15+
/**
16+
* Tracing example.
17+
*
18+
* @author Kenan Klisura
19+
*/
20+
public class TracingExample {
21+
public static void main(String[] args) throws InterruptedException {
22+
// Create chrome launcher.
23+
final ChromeLauncher launcher = new ChromeLauncher();
24+
25+
// Launch chrome either as headless (true) or regular (false).
26+
final ChromeService chromeService = launcher.launch(false);
27+
28+
// Create empty tab ie about:blank.
29+
final ChromeTab tab = chromeService.createTab();
30+
31+
// Get DevTools service to this tab
32+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
33+
34+
// Get individual commands
35+
final Page page = devToolsService.getPage();
36+
final Tracing tracing = devToolsService.getTracing();
37+
38+
final List<Object> dataCollectedList = new LinkedList<>();
39+
40+
// Add tracing data to dataCollectedList
41+
tracing.onDataCollected(
42+
event -> {
43+
if (event.getValue() != null) {
44+
dataCollectedList.addAll(event.getValue());
45+
}
46+
});
47+
48+
// When tracing is complete, dump dataCollectedList to JSON file.
49+
tracing.onTracingComplete(
50+
event -> {
51+
// Dump tracing to file.
52+
System.out.println("Tracing completed! Dumping to a file.");
53+
54+
dump("tracing.json", dataCollectedList);
55+
56+
devToolsService.close();
57+
});
58+
59+
page.onLoadEventFired(
60+
event -> {
61+
tracing.end();
62+
});
63+
64+
// Enable page events.
65+
page.enable();
66+
67+
// Start tracing
68+
tracing.start();
69+
70+
// Navigate to github.com.
71+
page.navigate("http://github.com");
72+
73+
devToolsService.waitUntilClosed();
74+
}
75+
76+
private static void dump(String fileName, List<Object> data) {
77+
final ObjectMapper objectMapper = new ObjectMapper();
78+
79+
try {
80+
File file = new File(fileName);
81+
objectMapper.writeValue(file, data);
82+
} catch (IOException e) {
83+
e.printStackTrace();
84+
}
85+
}
86+
}

cdtp-examples/src/resources/logback.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</encoder>
99
</appender>
1010

11-
<!-- By default, the level of the root level is set to DEBUG -->
1211
<root level="INFO">
1312
<appender-ref ref="STDOUT" />
1413
</root>

0 commit comments

Comments
 (0)