24
24
import org .openqa .selenium .ImmutableCapabilities ;
25
25
import org .openqa .selenium .MutableCapabilities ;
26
26
import org .openqa .selenium .OutputType ;
27
+ import org .openqa .selenium .PersistentCapabilities ;
27
28
import org .openqa .selenium .Proxy ;
28
29
import org .openqa .selenium .WebDriverException ;
29
30
import org .openqa .selenium .devtools .CdpEndpointFinder ;
52
53
import java .net .URI ;
53
54
import java .net .URISyntaxException ;
54
55
import java .nio .file .Path ;
56
+ import java .util .Optional ;
55
57
import java .util .ServiceLoader ;
56
58
import java .util .Set ;
57
59
import java .util .stream .StreamSupport ;
@@ -160,8 +162,10 @@ public FirefoxDriverCommandExecutor(DriverService service) {
160
162
}
161
163
}
162
164
165
+ private final Capabilities capabilities ;
163
166
protected FirefoxBinary binary ;
164
- private RemoteWebStorage webStorage ;
167
+ private final RemoteWebStorage webStorage ;
168
+ private final Optional <URI > cdpUri ;
165
169
private DevTools devTools ;
166
170
167
171
public FirefoxDriver () {
@@ -201,6 +205,27 @@ public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options) {
201
205
private FirefoxDriver (FirefoxDriverCommandExecutor executor , FirefoxOptions options ) {
202
206
super (executor , dropCapabilities (options ));
203
207
webStorage = new RemoteWebStorage (getExecuteMethod ());
208
+
209
+ Capabilities capabilities = super .getCapabilities ();
210
+ Optional <URI > cdpUri = Optional .empty ();
211
+ if (capabilities .getCapability ("moz:debuggerAddress" ) instanceof String ) {
212
+ try {
213
+ URI providedUri = new URI (String .format ("http://%s" , capabilities .getCapability ("moz:debuggerAddress" )));
214
+ HttpClient .Factory clientFactory = HttpClient .Factory .createDefault ();
215
+
216
+ cdpUri = CdpEndpointFinder .getCdpEndPoint (clientFactory , providedUri );
217
+ } catch (RuntimeException | URISyntaxException e ) {
218
+ // Swallow the exception.
219
+ }
220
+ }
221
+
222
+ this .cdpUri = cdpUri ;
223
+ this .capabilities = cdpUri .map (uri ->
224
+ new ImmutableCapabilities (
225
+ new PersistentCapabilities (capabilities )
226
+ .setCapability ("se:cdp" , uri .toString ())
227
+ .setCapability ("se:cdpVersion" , "86" )))
228
+ .orElse (new ImmutableCapabilities (capabilities ));
204
229
}
205
230
206
231
private static FirefoxDriverCommandExecutor toExecutor (FirefoxOptions options ) {
@@ -220,6 +245,11 @@ private static FirefoxDriverCommandExecutor toExecutor(FirefoxOptions options) {
220
245
return new FirefoxDriverCommandExecutor (builder .withOptions (options ).build ());
221
246
}
222
247
248
+ @ Override
249
+ public Capabilities getCapabilities () {
250
+ return capabilities ;
251
+ }
252
+
223
253
@ Override
224
254
public void setFileDetector (FileDetector detector ) {
225
255
throw new WebDriverException (
@@ -323,27 +353,17 @@ private static Capabilities dropCapabilities(Capabilities capabilities) {
323
353
@ Override
324
354
public DevTools getDevTools () {
325
355
if (devTools == null ) {
326
- Object debuggerAddress = getCapabilities ().getCapability ("moz:debuggerAddress" );
327
- if (debuggerAddress == null ) {
328
- throw new WebDriverException ("This version of Firefox or geckodriver does not support CDP" );
329
- }
356
+ URI wsUri = cdpUri .orElseThrow (() ->
357
+ new DevToolsException ("This version of Firefox or geckodriver does not support CDP" ));
330
358
331
- try {
332
- HttpClient .Factory clientFactory = HttpClient .Factory .createDefault ();
333
-
334
- URI uri = new URI (String .format ("http://%s" , debuggerAddress ));
335
- URI wsUri = CdpEndpointFinder .getCdpEndPoint (clientFactory , uri )
336
- .orElseThrow (() -> new DevToolsException ("Unable to determine URI to connect to from " + debuggerAddress ));
359
+ HttpClient .Factory clientFactory = HttpClient .Factory .createDefault ();
337
360
338
- ClientConfig wsConfig = ClientConfig .defaultConfig ().baseUri (wsUri );
339
- HttpClient wsClient = clientFactory .createClient (wsConfig );
361
+ ClientConfig wsConfig = ClientConfig .defaultConfig ().baseUri (wsUri );
362
+ HttpClient wsClient = clientFactory .createClient (wsConfig );
340
363
341
- Connection connection = new Connection (wsClient , wsUri .toString ());
342
- CdpInfo cdpInfo = new CdpVersionFinder ().match ("86.0" ).orElseGet (NoOpCdpInfo ::new );
343
- devTools = new DevTools (cdpInfo ::getDomains , connection );
344
- } catch (URISyntaxException e ) {
345
- throw new WebDriverException ("Could not initialize DevTools" , e );
346
- }
364
+ Connection connection = new Connection (wsClient , wsUri .toString ());
365
+ CdpInfo cdpInfo = new CdpVersionFinder ().match ("86.0" ).orElseGet (NoOpCdpInfo ::new );
366
+ devTools = new DevTools (cdpInfo ::getDomains , connection );
347
367
}
348
368
return devTools ;
349
369
}
0 commit comments