Skip to content

Commit

Permalink
DefaultCapabilityMatcher in grid should consider browserVersion (w3c)…
Browse files Browse the repository at this point in the history
… and version (jsonwp) capability keys
  • Loading branch information
lukeis committed Oct 25, 2016
1 parent 5e86496 commit 3a616b0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public interface CapabilityType {
String SUPPORTS_JAVASCRIPT = "javascriptEnabled";
String TAKES_SCREENSHOT = "takesScreenshot";
String VERSION = "version";
String BROWSER_VERSION = "browserVersion";
String SUPPORTS_ALERTS = "handlesAlerts";
String SUPPORTS_SQL_DATABASE = "databaseEnabled";
String SUPPORTS_LOCATION_CONTEXT = "locationContextEnabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public DefaultCapabilityMatcher() {
toConsider.add(CapabilityType.PLATFORM);
toConsider.add(CapabilityType.BROWSER_NAME);
toConsider.add(CapabilityType.VERSION);
toConsider.add(CapabilityType.BROWSER_VERSION);
toConsider.add(CapabilityType.APPLICATION_NAME);

}
Expand All @@ -63,24 +64,37 @@ public boolean matches(Map<String, Object> nodeCapability, Map<String, Object> r
if (!key.startsWith(GRID_TOKEN) && toConsider.contains(key)) {
if (requestedCapability.get(key) != null) {
String value = requestedCapability.get(key).toString();
// ignore matching 'ANY' or '*" or empty string cases
if (!("ANY".equalsIgnoreCase(value) || "".equals(value) || "*".equals(value))) {
Platform requested = extractPlatform(requestedCapability.get(key));
// special case for platform
if (requested != null) {
Platform node = extractPlatform(nodeCapability.get(key));
if (node == null) {
return false;
}
if (!node.is(requested)) {
return false;
}
} else {
if (!requestedCapability.get(key).equals(nodeCapability.get(key))) {
return false;
}
switch (key) {
case CapabilityType.PLATFORM:
Platform requested = extractPlatform(requestedCapability.get(key));
if (requested != null) {
Platform node = extractPlatform(nodeCapability.get(key));
if (node == null) {
return false;
}
if (!node.is(requested)) {
return false;
}
}
break;

case CapabilityType.BROWSER_VERSION:
case CapabilityType.VERSION:
// w3c uses 'browserVersion' but 2.X / 3.X use 'version'
// w3c name takes precedence
Object nodeVersion = nodeCapability.getOrDefault(CapabilityType.BROWSER_VERSION, nodeCapability.get(CapabilityType.VERSION));
if (!value.equals(nodeVersion)) {
return false;
}
break;

default:
if (!requestedCapability.get(key).equals(nodeCapability.get(key))) {
return false;
}
}
} else {
// null value matches anything.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Platform;
Expand All @@ -32,56 +35,38 @@

public class DefaultCapabilityMatcherTest {

private DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();

private Map<String, Object> firefox = new HashMap<>();
private Map<String, Object> tl = new HashMap<>();

private Map<String, Object> firefox2 = new HashMap<>();
private Map<String, Object> tl2 = new HashMap<>();

private Map<String, Object> exotic = new HashMap<>();


private CapabilityMatcher helper = new DefaultCapabilityMatcher();

@Before
public void build() {
tl.put(CapabilityType.APPLICATION_NAME, "A");
tl.put(CapabilityType.VERSION, null);
firefox.put(CapabilityType.BROWSER_NAME, "B");
firefox.put(CapabilityType.PLATFORM, "XP");

tl2.put(CapabilityType.APPLICATION_NAME, "A");
tl2.put(CapabilityType.VERSION, "8.5.100.7");

firefox2.put(CapabilityType.BROWSER_NAME, "B");
firefox2.put(CapabilityType.PLATFORM, "Vista");
firefox2.put(CapabilityType.VERSION, "3.6");

exotic.put("numberOfHead", 2);
}

@Test
public void smokeTest() {
assertTrue(helper.matches(tl, tl));
assertFalse(helper.matches(tl, tl2));
assertTrue(helper.matches(tl2, tl));
assertTrue(helper.matches(tl2, tl2));

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

assertFalse(helper.matches(tl, null));
assertFalse(helper.matches(null, null));
assertFalse(helper.matches(tl, firefox));
assertFalse(helper.matches(firefox, tl2));
Map<String, Object> firefox = ImmutableMap.of(CapabilityType.BROWSER_NAME, "B", CapabilityType.PLATFORM, "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, "Vista", 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));
assertFalse(matcher.matches(firefox, firefox2));

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

@Test
public void platformMatchingTest() {
DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();
Platform p = Platform.WINDOWS;

assertTrue(matcher.extractPlatform("WINDOWS") == p);
Expand All @@ -93,7 +78,6 @@ public void platformMatchingTest() {

@Test
public void nullEmptyValues() {
DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();

Map<String, Object> requested = new HashMap<>();
requested.put(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
Expand All @@ -109,4 +93,14 @@ public void nullEmptyValues() {


}

@Test
public void versionTests() {
DefaultCapabilityMatcher matcher = new DefaultCapabilityMatcher();

assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.VERSION, "50"), ImmutableMap.of(CapabilityType.VERSION, "50")));
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.VERSION, "50"), ImmutableMap.of(CapabilityType.BROWSER_VERSION, "50")));
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.BROWSER_VERSION, "50"), ImmutableMap.of(CapabilityType.VERSION, "50")));
assertTrue(matcher.matches(ImmutableMap.of(CapabilityType.BROWSER_VERSION, "50"), ImmutableMap.of(CapabilityType.BROWSER_VERSION, "50")));
}
}

0 comments on commit 3a616b0

Please sign in to comment.