Skip to content

Commit 9832d17

Browse files
committed
[grid] Flesh out the default slot matcher to consider other capabilities too
1 parent 4af354b commit 9832d17

File tree

4 files changed

+90
-34
lines changed

4 files changed

+90
-34
lines changed

java/server/src/org/openqa/selenium/grid/data/DefaultSlotMatcher.java

+41-2
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,56 @@
1919

2020
import org.openqa.selenium.Capabilities;
2121

22+
import java.util.AbstractMap;
23+
import java.util.HashSet;
2224
import java.util.Objects;
25+
import java.util.Set;
2326

27+
/**
28+
* Default matching implementation for slots, loosely based on the
29+
* requirements for capability matching from the WebDriver spec. A match
30+
* is made if the following are all true:
31+
* <ul>
32+
* <li>All non-extension capabilities from the {@code stereotype} match
33+
* those in the {@link Capabilities} being considered.
34+
* <li>If the {@link Capabilities} being considered contain any of:
35+
* <ul>
36+
* <li>browserName
37+
* <li>browserVersion
38+
* <li>platform
39+
* </ul>
40+
* Then the {@code stereotype} must contain the same values.
41+
* </ul>
42+
* <p>
43+
* One thing to note is that extension capabilities are not considered when
44+
* matching slots, since the matching of these is implementation-specific
45+
* to each driver.
46+
*/
2447
public class DefaultSlotMatcher implements SlotMatcher {
2548

2649
@Override
27-
public boolean matches(Capabilities capabilities, Capabilities stereotype) {
50+
public boolean matches(Capabilities stereotype, Capabilities capabilities) {
51+
52+
Boolean initialMatch = stereotype.getCapabilityNames().stream()
53+
// Matching of extension capabilities is implementation independent. Skip them
54+
.filter(name -> !name.contains(":"))
55+
.map(name -> {
56+
Object value = capabilities.getCapability(name);
57+
return value == null || Objects.equals(stereotype.getCapability(name), value);
58+
})
59+
.reduce(Boolean::logicalAnd)
60+
.orElse(false);
61+
62+
if (!initialMatch) {
63+
return false;
64+
}
65+
2866
// Simple browser, browserVersion and platformName match
2967
boolean browserNameMatch =
68+
capabilities.getBrowserName() == null ||
3069
Objects.equals(stereotype.getBrowserName(), capabilities.getBrowserName());
3170
boolean browserVersionMatch =
32-
capabilities.getBrowserVersion().isEmpty() ||
71+
(capabilities.getBrowserVersion() == null || capabilities.getBrowserVersion().isEmpty()) ||
3372
Objects.equals(stereotype.getBrowserVersion(), capabilities.getBrowserVersion());
3473
boolean platformNameMatch =
3574
capabilities.getPlatformName() == null ||

java/server/src/org/openqa/selenium/grid/data/SlotMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
*/
2626
@FunctionalInterface
2727
public interface SlotMatcher {
28-
boolean matches(Capabilities capabilities, Capabilities stereotype);
28+
boolean matches(Capabilities stereotype, Capabilities capabilities);
2929
}

java/server/src/org/openqa/selenium/grid/node/local/LocalNodeFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static Collection<SessionFactory> createSessionFactory(
9292
tracer,
9393
clientFactory,
9494
stereotype,
95-
capabilities -> slotMatcher.matches(capabilities, stereotype),
95+
capabilities -> slotMatcher.matches(stereotype, capabilities),
9696
driverServiceBuilder));
9797
});
9898

java/server/test/org/openqa/selenium/grid/data/DefaultSlotMatcherTest.java

+47-30
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.assertj.core.api.Assertions.assertThat;
2121

2222
import org.junit.Test;
23+
import org.openqa.selenium.Capabilities;
2324
import org.openqa.selenium.ImmutableCapabilities;
2425
import org.openqa.selenium.Platform;
2526
import org.openqa.selenium.remote.CapabilityType;
@@ -30,133 +31,149 @@ public class DefaultSlotMatcherTest {
3031

3132
@Test
3233
public void fullMatch() {
33-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
34+
Capabilities stereotype = new ImmutableCapabilities(
3435
CapabilityType.BROWSER_NAME, "chrome",
3536
CapabilityType.BROWSER_VERSION, "80",
3637
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
3738
);
38-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
39+
Capabilities capabilities = new ImmutableCapabilities(
3940
CapabilityType.BROWSER_NAME, "chrome",
4041
CapabilityType.BROWSER_VERSION, "80",
4142
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
4243
);
43-
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
44+
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
4445
}
4546

4647
@Test
4748
public void matchesBrowserAndVersion() {
48-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
49+
Capabilities stereotype = new ImmutableCapabilities(
4950
CapabilityType.BROWSER_NAME, "chrome",
5051
CapabilityType.BROWSER_VERSION, "80",
5152
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
5253
);
53-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
54+
Capabilities capabilities = new ImmutableCapabilities(
5455
CapabilityType.BROWSER_NAME, "chrome",
5556
CapabilityType.BROWSER_VERSION, "80"
5657
);
57-
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
58+
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
5859
}
5960

6061
@Test
6162
public void matchesBrowser() {
62-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
63+
Capabilities stereotype = new ImmutableCapabilities(
6364
CapabilityType.BROWSER_NAME, "chrome",
6465
CapabilityType.BROWSER_VERSION, "80",
6566
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
6667
);
67-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
68+
Capabilities capabilities = new ImmutableCapabilities(
6869
CapabilityType.BROWSER_NAME, "chrome"
6970
);
70-
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
71+
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
7172
}
7273

7374
@Test
7475
public void platformDoesNotMatch() {
75-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
76+
Capabilities stereotype = new ImmutableCapabilities(
7677
CapabilityType.BROWSER_NAME, "chrome",
7778
CapabilityType.BROWSER_VERSION, "80",
7879
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
7980
);
80-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
81+
Capabilities capabilities = new ImmutableCapabilities(
8182
CapabilityType.BROWSER_NAME, "chrome",
8283
CapabilityType.BROWSER_VERSION, "80",
8384
CapabilityType.PLATFORM_NAME, Platform.MAC
8485
);
85-
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
86+
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
8687
}
8788

8889
@Test
8990
public void browserDoesNotMatch() {
90-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
91+
Capabilities stereotype = new ImmutableCapabilities(
9192
CapabilityType.BROWSER_NAME, "chrome",
9293
CapabilityType.BROWSER_VERSION, "80",
9394
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
9495
);
95-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
96+
Capabilities capabilities = new ImmutableCapabilities(
9697
CapabilityType.BROWSER_NAME, "firefox",
9798
CapabilityType.BROWSER_VERSION, "80",
9899
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
99100
);
100-
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
101+
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
101102
}
102103

103104
@Test
104105
public void browserVersionDoesNotMatch() {
105-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
106+
Capabilities stereotype = new ImmutableCapabilities(
106107
CapabilityType.BROWSER_NAME, "chrome",
107108
CapabilityType.BROWSER_VERSION, "80",
108109
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
109110
);
110-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
111+
Capabilities capabilities = new ImmutableCapabilities(
111112
CapabilityType.BROWSER_NAME, "chrome",
112113
CapabilityType.BROWSER_VERSION, "84",
113114
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
114115
);
115-
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
116+
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
116117
}
117118

118119
@Test
119120
public void requestedPlatformDoesNotMatch() {
120-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
121+
Capabilities stereotype = new ImmutableCapabilities(
121122
CapabilityType.BROWSER_NAME, "chrome",
122123
CapabilityType.BROWSER_VERSION, "80"
123124
);
124125

125-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
126+
Capabilities capabilities = new ImmutableCapabilities(
126127
CapabilityType.BROWSER_NAME, "chrome",
127-
CapabilityType.BROWSER_VERSION, "84",
128+
CapabilityType.BROWSER_VERSION, "80",
128129
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
129130
);
130-
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
131+
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
131132
}
132133

133134
@Test
134-
public void requestedBrowserVersionDoesNotMatch() {
135-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
135+
public void shouldNotMatchIfRequestedBrowserVersionIsMissingFromStereotype() {
136+
Capabilities stereotype = new ImmutableCapabilities(
136137
CapabilityType.BROWSER_NAME, "chrome",
137138
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
138139
);
139140

140-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
141+
Capabilities capabilities = new ImmutableCapabilities(
141142
CapabilityType.BROWSER_NAME, "chrome",
142143
CapabilityType.BROWSER_VERSION, "84",
143144
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
144145
);
145-
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
146+
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
146147
}
147148

148149
@Test
149-
public void matchesWithJWPCaps() {
150-
ImmutableCapabilities stereotype = new ImmutableCapabilities(
150+
public void matchesWithJsonWireProtocolCaps() {
151+
Capabilities stereotype = new ImmutableCapabilities(
151152
CapabilityType.BROWSER_NAME, "chrome",
152153
CapabilityType.BROWSER_VERSION, "80",
153154
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
154155
);
155-
ImmutableCapabilities capabilities = new ImmutableCapabilities(
156+
Capabilities capabilities = new ImmutableCapabilities(
156157
CapabilityType.BROWSER_NAME, "chrome",
157158
CapabilityType.VERSION, "80",
158159
CapabilityType.PLATFORM, Platform.WINDOWS
159160
);
160-
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
161+
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
162+
}
163+
164+
@Test
165+
public void shouldNotMatchCapabilitiesThatAreDifferentButDoNotContainCommonCapabilityNames() {
166+
Capabilities stereotype = new ImmutableCapabilities("acceptInsecureCerts", "true");
167+
Capabilities capabilities = new ImmutableCapabilities("acceptInsecureCerts", "false");
168+
169+
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
170+
}
171+
172+
@Test
173+
public void shouldMatchCapabilitiesThatAreTheSameButDoNotContainCommonCapabilityNames() {
174+
Capabilities stereotype = new ImmutableCapabilities("strictFileInteractability", "true");
175+
Capabilities capabilities = new ImmutableCapabilities("strictFileInteractability", "true");
176+
177+
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
161178
}
162179
}

0 commit comments

Comments
 (0)