Skip to content

Commit

Permalink
[CDP] Add all Target domain methods
Browse files Browse the repository at this point in the history
1. add all Target methods
2. adopt DevTools with API Spec
3. Add all Objects
4. Add More tests for all events
5. Add copyright to all Classes
  • Loading branch information
dratler authored and shs96c committed Jun 20, 2019
1 parent 6311d01 commit 6c6d811
Show file tree
Hide file tree
Showing 14 changed files with 1,074 additions and 273 deletions.
5 changes: 3 additions & 2 deletions java/client/src/org/openqa/selenium/devtools/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;

import org.openqa.selenium.devtools.target.model.SessionId;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.remote.http.HttpClient;
Expand Down Expand Up @@ -58,7 +59,7 @@ public Connection(HttpClient client, String url) {
socket = client.openSocket(new HttpRequest(GET, url), new Listener());
}

public <X> CompletableFuture<X> send(Target.SessionId sessionId, Command<X> command) {
public <X> CompletableFuture<X> send(SessionId sessionId, Command<X> command) {
long id = NEXT_ID.getAndIncrement();

CompletableFuture<X> result = new CompletableFuture<>();
Expand All @@ -80,7 +81,7 @@ public <X> CompletableFuture<X> send(Target.SessionId sessionId, Command<X> comm
return result;
}

public <X> X sendAndWait(Target.SessionId sessionId, Command<X> command, Duration timeout) {
public <X> X sendAndWait(SessionId sessionId, Command<X> command, Duration timeout) {
try {
return send(sessionId, command).get(timeout.toMillis(), MILLISECONDS);
} catch (InterruptedException e) {
Expand Down
22 changes: 15 additions & 7 deletions java/client/src/org/openqa/selenium/devtools/DevTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import org.openqa.selenium.devtools.target.Target;
import org.openqa.selenium.devtools.target.model.SessionId;
import org.openqa.selenium.devtools.target.model.TargetId;
import org.openqa.selenium.devtools.target.model.TargetInfo;

import java.io.Closeable;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand All @@ -32,7 +38,7 @@ public class DevTools implements Closeable {

private final Duration timeout = Duration.ofSeconds(10);
private final Connection connection;
private Target.SessionId cdpSession = null;
private SessionId cdpSession = null;

public DevTools(Connection connection) {
this.connection = connection;
Expand All @@ -57,24 +63,26 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {

public void createSession() {
// Figure out the targets.
Set<Target.TargetInfo> infos = connection.sendAndWait(cdpSession, Target.getTargets(), timeout);
List<TargetInfo> infos = connection.sendAndWait(cdpSession, Target.getTargets(), timeout);

// Grab the first "page" type, and glom on to that.
// TODO: Find out which one might be the current one
Target.TargetId targetId = infos.stream()
TargetId targetId = infos.stream()
.filter(info -> "page".equals(info.getType()))
.map(Target.TargetInfo::getTargetId)
.map(TargetInfo::getTargetId)
.findAny()
.orElseThrow(() -> new DevToolsException("Unable to find target id of a page"));

// Start the session.
cdpSession = connection.sendAndWait(cdpSession, Target.attachToTarget(targetId), timeout);
cdpSession =
connection
.sendAndWait(cdpSession, Target.attachToTarget(targetId, Optional.empty()), timeout);

try {
// We can do all of these in parallel, and we don't care about the result.
CompletableFuture.allOf(
// Set auto-attach to true and run for the hills.
connection.send(cdpSession, Target.setAutoAttach(true)),
connection.send(cdpSession, Target.setAutoAttach(true, false, Optional.empty())),
// Clear the existing logs
connection.send(cdpSession, Log.clear()))
.get(timeout.toMillis(), MILLISECONDS);
Expand Down
253 changes: 0 additions & 253 deletions java/client/src/org/openqa/selenium/devtools/Target.java

This file was deleted.

0 comments on commit 6c6d811

Please sign in to comment.