46
46
import org .openqa .selenium .remote .tracing .Tracer ;
47
47
48
48
import java .net .URI ;
49
+ import java .net .URISyntaxException ;
49
50
import java .net .URL ;
50
51
import java .time .Instant ;
51
52
import java .util .HashMap ;
52
53
import java .util .Map ;
53
54
import java .util .Optional ;
54
55
import java .util .Set ;
56
+ import java .util .function .Function ;
55
57
import java .util .function .Predicate ;
58
+ import java .util .stream .Stream ;
56
59
57
60
import static org .openqa .selenium .remote .RemoteTags .CAPABILITIES ;
58
61
import static org .openqa .selenium .remote .RemoteTags .CAPABILITIES_EVENT ;
@@ -63,7 +66,7 @@ public class DriverServiceSessionFactory implements SessionFactory {
63
66
private final Tracer tracer ;
64
67
private final HttpClient .Factory clientFactory ;
65
68
private final Predicate <Capabilities > predicate ;
66
- private final DriverService .Builder builder ;
69
+ private final DriverService .Builder <?, ?> builder ;
67
70
private final Capabilities stereotype ;
68
71
private final BrowserOptionsMutator browserOptionsMutator ;
69
72
@@ -72,7 +75,7 @@ public DriverServiceSessionFactory(
72
75
HttpClient .Factory clientFactory ,
73
76
Capabilities stereotype ,
74
77
Predicate <Capabilities > predicate ,
75
- DriverService .Builder builder ) {
78
+ DriverService .Builder <?, ?> builder ) {
76
79
this .tracer = Require .nonNull ("Tracer" , tracer );
77
80
this .clientFactory = Require .nonNull ("HTTP client factory" , clientFactory );
78
81
this .stereotype = ImmutableCapabilities .copyOf (Require .nonNull ("Stereotype" , stereotype ));
@@ -147,16 +150,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
147
150
caps = setInitialPlatform (caps , platformName .get ());
148
151
}
149
152
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 );
160
154
161
155
span .addEvent ("Driver service created session" , attributeMap );
162
156
return Either .right (
@@ -193,8 +187,47 @@ public void stop() {
193
187
}
194
188
}
195
189
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 ;
198
231
}
199
232
200
233
// We set the platform to ANY before sending the caps to the driver because some drivers will
0 commit comments