Skip to content

Commit 14429fa

Browse files
committed
Add html dump example
1 parent 6f76368 commit 14429fa

File tree

13 files changed

+599
-57
lines changed

13 files changed

+599
-57
lines changed

cdtp-examples/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,39 @@
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<maven.compiler.source>1.8</maven.compiler.source>
1616
<maven.compiler.target>1.8</maven.compiler.target>
17+
18+
<logback.version>1.2.3</logback.version>
1719
</properties>
1820

1921
<dependencies>
22+
<dependency>
23+
<groupId>ch.qos.logback</groupId>
24+
<artifactId>logback-classic</artifactId>
25+
<version>${logback.version}</version>
26+
</dependency>
27+
2028
<dependency>
2129
<groupId>com.github.kklisura.cdtp</groupId>
2230
<artifactId>cdtp-java-client</artifactId>
2331
<version>1.0-SNAPSHOT</version>
2432
</dependency>
2533
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>com.coveo</groupId>
39+
<artifactId>fmt-maven-plugin</artifactId>
40+
<version>2.1.0</version>
41+
<executions>
42+
<execution>
43+
<goals>
44+
<goal>format</goal>
45+
</goals>
46+
<phase>process-sources</phase>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
2652
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.protocol.commands.Runtime;
6+
import com.github.kklisura.cdtp.protocol.types.runtime.Evaluate;
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+
* The following example dumps the index html from github.com.
13+
*
14+
* @author Kenan Klisura
15+
*/
16+
public class DumpHtmlFromPageExample {
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 Runtime runtime = devToolsService.getRuntime();
33+
34+
// Wait for on load event
35+
page.onLoadEventFired(
36+
event -> {
37+
// Evaluate javascript
38+
Evaluate evaluation = runtime.evaluate("document.documentElement.outerHTML");
39+
System.out.println(evaluation.getResult().getValue());
40+
41+
// Close devtools.
42+
devToolsService.close();
43+
});
44+
45+
// Enable page events.
46+
page.enable();
47+
48+
// Navigate to github.com.
49+
page.navigate("http://github.com");
50+
51+
// Wait until devtools is closed.
52+
devToolsService.waitUntilClosed();
53+
54+
// Close tab.
55+
chromeService.closeTab(tab);
56+
}
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
10+
/**
11+
* Log requests example with DevTools Protocol java client.
12+
*
13+
* <p>The following example will open chrome, create a tab with about:blank url, subscribe to
14+
* requestWillBeSent event and then navigate to github.com.
15+
*
16+
* @author Kenan Klisura
17+
*/
18+
public class LogRequestsExample {
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+
final Network network = devToolsService.getNetwork();
35+
36+
// Log requests with onRequestWillBeSent event handler.
37+
network.onRequestWillBeSent(
38+
event ->
39+
System.out.printf(
40+
"request: %s %s%s",
41+
event.getRequest().getMethod(),
42+
event.getRequest().getUrl(),
43+
System.lineSeparator()));
44+
45+
network.onLoadingFinished(
46+
event -> {
47+
// Close the tab and close the browser when loading finishes.
48+
chromeService.closeTab(tab);
49+
launcher.close();
50+
});
51+
52+
// Enable network events.
53+
network.enable();
54+
55+
// Navigate to github.com.
56+
page.navigate("http://github.com");
57+
}
58+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Simple navigate-to-url example with DevTools Protocol java client.
1111
*
12-
* The following example with open chrome, create a tab with about:blank url and then navigate to
12+
* <p>The following example will open chrome, create a tab with about:blank url and then navigate to
1313
* github.com. It will then wait 2seconds and it will navigate to twitter.com. It will again wait
1414
* for 2seconds and on the end it will close the tab and close the browser.
1515
*

cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/ChromeDevToolsService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public interface ChromeDevToolsService extends ChromeDevTools, AutoCloseable {
4747
/** Closes the dev tools service. */
4848
void close();
4949

50+
/**
51+
* Returns true if devtools is closed.
52+
*
53+
* @return True if its closed.
54+
*/
55+
boolean isClosed();
56+
57+
/** Waits until devtools is closed. */
58+
void waitUntilClosed();
59+
5060
/**
5161
* Adds an event listener on a given event name belonging to some domain.
5262
*

cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/ChromeService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.github.kklisura.cdtp.services.config.ChromeDevToolsServiceConfiguration;
2324
import com.github.kklisura.cdtp.services.exceptions.ChromeServiceException;
2425
import com.github.kklisura.cdtp.services.types.ChromeTab;
2526
import com.github.kklisura.cdtp.services.types.ChromeVersion;
@@ -84,6 +85,17 @@ public interface ChromeService {
8485
* Creates a dev tools service to specified tab.
8586
*
8687
* @param tab Tab.
88+
* @param chromeDevToolsServiceConfiguration Service configuration.
89+
* @return Dev tools.
90+
*/
91+
ChromeDevToolsService createDevToolsService(
92+
ChromeTab tab, ChromeDevToolsServiceConfiguration chromeDevToolsServiceConfiguration)
93+
throws ChromeServiceException;
94+
95+
/**
96+
* Creates a dev tools service to specified tab with default service configuration.
97+
*
98+
* @param tab Tab.
8799
* @return Dev tools.
88100
*/
89101
ChromeDevToolsService createDevToolsService(ChromeTab tab) throws ChromeServiceException;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.github.kklisura.cdtp.services.config;
2+
3+
/*-
4+
* #%L
5+
* cdpt-java-client
6+
* %%
7+
* Copyright (C) 2018 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import static com.github.kklisura.cdtp.services.utils.ConfigurationUtils.systemProperty;
24+
25+
import com.github.kklisura.cdtp.services.executors.DefaultEventExecutorService;
26+
import com.github.kklisura.cdtp.services.executors.EventExecutorService;
27+
28+
/**
29+
* Chrome DevTools service configuration.
30+
*
31+
* @author Kenan Klisura
32+
*/
33+
public class ChromeDevToolsServiceConfiguration {
34+
private static final String READ_TIMEOUT_PROPERTY =
35+
"com.github.kklisura.cdtp.services.config.readTimeout";
36+
37+
private static final long READ_TIMEOUT = systemProperty(READ_TIMEOUT_PROPERTY, 0);
38+
39+
/** Read timeout in seconds. Default 0. */
40+
private long readTimeout = READ_TIMEOUT;
41+
42+
private EventExecutorService eventExecutorService = new DefaultEventExecutorService();
43+
44+
/**
45+
* Gets read timeout in seconds.
46+
*
47+
* @return Read timeout in seconds.
48+
*/
49+
public long getReadTimeout() {
50+
return readTimeout;
51+
}
52+
53+
/**
54+
* Sets read timeout in seconds. 0 for infinite timeout.
55+
*
56+
* <p>This property can be set by {@link ChromeDevToolsServiceConfiguration#READ_TIMEOUT_PROPERTY}
57+
* property.
58+
*
59+
* @param readTimeout Read timeout in seconds.
60+
*/
61+
public void setReadTimeout(long readTimeout) {
62+
this.readTimeout = readTimeout;
63+
}
64+
65+
/**
66+
* Gets event executor service.
67+
*
68+
* @return Event executor service.
69+
*/
70+
public EventExecutorService getEventExecutorService() {
71+
return eventExecutorService;
72+
}
73+
74+
/**
75+
* Sets event executor service.
76+
*
77+
* @param eventExecutorService Event executor service.
78+
*/
79+
public void setEventExecutorService(EventExecutorService eventExecutorService) {
80+
this.eventExecutorService = eventExecutorService;
81+
}
82+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.github.kklisura.cdtp.services.executors;
2+
3+
/*-
4+
* #%L
5+
* cdpt-java-client
6+
* %%
7+
* Copyright (C) 2018 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import java.util.concurrent.ExecutorService;
24+
import java.util.concurrent.Executors;
25+
26+
/**
27+
* Default event executor service.
28+
*
29+
* @author Kenan Klisura
30+
*/
31+
public class DefaultEventExecutorService implements EventExecutorService {
32+
private ExecutorService executorService = Executors.newCachedThreadPool();
33+
34+
@Override
35+
public void execute(Runnable runnable) {
36+
executorService.execute(runnable);
37+
}
38+
39+
@Override
40+
public void shutdown() {
41+
executorService.shutdown();
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.kklisura.cdtp.services.executors;
2+
3+
/*-
4+
* #%L
5+
* cdpt-java-client
6+
* %%
7+
* Copyright (C) 2018 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
/**
24+
* Event executor service.
25+
*
26+
* @author Kenan Klisura
27+
*/
28+
public interface EventExecutorService {
29+
/**
30+
* Executes a runnable in separate thread.
31+
*
32+
* @param runnable Runnable.
33+
*/
34+
void execute(Runnable runnable);
35+
36+
/** Shutdowns executor service. */
37+
void shutdown();
38+
}

0 commit comments

Comments
 (0)