Skip to content

Commit

Permalink
Improving capability matcher to handle case when nodes or clients use…
Browse files Browse the repository at this point in the history
… non deprecated CapabilityType.PLATFORM_NAME

Signed-off-by: Alexei Barantsev <barancev@gmail.com>
  • Loading branch information
diemol authored and barancev committed Dec 7, 2017
1 parent bbac6df commit ba71bfa
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Stream;

Expand All @@ -54,12 +55,13 @@ private boolean anything(Object requested) {
class PlatformValidator implements Validator {
@Override
public Boolean apply(Map<String, Object> providedCapabilities, Map<String, Object> requestedCapabilities) {
Object requested = requestedCapabilities.get(CapabilityType.PLATFORM);
Object requested = Optional.ofNullable(requestedCapabilities.get(CapabilityType.PLATFORM))
.orElse(requestedCapabilities.get(CapabilityType.PLATFORM_NAME));
if (anything(requested)) {
return true;
}
Object provided = providedCapabilities.get(CapabilityType.PLATFORM);

Object provided = Optional.ofNullable(providedCapabilities.get(CapabilityType.PLATFORM))
.orElse(providedCapabilities.get(CapabilityType.PLATFORM_NAME));
Platform requestedPlatform = extractPlatform(requested);
if (requestedPlatform != null) {
Platform providedPlatform = extractPlatform(provided);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;

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

private DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();

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

@Test
public void genericPlatformMatchingTest() {
public void smokeTest() {
Map<String, Object> firefox = ImmutableMap.of(
CapabilityType.BROWSER_NAME, "B",
CapabilityType.PLATFORM_NAME, "XP");
Map<String, Object> tl = new HashMap<String, Object>() {{
put(CapabilityType.APPLICATION_NAME, "A");
put(CapabilityType.VERSION, null);
}};

Map<String, Object> firefox2 = ImmutableMap.of(
CapabilityType.BROWSER_NAME, "B",
CapabilityType.PLATFORM_NAME, "win7",
CapabilityType.VERSION, "3.6");
Map<String, Object> tl2 = ImmutableMap.of(
CapabilityType.APPLICATION_NAME, "A",
CapabilityType.VERSION, "8.5.100.7");

assertTrue(matcher.matches(tl, tl));
assertFalse(matcher.matches(tl, tl2));
assertTrue(matcher.matches(tl2, tl));
assertTrue(matcher.matches(tl2, tl2));

assertTrue(matcher.matches(firefox, firefox));
assertFalse(matcher.matches(firefox, firefox2));
assertFalse(matcher.matches(firefox2, firefox));
assertTrue(matcher.matches(firefox2, firefox2));

assertFalse(matcher.matches(tl, null));
assertFalse(matcher.matches(null, null));
assertFalse(matcher.matches(tl, firefox));
assertFalse(matcher.matches(firefox, tl2));
}

// TODO remove test when CapabilityType.PLATFORM is removed from code base
@Test
public void genericPlatformMatchingTestWithDeprecatedPlatformCapability() {
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM, Platform.WINDOWS);

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

@Test
public void specificPlatformMatchingTest() {
public void genericPlatformMatchingTest() {
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);

assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "WINDOWS"), requested));
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "xp"), requested));
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows VISTA"), requested));
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows 7"), requested));

assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "linux"), requested));
}

// TODO remove test when CapabilityType.PLATFORM is removed from code base
@Test
public void specificPlatformMatchingTestWithDeprecatedPlatformCapability() {
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM, Platform.XP);

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

@Test
public void unknownPlatformMatchingTest() {
public void specificPlatformMatchingTest() {
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM_NAME, Platform.XP);

assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "xp"), requested));

assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "WINDOWS"), requested));
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows VISTA"), requested));
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows 7"), requested));

assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "linux"), requested));
}

// TODO remove test when CapabilityType.PLATFORM is removed from code base
@Test
public void unknownPlatformMatchingTestWithDeprecatedPlatformCapability() {
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM, "ms-dos");

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

@Test
public void unknownPlatformMatchingTest() {
Map<String, Object> requested = ImmutableMap.of(CapabilityType.PLATFORM_NAME, "ms-dos");

assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "ms-dos"), requested));

assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "windows"), requested));
assertFalse(matcher.matches(ImmutableMap.of(CapabilityType.PLATFORM_NAME, "PS/2"), requested));
}

@Test
public void canAddAttributeMatcher() {
matcher.addToConsider("my:capability");
Expand All @@ -115,8 +187,9 @@ public void canAddAttributeMatcher() {
assertFalse(matcher.matches(ImmutableMap.of("my:capability", "milk"), requested));
}

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

@Test
public void nullEmptyValues() {
Map<String, Object> requested = new HashMap<>();
requested.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
requested.put(CapabilityType.PLATFORM_NAME, null);
requested.put(CapabilityType.VERSION, "");

Map<String, Object> node = new HashMap<>();
node.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
node.put(CapabilityType.PLATFORM_NAME, Platform.LINUX);
node.put(CapabilityType.VERSION, "3.6");

assertTrue(matcher.matches(node, requested));
}

@Test
public void versionTests() {
DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();
Expand Down Expand Up @@ -183,8 +271,9 @@ public void shouldMatchMarionetteFirefoxDriverOnly() {
assertTrue(matcher.matches(mNode, requested));
}

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

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

@Test
public void shouldMatchRegularSafariOnly() {
public void shouldMatchSafariTechnologyPreviewOnly() {
Map<String, Object> requested = new SafariOptions().setUseTechnologyPreview(true).asMap();

Map<String, Object> tpNode = new HashMap<>();
tpNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
tpNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
tpNode.put("technologyPreview", true);

Map<String, Object> regularNode = new HashMap<>();
regularNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
regularNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);

assertTrue(matcher.matches(tpNode, requested));
assertFalse(matcher.matches(regularNode, requested));
}

// TODO remove test when CapabilityType.PLATFORM is removed from code base
@Test
public void shouldMatchRegularSafariOnlyWithDeprecatedPlatformCapability() {
Map<String, Object> requested = new SafariOptions().asMap();

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

@Test
public void shouldMatchRegularSafariOnly() {
Map<String, Object> requested = new SafariOptions().asMap();

Map<String, Object> tpNode = new HashMap<>();
tpNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
tpNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
tpNode.put("technologyPreview", true);

Map<String, Object> regularNode = new HashMap<>();
regularNode.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
regularNode.put(CapabilityType.PLATFORM_NAME, Platform.MAC);

assertFalse(matcher.matches(tpNode, requested));
assertTrue(matcher.matches(regularNode, requested));
}

// TODO remove test when CapabilityType.PLATFORM is removed from code base
@Test
public void shouldMatchWhenRequestedHasDeprecatedPlatformCapability() {
Map<String, Object> requested = new FirefoxOptions().asMap();
requested.put(CapabilityType.PLATFORM, Platform.ANY);

Map<String, Object> node = new HashMap<>();
node.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
node.put(CapabilityType.PLATFORM_NAME, Platform.LINUX);

assertTrue(matcher.matches(node, requested));
}

// TODO remove test when CapabilityType.PLATFORM is removed from code base
@Test
public void shouldMatchWhenNodeHasDeprecatedPlatformCapability() {
Map<String, Object> requested = new FirefoxOptions().asMap();
requested.put(CapabilityType.PLATFORM_NAME, Platform.ANY);

Map<String, Object> node = new HashMap<>();
node.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
node.put(CapabilityType.PLATFORM, Platform.LINUX);

assertTrue(matcher.matches(node, requested));
}

}

0 comments on commit ba71bfa

Please sign in to comment.