-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[devtools] Provide a basic CDP implementation
This currently always assumes that it's possible to make a direct connection to the CDP host, which may not always be true.
- Loading branch information
Showing
6 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class Command { | ||
|
||
private static final AtomicLong NEXT_ID = new AtomicLong(1); | ||
|
||
private final long id; | ||
private final String method; | ||
private final Map<String, Object> params; | ||
|
||
public Command(String method, Map<String, Object> params) { | ||
this(NEXT_ID.getAndIncrement(), method, params); | ||
} | ||
|
||
public Command(long id, String method, Map<String, Object> params) { | ||
this.id = id; | ||
this.method = Objects.requireNonNull(method, "Method name must be set."); | ||
this.params = ImmutableMap.copyOf(Objects.requireNonNull(params, "Command parameters must be set.")); | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
private Map<String, Object> toJson() { | ||
return ImmutableMap.of( | ||
"id", id, | ||
"method", method, | ||
"params", params); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
java/client/src/org/openqa/selenium/devtools/Connection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
import static org.openqa.selenium.json.Json.MAP_TYPE; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import org.openqa.selenium.json.Json; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.WebSocket; | ||
import okhttp3.WebSocketListener; | ||
|
||
import java.io.Closeable; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class Connection implements Closeable { | ||
|
||
private static final Json JSON = new Json(); | ||
private final WebSocket socket; | ||
private final OkHttpClient client; | ||
|
||
public Connection(String url) { | ||
Objects.requireNonNull(url, "URL to connect to must be set."); | ||
|
||
client = new OkHttpClient.Builder().build(); | ||
|
||
Request request = new Request.Builder() | ||
.url(url) | ||
.build(); | ||
|
||
socket = client.newWebSocket(request, new Listener()); | ||
} | ||
|
||
public void send(Command command) { | ||
socket.send(JSON.toJson(command)); | ||
} | ||
|
||
public void onMessage(Message message) { | ||
System.out.println(message); | ||
} | ||
|
||
public void onEvent(Event event) { | ||
System.out.println(event); | ||
} | ||
|
||
|
||
@Override | ||
public void close() { | ||
socket.close(1000, "Exiting"); | ||
client.dispatcher().executorService().shutdown(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
try (Connection connection = new Connection( | ||
"http://127.0.0.1:9222/devtools/browser/cbd28705-5dfd-4c6c-b665-64940e252078")) { | ||
Command command = new Command("Target.setDiscoverTargets", ImmutableMap.of("discover", true)); | ||
connection.send(command); | ||
} | ||
} | ||
|
||
private class Listener extends WebSocketListener { | ||
|
||
@Override | ||
public void onMessage(WebSocket webSocket, String text) { | ||
Map<String, Object> raw = JSON.toType(text, MAP_TYPE); | ||
if (raw.get("id") instanceof Number && raw.get("result") != null) { | ||
Message message = new Message(((Number) raw.get("id")).longValue(), raw.get("result")); | ||
Connection.this.onMessage(message); | ||
} else if (raw.get("method") instanceof String && raw.get("params") instanceof Map) { | ||
Event event = new Event( | ||
(String) raw.get("method"), | ||
(Map<?, ?>) raw.get("params")); | ||
onEvent(event); | ||
} else { | ||
System.out.println("Unhandled type: " + text); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onClosing(WebSocket webSocket, int code, String reason) { | ||
super.onClosing(webSocket, code, reason); | ||
} | ||
|
||
@Override | ||
public void onFailure(WebSocket webSocket, Throwable t, Response response) { | ||
super.onFailure(webSocket, t, response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
public class Event { | ||
|
||
private final String method; | ||
private final Map<String, Object> params; | ||
|
||
public Event(String method, Map<?, ?> params) { | ||
this.method = Objects.requireNonNull(method, "Event method must be set."); | ||
this.params = params == null ? | ||
ImmutableMap.of() : | ||
params.entrySet().stream() | ||
.map(entry -> new AbstractMap.SimpleEntry<String, Object>( | ||
String.valueOf(entry.getKey()), entry.getValue())) | ||
.collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", Event.class.getSimpleName() + "[", "]") | ||
.add("method='" + method + "'") | ||
.add("params=" + params) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
import java.util.StringJoiner; | ||
|
||
public class Message { | ||
|
||
private final long id; | ||
private final Object result; | ||
|
||
public Message(long id, Object result) { | ||
this.id = id; | ||
this.result = result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", Message.class.getSimpleName() + "[", "]") | ||
.add("id=" + id) | ||
.add("result=" + result) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
public class Reply { | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
java/client/test/org/openqa/selenium/devtools/DevToolsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.concurrent.Future; | ||
|
||
public class DevToolsTest { | ||
|
||
@Test | ||
public void bootstrap() { | ||
// String rawResult = "{\"id\":1,\"result\":{}}"; | ||
// | ||
// Connection connection = new Connection() { | ||
// | ||
// }; | ||
// | ||
// DevTools devTools = new DevTools(connection); | ||
// Future<Reply> future = devTools.listenFor(reply -> reply.getId() == 1); | ||
} | ||
|
||
@Test | ||
public void boostrap2() { | ||
// String rawEvent = | ||
// "{\"method\":\"Target.targetCreated\"," + | ||
// "\"params\":{\"targetInfo\":{\"targetId\":\"92C067F8FA440F318ED1B8945C029B47\"," + | ||
// "\"type\":\"page\",\"title\":\"\",\"url\":\"about:blank\"," + | ||
// "\"attached\":false,\"browserContextId\":\"CBDE829A8078645243E8567CB24C3B9C\"}}}"; | ||
// | ||
// Connection connection = new Connection() { | ||
// | ||
// }; | ||
// | ||
// DevTools devTools = new DevTools(connection); | ||
// devTools.on(reply -> reply.getId() == 1) | ||
} | ||
|
||
} |