2020import java .net .URI ;
2121import java .net .URISyntaxException ;
2222import java .util .Optional ;
23+ import java .util .logging .Level ;
24+ import java .util .logging .Logger ;
2325import org .openqa .selenium .Capabilities ;
2426import org .openqa .selenium .HasCapabilities ;
2527import org .openqa .selenium .WebDriver ;
2628import org .openqa .selenium .internal .Require ;
27- import org .openqa .selenium .remote .http .ClientConfig ;
2829import org .openqa .selenium .remote .http .HttpClient ;
2930
3031public class SeleniumCdpConnection extends Connection {
3132
33+ private static final Logger LOG = Logger .getLogger (SeleniumCdpConnection .class .getName ());
34+
3235 private SeleniumCdpConnection (HttpClient client , String url ) {
3336 super (client , url );
3437 }
@@ -51,28 +54,48 @@ public static Optional<Connection> create(
5154 Require .nonNull ("HTTP client factory" , clientFactory );
5255 Require .nonNull ("Capabilities" , capabilities );
5356
54- return getCdpUri (clientFactory , capabilities )
55- .map (
56- uri ->
57- new SeleniumCdpConnection (
58- clientFactory .createClient (ClientConfig .defaultConfig ().baseUri (uri )),
59- uri .toString ()));
60- }
6157
62- public static Optional <URI > getCdpUri (
63- HttpClient .Factory clientFactory , Capabilities capabilities ) {
64- Object cdp = capabilities .getCapability ("se:cdp" );
58+ Optional <URI > cdpUri = Optional
59+ .ofNullable (capabilities .getCapability ("se:cdp" ))
60+ .flatMap ((uri ) -> {
61+ if (uri instanceof String ) {
62+ try {
63+ return Optional .of (new URI ((String ) uri ));
64+ } catch (URISyntaxException e ) {
65+ return Optional .empty ();
66+ }
67+ }
68+ return Optional .empty ();
69+ });
70+
71+ Optional <HttpClient > client ;
72+
73+ if (cdpUri .isPresent ()) {
74+ client = Optional .of (CdpEndpointFinder .getHttpClient (clientFactory , cdpUri .get ()));
75+ } else {
76+ Optional <URI > reportedUri = CdpEndpointFinder .getReportedUri (capabilities );
77+ client = reportedUri .map (uri -> CdpEndpointFinder .getHttpClient (clientFactory , uri ));
6578
66- if (cdp instanceof String ) {
6779 try {
68- return Optional .of (new URI ((String ) cdp ));
69- } catch (URISyntaxException e ) {
70- return Optional .empty ();
80+ cdpUri = client .flatMap (httpClient -> CdpEndpointFinder .getCdpEndPoint (httpClient ));
81+ } catch (Exception e ) {
82+ try {
83+ client .ifPresent (HttpClient ::close );
84+ } catch (Exception ex ) {
85+ e .addSuppressed (ex );
86+ }
87+ throw e ;
7188 }
72- }
7389
74- Optional <URI > reportedUri = CdpEndpointFinder .getReportedUri (capabilities );
90+ if (!cdpUri .isPresent ()) {
91+ try {
92+ client .ifPresent (HttpClient ::close );
93+ } catch (Exception e ) {
94+ LOG .log (Level .FINE , "failed to close the http client used to check the reported CDP endpoint: " + reportedUri .get (), e );
95+ }
96+ }
97+ }
7598
76- return reportedUri . flatMap (uri -> CdpEndpointFinder . getCdpEndPoint ( clientFactory , uri ));
99+ return cdpUri . map (uri -> new SeleniumCdpConnection ( client . get () , uri . toString () ));
77100 }
78101}
0 commit comments