-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Expand file tree
/
Copy pathCdpEndpointFinder.java
More file actions
133 lines (113 loc) · 4.3 KB
/
Copy pathCdpEndpointFinder.java
File metadata and controls
133 lines (113 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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 java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
public class CdpEndpointFinder {
private static final Logger LOG = Logger.getLogger(CdpEndpointFinder.class.getName());
private static final Json JSON = new Json();
/**
* @deprecated Use {@link #getHttpClient(HttpClient.Factory, URI, ClientConfig)} instead
*/
@Deprecated
public static HttpClient getHttpClient(HttpClient.Factory clientFactory, URI reportedUri) {
return getHttpClient(clientFactory, reportedUri, ClientConfig.defaultConfig());
}
public static HttpClient getHttpClient(
HttpClient.Factory clientFactory, URI reportedUri, ClientConfig clientConfig) {
Require.nonNull("HTTP client factory", clientFactory);
Require.nonNull("DevTools URI", reportedUri);
ClientConfig config = clientConfig.baseUri(reportedUri);
return clientFactory.createClient(config);
}
public static Optional<URI> getCdpEndPoint(HttpClient client) {
Require.nonNull("HTTP client", client);
HttpResponse res;
try {
res = client.execute(new HttpRequest(GET, "/json/version"));
} catch (UncheckedIOException e) {
LOG.warning("Unable to connect to determine websocket url: " + e.getMessage());
return Optional.empty();
}
if (res.getStatus() != HTTP_OK) {
return Optional.empty();
}
Map<String, Object> versionData = JSON.toType(res.contentAsString(), MAP_TYPE);
Object raw = versionData.get("webSocketDebuggerUrl");
if (!(raw instanceof String)) {
return Optional.empty();
}
String debuggerUrl = (String) raw;
try {
return Optional.of(new URI(debuggerUrl));
} catch (URISyntaxException e) {
LOG.warning("Invalid URI for endpoint " + raw);
return Optional.empty();
}
}
public static Optional<URI> getReportedUri(Capabilities caps) {
String key;
switch (caps.getBrowserName()) {
case "chrome":
key = "goog:chromeOptions";
break;
case "msedge":
key = "ms:edgeOptions";
break;
default:
return Optional.empty();
}
return getReportedUri(key, caps);
}
public static Optional<URI> getReportedUri(String capabilityKey, Capabilities caps) {
Object raw = caps.getCapability(capabilityKey);
if ((raw instanceof Map)) {
raw = ((Map<?, ?>) raw).get("debuggerAddress");
}
if (!(raw instanceof String)) {
LOG.fine("No debugger address");
return Optional.empty();
}
int index = ((String) raw).lastIndexOf(':');
if (index == -1 || index == ((String) raw).length() - 1) {
LOG.fine("No index in " + raw);
return Optional.empty();
}
try {
URI uri = new URI(String.format("http://%s", raw));
LOG.fine("URI found: " + uri);
return Optional.of(uri);
} catch (URISyntaxException e) {
LOG.warning("Unable to create URI from: " + raw);
return Optional.empty();
}
}
}