Skip to content

Commit 7e7bcf8

Browse files
committed
make augmentation of HasBiDi/HasDevTools lazy-loaded
Otherwise, command `new Augmenter().augment(remoteWebDriver)` fails immediately (even if I don't want to use CDP or BiDi).
1 parent b731bb4 commit 7e7bcf8

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

java/src/org/openqa/selenium/bidi/BiDiProvider.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,32 @@ public Class<HasBiDi> getDescribedInterface() {
4444

4545
@Override
4646
public HasBiDi getImplementation(Capabilities caps, ExecuteMethod executeMethod) {
47+
return new HasBiDi() {
48+
private volatile Optional<BiDi> biDi;
4749

48-
URI wsUri = getBiDiUrl(caps).orElseThrow(() -> new BiDiException("BiDi not supported"));
50+
@Override
51+
public Optional<BiDi> maybeGetBiDi() {
52+
if (biDi == null) {
53+
synchronized (this) {
54+
if (biDi == null) {
55+
URI wsUri =
56+
getBiDiUrl(caps).orElseThrow(() -> new BiDiException("BiDi not supported"));
4957

50-
HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
51-
ClientConfig wsConfig = ClientConfig.defaultConfig().baseUri(wsUri);
52-
HttpClient wsClient = clientFactory.createClient(wsConfig);
53-
Connection connection = new Connection(wsClient, wsUri.toString());
58+
HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
59+
ClientConfig wsConfig = ClientConfig.defaultConfig().baseUri(wsUri);
60+
HttpClient wsClient = clientFactory.createClient(wsConfig);
61+
Connection connection = new Connection(wsClient, wsUri.toString());
5462

55-
return () -> Optional.of(new BiDi(connection));
63+
biDi = Optional.of(new BiDi(connection));
64+
}
65+
}
66+
}
67+
return biDi;
68+
}
69+
};
5670
}
5771

58-
private Optional<URI> getBiDiUrl(Capabilities caps) {
72+
private static Optional<URI> getBiDiUrl(Capabilities caps) {
5973
Object biDiCapability = caps.getCapability("webSocketUrl");
6074
Optional<String> webSocketUrl = Optional.ofNullable((String) biDiCapability);
6175

java/src/org/openqa/selenium/devtools/DevToolsProvider.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,28 @@ public Class<HasDevTools> getDescribedInterface() {
4242

4343
@Override
4444
public HasDevTools getImplementation(Capabilities caps, ExecuteMethod executeMethod) {
45-
Object cdpVersion = caps.getCapability("se:cdpVersion");
46-
String version = cdpVersion instanceof String ? (String) cdpVersion : caps.getBrowserVersion();
45+
return new HasDevTools() {
46+
private volatile Optional<DevTools> devTools;
4747

48-
CdpInfo info = new CdpVersionFinder().match(version).orElseGet(NoOpCdpInfo::new);
49-
Optional<DevTools> devTools =
50-
SeleniumCdpConnection.create(caps).map(conn -> new DevTools(info::getDomains, conn));
48+
@Override
49+
public Optional<DevTools> maybeGetDevTools() {
50+
if (devTools == null) {
51+
synchronized (this) {
52+
if (devTools == null) {
53+
Object cdpVersion = caps.getCapability("se:cdpVersion");
54+
String version =
55+
cdpVersion instanceof String ? (String) cdpVersion : caps.getBrowserVersion();
5156

52-
return () -> devTools;
57+
CdpInfo info = new CdpVersionFinder().match(version).orElseGet(NoOpCdpInfo::new);
58+
this.devTools =
59+
SeleniumCdpConnection.create(caps)
60+
.map(conn -> new DevTools(info::getDomains, conn));
61+
}
62+
}
63+
}
64+
return devTools;
65+
}
66+
};
5367
}
5468

5569
private String getCdpUrl(Capabilities caps) {

0 commit comments

Comments
 (0)