2727
2828import org .openqa .selenium .Capabilities ;
2929import org .openqa .selenium .ImmutableCapabilities ;
30+ import org .openqa .selenium .MutableCapabilities ;
3031import org .openqa .selenium .NoSuchSessionException ;
3132import org .openqa .selenium .PersistentCapabilities ;
3233import org .openqa .selenium .RetrySessionRequestException ;
@@ -118,6 +119,7 @@ public class LocalNode extends Node {
118119 private final HealthCheck healthCheck ;
119120 private final int maxSessionCount ;
120121 private final int configuredSessionCount ;
122+ private final boolean cdpEnabled ;
121123 private final AtomicBoolean drainAfterSessions = new AtomicBoolean ();
122124 private final List <SessionSlot > factories ;
123125 private final Cache <SessionId , SessionSlot > currentSessions ;
@@ -133,6 +135,7 @@ private LocalNode(
133135 HealthCheck healthCheck ,
134136 int maxSessionCount ,
135137 int drainAfterSessionCount ,
138+ boolean cdpEnabled ,
136139 Ticker ticker ,
137140 Duration sessionTimeout ,
138141 Duration heartbeatPeriod ,
@@ -152,6 +155,7 @@ private LocalNode(
152155 this .configuredSessionCount = drainAfterSessionCount ;
153156 this .drainAfterSessions .set (this .configuredSessionCount > 0 );
154157 this .sessionCount .set (drainAfterSessionCount );
158+ this .cdpEnabled = cdpEnabled ;
155159
156160 this .healthCheck = healthCheck == null ?
157161 () -> {
@@ -512,9 +516,18 @@ private Session createExternalSession(ActiveSession other, URI externalUri,
512516 .copyOf (requestCapabilities .merge (other .getCapabilities ()));
513517
514518 // Add se:cdp if necessary to send the cdp url back
515- if (isSupportingCdp || toUse .getCapability ("se:cdp" ) != null ) {
519+ if (( isSupportingCdp || toUse .getCapability ("se:cdp" ) != null ) && cdpEnabled ) {
516520 String cdpPath = String .format ("/session/%s/se/cdp" , other .getId ());
517521 toUse = new PersistentCapabilities (toUse ).setCapability ("se:cdp" , rewrite (cdpPath ));
522+ } else {
523+ // Remove any se:cdp* from the response, CDP is not supported nor enabled
524+ MutableCapabilities cdpFiltered = new MutableCapabilities ();
525+ toUse .asMap ().forEach ((key , value ) -> {
526+ if (!key .startsWith ("se:cdp" )) {
527+ cdpFiltered .setCapability (key , value );
528+ }
529+ });
530+ toUse = new PersistentCapabilities (cdpFiltered ).setCapability ("se:cdpEnabled" , false );
518531 }
519532
520533 // If enabled, set the VNC endpoint for live view
@@ -644,6 +657,7 @@ public static class Builder {
644657 private final ImmutableList .Builder <SessionSlot > factories ;
645658 private int maxSessions = NodeOptions .DEFAULT_MAX_SESSIONS ;
646659 private int drainAfterSessionCount = NodeOptions .DEFAULT_DRAIN_AFTER_SESSION_COUNT ;
660+ private boolean cdpEnabled = NodeOptions .DEFAULT_ENABLE_CDP ;
647661 private Ticker ticker = Ticker .systemTicker ();
648662 private Duration sessionTimeout = Duration .ofSeconds (NodeOptions .DEFAULT_SESSION_TIMEOUT );
649663 private HealthCheck healthCheck ;
@@ -682,6 +696,11 @@ public Builder drainAfterSessionCount(int sessionCount) {
682696 return this ;
683697 }
684698
699+ public Builder enableCdp (boolean cdpEnabled ) {
700+ this .cdpEnabled = cdpEnabled ;
701+ return this ;
702+ }
703+
685704 public Builder sessionTimeout (Duration timeout ) {
686705 sessionTimeout = timeout ;
687706 return this ;
@@ -701,6 +720,7 @@ public LocalNode build() {
701720 healthCheck ,
702721 maxSessions ,
703722 drainAfterSessionCount ,
723+ cdpEnabled ,
704724 ticker ,
705725 sessionTimeout ,
706726 heartbeatPeriod ,
0 commit comments