1818package org .openqa .selenium .grid .data ;
1919
2020import java .io .Serializable ;
21- import java .util .Arrays ;
2221import java .util .List ;
22+ import java .util .Map ;
2323import java .util .Objects ;
2424import org .openqa .selenium .Capabilities ;
2525
4444 */
4545public class DefaultSlotMatcher implements SlotMatcher , Serializable {
4646
47- /*
48- List of prefixed extension capabilities we never should try to match, they should be
49- matched in the Node or in the browser driver.
50- */
51- private static final List <String > EXTENSION_CAPABILITIES_PREFIXES =
52- Arrays .asList ("goog:" , "moz:" , "ms:" , "se:" );
53-
5447 @ Override
5548 public boolean matches (Capabilities stereotype , Capabilities capabilities ) {
5649
@@ -76,14 +69,14 @@ public boolean matches(Capabilities stereotype, Capabilities capabilities) {
7669
7770 // At the end, a simple browser, browserVersion and platformName match
7871 boolean browserNameMatch =
79- (capabilities .getBrowserName () == null || capabilities .getBrowserName ().isEmpty ())
72+ capabilities .getBrowserName () == null
73+ || capabilities .getBrowserName ().isEmpty ()
8074 || Objects .equals (stereotype .getBrowserName (), capabilities .getBrowserName ());
8175 boolean browserVersionMatch =
82- (capabilities .getBrowserVersion () == null
83- || capabilities .getBrowserVersion ().isEmpty ()
84- || Objects .equals (capabilities .getBrowserVersion (), "stable" ))
85- || browserVersionMatch (
86- stereotype .getBrowserVersion (), capabilities .getBrowserVersion ());
76+ capabilities .getBrowserVersion () == null
77+ || capabilities .getBrowserVersion ().isEmpty ()
78+ || Objects .equals (capabilities .getBrowserVersion (), "stable" )
79+ || browserVersionMatch (stereotype .getBrowserVersion (), capabilities .getBrowserVersion ());
8780 boolean platformNameMatch =
8881 capabilities .getPlatformName () == null
8982 || Objects .equals (stereotype .getPlatformName (), capabilities .getPlatformName ())
@@ -102,21 +95,17 @@ private Boolean initialMatch(Capabilities stereotype, Capabilities capabilities)
10295 .filter (name -> !name .contains (":" ))
10396 // Platform matching is special, we do it later
10497 .filter (name -> !"platformName" .equalsIgnoreCase (name ))
105- .map (
98+ .filter (name -> capabilities .getCapability (name ) != null )
99+ .allMatch (
106100 name -> {
107- if (capabilities .getCapability (name ) instanceof String ) {
108- return stereotype
109- .getCapability (name )
110- .toString ()
111- .equalsIgnoreCase (capabilities .getCapability (name ).toString ());
112- } else {
113- return capabilities .getCapability (name ) == null
114- || Objects .equals (
115- stereotype .getCapability (name ), capabilities .getCapability (name ));
101+ if (stereotype .getCapability (name ) instanceof String
102+ && capabilities .getCapability (name ) instanceof String ) {
103+ return ((String ) stereotype .getCapability (name ))
104+ .equalsIgnoreCase ((String ) capabilities .getCapability (name ));
116105 }
117- })
118- . reduce ( Boolean :: logicalAnd )
119- . orElse ( true );
106+ return Objects . equals (
107+ stereotype . getCapability ( name ), capabilities . getCapability ( name ));
108+ } );
120109 }
121110
122111 private Boolean managedDownloadsEnabled (Capabilities stereotype , Capabilities capabilities ) {
@@ -140,39 +129,40 @@ private Boolean platformVersionMatch(Capabilities stereotype, Capabilities capab
140129 */
141130 return capabilities .getCapabilityNames ().stream ()
142131 .filter (name -> name .contains ("platformVersion" ))
143- .map (
132+ .allMatch (
144133 platformVersionCapName ->
145134 Objects .equals (
146135 stereotype .getCapability (platformVersionCapName ),
147- capabilities .getCapability (platformVersionCapName )))
148- .reduce (Boolean ::logicalAnd )
149- .orElse (true );
136+ capabilities .getCapability (platformVersionCapName )));
150137 }
151138
152139 private Boolean extensionCapabilitiesMatch (Capabilities stereotype , Capabilities capabilities ) {
153140 /*
154- We match extension capabilities when they are not prefixed with any of the
155- EXTENSION_CAPABILITIES_PREFIXES items. Also, we match them only when the capabilities
156- of the new session request contains that specific extension capability.
141+ We match extension capabilities in new session requests whose values are of type
142+ 'String', 'Number', or 'Boolean'. We ignore extension capability values of type
143+ 'Map' or 'List'. These are forwarded to the matched node for use in configuration,
144+ but are not considered for node matching.
157145 */
158146 return stereotype .getCapabilityNames ().stream ()
147+ // examine only extension capabilities
159148 .filter (name -> name .contains (":" ))
160- .filter (name -> capabilities .asMap ().containsKey (name ))
161- .filter (name -> EXTENSION_CAPABILITIES_PREFIXES .stream ().noneMatch (name ::contains ))
162- .map (
149+ // ignore capabilities not specified in the request
150+ .filter (name -> capabilities .getCapability (name ) != null )
151+ // ignore capabilities with Map values
152+ .filter (name -> !(capabilities .getCapability (name ) instanceof Map ))
153+ // ignore capabilities with List values
154+ .filter (name -> !(capabilities .getCapability (name ) instanceof List ))
155+ .allMatch (
163156 name -> {
164- if (capabilities .getCapability (name ) instanceof String ) {
165- return stereotype
166- .getCapability (name )
167- .toString ()
168- .equalsIgnoreCase (capabilities .getCapability (name ).toString ());
169- } else {
170- return capabilities .getCapability (name ) == null
171- || Objects .equals (
172- stereotype .getCapability (name ), capabilities .getCapability (name ));
157+ // evaluate capabilities with String values
158+ if (stereotype .getCapability (name ) instanceof String
159+ && capabilities .getCapability (name ) instanceof String ) {
160+ return ((String ) stereotype .getCapability (name ))
161+ .equalsIgnoreCase ((String ) capabilities .getCapability (name ));
173162 }
174- })
175- .reduce (Boolean ::logicalAnd )
176- .orElse (true );
163+ // evaluate capabilities with Number or Boolean values
164+ return Objects .equals (
165+ stereotype .getCapability (name ), capabilities .getCapability (name ));
166+ });
177167 }
178168}
0 commit comments