Skip to content

Commit ba71bfa

Browse files
diemolbarancev
authored andcommitted
Improving capability matcher to handle case when nodes or clients use non deprecated CapabilityType.PLATFORM_NAME
Signed-off-by: Alexei Barantsev <barancev@gmail.com>
1 parent bbac6df commit ba71bfa

File tree

2 files changed

+163
-11
lines changed

2 files changed

+163
-11
lines changed

java/server/src/org/openqa/grid/internal/utils/DefaultCapabilityMatcher.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Objects;
34+
import java.util.Optional;
3435
import java.util.function.BiFunction;
3536
import java.util.stream.Stream;
3637

@@ -54,12 +55,13 @@ private boolean anything(Object requested) {
5455
class PlatformValidator implements Validator {
5556
@Override
5657
public Boolean apply(Map<String, Object> providedCapabilities, Map<String, Object> requestedCapabilities) {
57-
Object requested = requestedCapabilities.get(CapabilityType.PLATFORM);
58+
Object requested = Optional.ofNullable(requestedCapabilities.get(CapabilityType.PLATFORM))
59+
.orElse(requestedCapabilities.get(CapabilityType.PLATFORM_NAME));
5860
if (anything(requested)) {
5961
return true;
6062
}
61-
Object provided = providedCapabilities.get(CapabilityType.PLATFORM);
62-
63+
Object provided = Optional.ofNullable(providedCapabilities.get(CapabilityType.PLATFORM))
64+
.orElse(providedCapabilities.get(CapabilityType.PLATFORM_NAME));
6365
Platform requestedPlatform = extractPlatform(requested);
6466
if (requestedPlatform != null) {
6567
Platform providedPlatform = extractPlatform(provided);

java/server/test/org/openqa/grid/internal/utils/DefaultCapabilityMatcherTest.java

Lines changed: 158 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.openqa.selenium.firefox.FirefoxOptions;
2929
import org.openqa.selenium.remote.BrowserType;
3030
import org.openqa.selenium.remote.CapabilityType;
31-
import org.openqa.selenium.safari.SafariDriver;
3231
import org.openqa.selenium.safari.SafariOptions;
3332

3433
import java.util.HashMap;
@@ -38,8 +37,9 @@ public class DefaultCapabilityMatcherTest {
3837

3938
private DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();
4039

40+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
4141
@Test
42-
public void smokeTest() {
42+
public void smokeTestWithDeprecatedPlatformCapability() {
4343
Map<String, Object> firefox = ImmutableMap.of(
4444
CapabilityType.BROWSER_NAME, "B",
4545
CapabilityType.PLATFORM, "XP");
@@ -73,7 +73,42 @@ public void smokeTest() {
7373
}
7474

7575
@Test
76-
public void genericPlatformMatchingTest() {
76+
public void smokeTest() {
77+
Map<String, Object> firefox = ImmutableMap.of(
78+
CapabilityType.BROWSER_NAME, "B",
79+
CapabilityType.PLATFORM_NAME, "XP");
80+
Map<String, Object> tl = new HashMap<String, Object>() {{
81+
put(CapabilityType.APPLICATION_NAME, "A");
82+
put(CapabilityType.VERSION, null);
83+
}};
84+
85+
Map<String, Object> firefox2 = ImmutableMap.of(
86+
CapabilityType.BROWSER_NAME, "B",
87+
CapabilityType.PLATFORM_NAME, "win7",
88+
CapabilityType.VERSION, "3.6");
89+
Map<String, Object> tl2 = ImmutableMap.of(
90+
CapabilityType.APPLICATION_NAME, "A",
91+
CapabilityType.VERSION, "8.5.100.7");
92+
93+
assertTrue(matcher.matches(tl, tl));
94+
assertFalse(matcher.matches(tl, tl2));
95+
assertTrue(matcher.matches(tl2, tl));
96+
assertTrue(matcher.matches(tl2, tl2));
97+
98+
assertTrue(matcher.matches(firefox, firefox));
99+
assertFalse(matcher.matches(firefox, firefox2));
100+
assertFalse(matcher.matches(firefox2, firefox));
101+
assertTrue(matcher.matches(firefox2, firefox2));
102+
103+
assertFalse(matcher.matches(tl, null));
104+
assertFalse(matcher.matches(null, null));
105+
assertFalse(matcher.matches(tl, firefox));
106+
assertFalse(matcher.matches(firefox, tl2));
107+
}
108+
109+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
110+
@Test
111+
public void genericPlatformMatchingTestWithDeprecatedPlatformCapability() {
77112
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM, Platform.WINDOWS);
78113

79114
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM, "WINDOWS"), requested));
@@ -85,7 +120,20 @@ public void genericPlatformMatchingTest() {
85120
}
86121

87122
@Test
88-
public void specificPlatformMatchingTest() {
123+
public void genericPlatformMatchingTest() {
124+
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);
125+
126+
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "WINDOWS"), requested));
127+
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "xp"), requested));
128+
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows VISTA"), requested));
129+
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows 7"), requested));
130+
131+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "linux"), requested));
132+
}
133+
134+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
135+
@Test
136+
public void specificPlatformMatchingTestWithDeprecatedPlatformCapability() {
89137
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM, Platform.XP);
90138

91139
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM, "xp"), requested));
@@ -98,7 +146,21 @@ public void specificPlatformMatchingTest() {
98146
}
99147

100148
@Test
101-
public void unknownPlatformMatchingTest() {
149+
public void specificPlatformMatchingTest() {
150+
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM_NAME, Platform.XP);
151+
152+
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "xp"), requested));
153+
154+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "WINDOWS"), requested));
155+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows VISTA"), requested));
156+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows 7"), requested));
157+
158+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "linux"), requested));
159+
}
160+
161+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
162+
@Test
163+
public void unknownPlatformMatchingTestWithDeprecatedPlatformCapability() {
102164
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM, "ms-dos");
103165

104166
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM, "ms-dos"), requested));
@@ -107,6 +169,16 @@ public void unknownPlatformMatchingTest() {
107169
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM, "PS/2"), requested));
108170
}
109171

172+
@Test
173+
public void unknownPlatformMatchingTest() {
174+
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM_NAME, "ms-dos");
175+
176+
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "ms-dos"), requested));
177+
178+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows"), requested));
179+
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "PS/2"), requested));
180+
}
181+
110182
@Test
111183
public void canAddAttributeMatcher() {
112184
matcher.addToConsider("my:capability");
@@ -115,8 +187,9 @@ public void canAddAttributeMatcher() {
115187
assertFalse(matcher.matches(ImmutableMap.of("my:capability", "milk"), requested));
116188
}
117189

190+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
118191
@Test
119-
public void nullEmptyValues() {
192+
public void nullEmptyValuesWithDeprecatedPlatformCapability() {
120193
Map<String, Object> requested = new HashMap<>();
121194
requested.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
122195
requested.put(CapabilityType.PLATFORM, null);
@@ -130,6 +203,21 @@ public void nullEmptyValues() {
130203
assertTrue(matcher.matches(node, requested));
131204
}
132205

206+
@Test
207+
public void nullEmptyValues() {
208+
Map<String, Object> requested = new HashMap<>();
209+
requested.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
210+
requested.put(CapabilityType.PLATFORM_NAME, null);
211+
requested.put(CapabilityType.VERSION, "");
212+
213+
Map<String, Object> node = new HashMap<>();
214+
node.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
215+
node.put(CapabilityType.PLATFORM_NAME, Platform.LINUX);
216+
node.put(CapabilityType.VERSION, "3.6");
217+
218+
assertTrue(matcher.matches(node, requested));
219+
}
220+
133221
@Test
134222
public void versionTests() {
135223
DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();
@@ -183,8 +271,9 @@ public void shouldMatchMarionetteFirefoxDriverOnly() {
183271
assertTrue(matcher.matches(mNode, requested));
184272
}
185273

274+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
186275
@Test
187-
public void shouldMatchSafariTechnologyPreviewOnly() {
276+
public void shouldMatchSafariTechnologyPreviewOnlyWithDeprecatedPlatformCapability() {
188277
Map<String, Object> requested = new SafariOptions().setUseTechnologyPreview(true).asMap();
189278

190279
Map<String, Object> tpNode = new HashMap<>();
@@ -201,7 +290,25 @@ public void shouldMatchSafariTechnologyPreviewOnly() {
201290
}
202291

203292
@Test
204-
public void shouldMatchRegularSafariOnly() {
293+
public void shouldMatchSafariTechnologyPreviewOnly() {
294+
Map<String, Object> requested = new SafariOptions().setUseTechnologyPreview(true).asMap();
295+
296+
Map<String, Object> tpNode = new HashMap<>();
297+
tpNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
298+
tpNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
299+
tpNode.put("technologyPreview", true);
300+
301+
Map<String, Object> regularNode = new HashMap<>();
302+
regularNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
303+
regularNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
304+
305+
assertTrue(matcher.matches(tpNode, requested));
306+
assertFalse(matcher.matches(regularNode, requested));
307+
}
308+
309+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
310+
@Test
311+
public void shouldMatchRegularSafariOnlyWithDeprecatedPlatformCapability() {
205312
Map<String, Object> requested = new SafariOptions().asMap();
206313

207314
Map<String, Object> tpNode = new HashMap<>();
@@ -217,4 +324,47 @@ public void shouldMatchRegularSafariOnly() {
217324
assertTrue(matcher.matches(regularNode, requested));
218325
}
219326

327+
@Test
328+
public void shouldMatchRegularSafariOnly() {
329+
Map<String, Object> requested = new SafariOptions().asMap();
330+
331+
Map<String, Object> tpNode = new HashMap<>();
332+
tpNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
333+
tpNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
334+
tpNode.put("technologyPreview", true);
335+
336+
Map<String, Object> regularNode = new HashMap<>();
337+
regularNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
338+
regularNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
339+
340+
assertFalse(matcher.matches(tpNode, requested));
341+
assertTrue(matcher.matches(regularNode, requested));
342+
}
343+
344+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
345+
@Test
346+
public void shouldMatchWhenRequestedHasDeprecatedPlatformCapability() {
347+
Map<String, Object> requested = new FirefoxOptions().asMap();
348+
requested.put(CapabilityType.PLATFORM, Platform.ANY);
349+
350+
Map<String, Object> node = new HashMap<>();
351+
node.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
352+
node.put(CapabilityType.PLATFORM_NAME, Platform.LINUX);
353+
354+
assertTrue(matcher.matches(node, requested));
355+
}
356+
357+
// TODO remove test when CapabilityType.PLATFORM is removed from code base
358+
@Test
359+
public void shouldMatchWhenNodeHasDeprecatedPlatformCapability() {
360+
Map<String, Object> requested = new FirefoxOptions().asMap();
361+
requested.put(CapabilityType.PLATFORM_NAME, Platform.ANY);
362+
363+
Map<String, Object> node = new HashMap<>();
364+
node.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
365+
node.put(CapabilityType.PLATFORM, Platform.LINUX);
366+
367+
assertTrue(matcher.matches(node, requested));
368+
}
369+
220370
}

0 commit comments

Comments
 (0)