Skip to content

Commit

Permalink
[grid] Matching for prefixed platformVersion
Browse files Browse the repository at this point in the history
This will make the matching process faster
when a vendor-prefixed platformVersion is
present.

Also, adding a few more tests for it.
  • Loading branch information
diemol committed Sep 24, 2021
1 parent 2086f38 commit 279e41f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 17 deletions.
58 changes: 41 additions & 17 deletions java/src/org/openqa/selenium/grid/data/DefaultSlotMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,11 @@ public class DefaultSlotMatcher implements SlotMatcher, Serializable {
@Override
public boolean matches(Capabilities stereotype, Capabilities capabilities) {

Boolean initialMatch = stereotype.getCapabilityNames().stream()
// Matching of extension capabilities is implementation independent. Skip them
.filter(name -> !name.contains(":"))
// Platform matching is special, we do it below
.filter(name -> !"platform".equalsIgnoreCase(name) && !"platformName".equalsIgnoreCase(name))
.map(name -> {
if (capabilities.getCapability(name) instanceof String) {
return stereotype.getCapability(name).toString()
.equalsIgnoreCase(capabilities.getCapability(name).toString());
} else {
return capabilities.getCapability(name) == null ||
Objects.equals(stereotype.getCapability(name), capabilities.getCapability(name));
}
})
.reduce(Boolean::logicalAnd)
.orElse(false);
if (!initialMatch(stereotype, capabilities)) {
return false;
}

if (!initialMatch) {
if (!platformVersionMatch(stereotype, capabilities)) {
return false;
}

Expand All @@ -82,4 +69,41 @@ public boolean matches(Capabilities stereotype, Capabilities capabilities) {
stereotype.getPlatformName().is(capabilities.getPlatformName()));
return browserNameMatch && browserVersionMatch && platformNameMatch;
}

private Boolean initialMatch(Capabilities stereotype, Capabilities capabilities) {
return stereotype.getCapabilityNames().stream()
// Matching of extension capabilities is implementation independent. Skip them
.filter(name -> !name.contains(":"))
// Platform matching is special, we do later
.filter(name -> !"platform".equalsIgnoreCase(name) && !"platformName".equalsIgnoreCase(name))
.map(name -> {
if (capabilities.getCapability(name) instanceof String) {
return stereotype.getCapability(name).toString()
.equalsIgnoreCase(capabilities.getCapability(name).toString());
} else {
return capabilities.getCapability(name) == null ||
Objects.equals(stereotype.getCapability(name), capabilities.getCapability(name));
}
})
.reduce(Boolean::logicalAnd)
.orElse(false);
}

private Boolean platformVersionMatch(Capabilities stereotype, Capabilities capabilities) {
/*
This platform version match is not W3C compliant but users can add Appium servers as
Nodes, so we avoid delaying the match until the Slot, which makes the whole matching
process faster.
*/
return capabilities.getCapabilityNames()
.stream()
.filter(name -> name.contains("platformVersion"))
.map(
platformVersionCapName ->
Objects.equals(stereotype.getCapability(platformVersionCapName),
capabilities.getCapability(platformVersionCapName)))
.reduce(Boolean::logicalAnd)
.orElse(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,85 @@ public void matchesBrowser() {
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void matchesEmptyBrowser() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
);
Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void matchesPrefixedPlatformVersion() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:platformVersion", "10"
);
Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:platformVersion", "10"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void prefixedPlatformVersionDoesNotMatch() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:platformVersion", "10"
);
Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:platformVersion", "11"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
}

@Test
public void matchesWhenPrefixedPlatformVersionIsNotRequested() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:platformVersion", "10"
);
Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void prefixedPlatformVersionDoesNotMatchWhenNotPresentInStereotype() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
);
Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "80",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:platformVersion", "10"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
}

@Test
public void platformDoesNotMatch() {
Capabilities stereotype = new ImmutableCapabilities(
Expand Down

0 comments on commit 279e41f

Please sign in to comment.