Skip to content

Commit 17d8a37

Browse files
committed
[grid] Have the node server rewrite cdp endpoints and versions
1 parent 48e0d17 commit 17d8a37

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

java/server/src/org/openqa/selenium/grid/node/config/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ java_library(
1111
deps = [
1212
"//java:auto-service",
1313
"//java/client/src/org/openqa/selenium/chromium",
14+
"//java/client/src/org/openqa/selenium/devtools",
1415
"//java/client/src/org/openqa/selenium/json",
1516
"//java/client/src/org/openqa/selenium/remote",
1617
"//java/server/src/org/openqa/selenium/grid/config",

java/server/src/org/openqa/selenium/grid/node/config/DriverServiceSessionFactory.java

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@
4646
import org.openqa.selenium.remote.tracing.Tracer;
4747

4848
import java.net.URI;
49+
import java.net.URISyntaxException;
4950
import java.net.URL;
5051
import java.time.Instant;
5152
import java.util.HashMap;
5253
import java.util.Map;
5354
import java.util.Optional;
5455
import java.util.Set;
56+
import java.util.function.Function;
5557
import java.util.function.Predicate;
58+
import java.util.stream.Stream;
5659

5760
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
5861
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;
@@ -63,7 +66,7 @@ public class DriverServiceSessionFactory implements SessionFactory {
6366
private final Tracer tracer;
6467
private final HttpClient.Factory clientFactory;
6568
private final Predicate<Capabilities> predicate;
66-
private final DriverService.Builder builder;
69+
private final DriverService.Builder<?, ?> builder;
6770
private final Capabilities stereotype;
6871
private final BrowserOptionsMutator browserOptionsMutator;
6972

@@ -72,7 +75,7 @@ public DriverServiceSessionFactory(
7275
HttpClient.Factory clientFactory,
7376
Capabilities stereotype,
7477
Predicate<Capabilities> predicate,
75-
DriverService.Builder builder) {
78+
DriverService.Builder<?, ?> builder) {
7679
this.tracer = Require.nonNull("Tracer", tracer);
7780
this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);
7881
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
@@ -147,16 +150,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
147150
caps = setInitialPlatform(caps, platformName.get());
148151
}
149152

150-
Optional<URI> reportedUri =
151-
ChromiumDevToolsLocator.getReportedUri("goog:chromeOptions", caps);
152-
if (reportedUri.isPresent()) {
153-
caps = addCdpCapability(caps, reportedUri.get());
154-
} else {
155-
reportedUri = ChromiumDevToolsLocator.getReportedUri("ms:edgeOptions", caps);
156-
if (reportedUri.isPresent()) {
157-
caps = addCdpCapability(caps, reportedUri.get());
158-
}
159-
}
153+
caps = readDevToolsEndpointAndVersion(caps);
160154

161155
span.addEvent("Driver service created session", attributeMap);
162156
return Either.right(
@@ -193,8 +187,47 @@ public void stop() {
193187
}
194188
}
195189

196-
private Capabilities addCdpCapability(Capabilities caps, URI uri) {
197-
return new PersistentCapabilities(caps).setCapability("se:cdp", uri);
190+
private Capabilities readDevToolsEndpointAndVersion(Capabilities caps) {
191+
class DevToolsInfo {
192+
public final URI cdpEndpoint;
193+
public final String version;
194+
195+
public DevToolsInfo(URI cdpEndpoint, String version) {
196+
this.cdpEndpoint = cdpEndpoint;
197+
this.version = version;
198+
}
199+
}
200+
201+
Function<Capabilities, Optional<DevToolsInfo>> chrome = c ->
202+
ChromiumDevToolsLocator.getReportedUri("goog:chromeOptions", c).map(uri -> new DevToolsInfo(uri, c.getBrowserVersion()));
203+
204+
Function<Capabilities, Optional<DevToolsInfo>> edge = c ->
205+
ChromiumDevToolsLocator.getReportedUri("ms:edgeOptions", c).map(uri -> new DevToolsInfo(uri, c.getBrowserVersion()));
206+
207+
Function<Capabilities, Optional<DevToolsInfo>> firefox = c -> {
208+
Object address = c.getCapability("moz:debuggerAddress");
209+
return Optional.ofNullable(address).map(adr -> {
210+
try {
211+
URI uri = new URI(String.format("http://%s", adr));
212+
return new DevToolsInfo(uri, "86");
213+
} catch (URISyntaxException e) {
214+
return null;
215+
}
216+
});
217+
};
218+
219+
Optional<DevToolsInfo> maybeInfo = Stream.of(chrome, edge, firefox)
220+
.map(finder -> finder.apply(caps))
221+
.filter(Optional::isPresent)
222+
.map(Optional::get)
223+
.findFirst();
224+
if (maybeInfo.isPresent()) {
225+
DevToolsInfo info = maybeInfo.get();
226+
return new PersistentCapabilities(caps)
227+
.setCapability("se:cdp", info.cdpEndpoint)
228+
.setCapability("se:cdpVersion", info.version);
229+
}
230+
return caps;
198231
}
199232

200233
// We set the platform to ANY before sending the caps to the driver because some drivers will

java/server/src/org/openqa/selenium/grid/node/config/NodeOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public Duration getRegisterCycle() {
9898
int seconds = Math.max(
9999
config.getInt(NODE_SECTION, "register-cycle").orElse(DEFAULT_REGISTER_CYCLE),
100100
1);
101+
101102
return Duration.ofSeconds(seconds);
102103
}
103104

@@ -106,6 +107,7 @@ public Duration getRegisterPeriod() {
106107
int seconds = Math.max(
107108
config.getInt(NODE_SECTION, "register-period").orElse(DEFAULT_REGISTER_PERIOD),
108109
1);
110+
109111
return Duration.ofSeconds(seconds);
110112
}
111113

java/server/src/org/openqa/selenium/grid/node/httpd/NodeServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.openqa.selenium.grid.TemplateGridServerCommand;
3131
import org.openqa.selenium.grid.config.CompoundConfig;
3232
import org.openqa.selenium.grid.config.Config;
33+
import org.openqa.selenium.grid.config.ConfigFlags;
3334
import org.openqa.selenium.grid.config.MemoizedConfig;
3435
import org.openqa.selenium.grid.config.Role;
3536
import org.openqa.selenium.grid.data.NodeAddedEvent;

java/server/src/org/openqa/selenium/grid/node/local/LocalNode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,10 @@ public Either<WebDriverException, CreateSessionResponse> newSession(CreateSessio
341341

342342
// The session we return has to look like it came from the node, since we might be dealing
343343
// with a webdriver implementation that only accepts connections from localhost
344-
Session externalSession = createExternalSession(session, externalUri, slotToUse.isSupportingCdp());
344+
Session externalSession = createExternalSession(
345+
session,
346+
externalUri,
347+
slotToUse.isSupportingCdp() || caps.getCapability("se:cdp") != null);
345348
return Either.right(new CreateSessionResponse(
346349
externalSession,
347350
getEncoder(session.getDownstreamDialect()).apply(externalSession)));

0 commit comments

Comments
 (0)