Skip to content

Commit 9a4ed99

Browse files
committed
Update examples
1 parent ea0622c commit 9a4ed99

File tree

8 files changed

+62
-8
lines changed

8 files changed

+62
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import com.github.kklisura.cdt.services.types.ChromeTab;
4141
* @author Kenan Klisura
4242
*/
4343
public class LogRequestsExample {
44-
public static void main(String[] args) throws InterruptedException {
44+
public static void main(String[] args) {
4545
// Create chrome launcher.
4646
final ChromeLauncher launcher = new ChromeLauncher();
4747

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @author Kenan Klisura
1515
*/
1616
public class BlockUrlGivenPatternExample {
17-
public static void main(String[] args) throws InterruptedException {
17+
public static void main(String[] args) {
1818
// Create chrome launcher.
1919
final ChromeLauncher launcher = new ChromeLauncher();
2020

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.services.ChromeDevToolsService;
7+
import com.github.kklisura.cdt.services.ChromeService;
8+
import com.github.kklisura.cdt.services.types.ChromeTab;
9+
import java.util.Arrays;
10+
11+
/**
12+
* Blocks specific urls from fetching. In this case png and css.
13+
*
14+
* @author Kenan Klisura
15+
*/
16+
public class BlockUrlsExample {
17+
public static void main(String[] args) {
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+
network.setBlockedURLs(Arrays.asList("*.png", "*.css"));
35+
page.onLoadEventFired(event -> devToolsService.close());
36+
37+
network.enable();
38+
39+
// Enable page events.
40+
page.enable();
41+
42+
// Navigate to github.com.
43+
page.navigate("http://github.com");
44+
45+
devToolsService.waitUntilClosed();
46+
}
47+
48+
public static boolean isBlocked(String url) {
49+
return url.endsWith(".png") || url.endsWith(".css");
50+
}
51+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @author Kenan Klisura
1515
*/
1616
public class DumpHtmlFromPageExample {
17-
public static void main(String[] args) throws InterruptedException {
17+
public static void main(String[] args) {
1818
// Create chrome launcher.
1919
final ChromeLauncher launcher = new ChromeLauncher();
2020

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.github.kklisura.cdt.protocol.commands.Network;
55
import com.github.kklisura.cdt.protocol.commands.Page;
66
import com.github.kklisura.cdt.protocol.types.network.ErrorReason;
7+
import com.github.kklisura.cdt.protocol.types.network.RequestPattern;
78
import com.github.kklisura.cdt.services.ChromeDevToolsService;
89
import com.github.kklisura.cdt.services.ChromeService;
910
import com.github.kklisura.cdt.services.types.ChromeTab;
11+
import java.util.Collections;
1012

1113
/**
1214
* Intercept and block per URL. Since requestIntercepted event is still Experimental it might not
@@ -15,7 +17,7 @@
1517
* @author Kenan Klisura
1618
*/
1719
public class InterceptAndBlockUrlsExample {
18-
public static void main(String[] args) throws InterruptedException {
20+
public static void main(String[] args) {
1921
// Create chrome launcher.
2022
final ChromeLauncher launcher = new ChromeLauncher();
2123

@@ -51,7 +53,8 @@ public static void main(String[] args) throws InterruptedException {
5153

5254
page.onLoadEventFired(event -> devToolsService.close());
5355

54-
network.setRequestInterceptionEnabled(Boolean.TRUE);
56+
RequestPattern interceptionRequestPattern = new RequestPattern();
57+
network.setRequestInterception(Collections.singletonList(interceptionRequestPattern));
5558
network.enable();
5659

5760
// Enable page events.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author Kenan Klisura
1717
*/
1818
public class LogRequestsExample {
19-
public static void main(String[] args) throws InterruptedException {
19+
public static void main(String[] args) {
2020
// Create chrome launcher.
2121
final ChromeLauncher launcher = new ChromeLauncher();
2222

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author Kenan Klisura
1717
*/
1818
public class TakeScreenshotExample {
19-
public static void main(String[] args) throws InterruptedException {
19+
public static void main(String[] args) {
2020
// Create chrome launcher.
2121
final ChromeLauncher launcher = new ChromeLauncher();
2222

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @author Kenan Klisura
1919
*/
2020
public class TracingExample {
21-
public static void main(String[] args) throws InterruptedException {
21+
public static void main(String[] args) {
2222
// Create chrome launcher.
2323
final ChromeLauncher launcher = new ChromeLauncher();
2424

0 commit comments

Comments
 (0)