Skip to content

Commit

Permalink
[grid] Adding match logic for extension caps
Browse files Browse the repository at this point in the history
This allows users who add specific, prefixed
capabilities to their Nodes configuration to
be matched to those capabilities sent by the
new session request.

Fixes #9839
  • Loading branch information
diemol committed Sep 24, 2021
1 parent 279e41f commit f8dad59
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
40 changes: 39 additions & 1 deletion java/src/org/openqa/selenium/grid/data/DefaultSlotMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.openqa.selenium.Capabilities;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -44,6 +46,13 @@
*/
public class DefaultSlotMatcher implements SlotMatcher, Serializable {

/*
List of prefixed extension capabilities we never should try to match, they should be
matched in the Node or in the browser driver.
*/
private static final List<String> EXTENSION_CAPABILITIES_PREFIXES = Arrays.asList(
"goog:", "moz:", "ms:", "se:");

@Override
public boolean matches(Capabilities stereotype, Capabilities capabilities) {

Expand All @@ -55,7 +64,11 @@ public boolean matches(Capabilities stereotype, Capabilities capabilities) {
return false;
}

// Simple browser, browserVersion and platformName match
if (!extensionCapabilitiesMatch(stereotype, capabilities)) {
return false;
}

// At the end, a simple browser, browserVersion and platformName match
boolean browserNameMatch =
(capabilities.getBrowserName() == null || capabilities.getBrowserName().isEmpty()) ||
Objects.equals(stereotype.getBrowserName(), capabilities.getBrowserName());
Expand Down Expand Up @@ -106,4 +119,29 @@ private Boolean platformVersionMatch(Capabilities stereotype, Capabilities capab
.orElse(true);
}

private Boolean extensionCapabilitiesMatch(Capabilities stereotype, Capabilities capabilities) {
/*
We match extension capabilities when they are not prefixed with any of the
EXTENSION_CAPABILITIES_PREFIXES items. Also, we match them only when the capabilities
of the new session request contains that specific extension capability.
*/
return stereotype.getCapabilityNames().stream()
.filter(name -> name.contains(":"))
.filter(name -> capabilities.asMap().containsKey(name))
.filter(name -> EXTENSION_CAPABILITIES_PREFIXES.stream().noneMatch(name::contains))
.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(true);
}

}
113 changes: 113 additions & 0 deletions java/test/org/openqa/selenium/grid/data/DefaultSlotMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,117 @@ public void shouldMatchCapabilitiesThatAreTheSameButDoNotContainCommonCapability

assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void extensionPrefixedCapabilitiesMatches() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam"
);

Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void extensionPrefixedCapabilitiesMatchesWhenNotPresentInStereotype() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
);

Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void extensionPrefixedCapabilitiesDoNotMatch() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam"
);

Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "gouda"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
}

@Test
public void multipleExtensionPrefixedCapabilitiesMatch() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam",
"prefixed:fruit", "mango"
);

Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam",
"prefixed:fruit", "mango"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}

@Test
public void multipleExtensionPrefixedCapabilitiesDoNotMatchWhenOneIsDifferent() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam",
"prefixed:fruit", "mango"
);

Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"prefixed:cheese", "amsterdam",
"prefixed:fruit", "orange"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isFalse();
}

@Test
public void vendorExtensionPrefixedCapabilitiesAreIgnoredForMatching() {
Capabilities stereotype = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"goog:cheese", "amsterdam",
"ms:fruit", "mango"
);

Capabilities capabilities = new ImmutableCapabilities(
CapabilityType.BROWSER_NAME, "chrome",
CapabilityType.BROWSER_VERSION, "84",
CapabilityType.PLATFORM_NAME, Platform.WINDOWS,
"goog:cheese", "gouda",
"ms:fruit", "orange"
);
assertThat(slotMatcher.matches(stereotype, capabilities)).isTrue();
}
}

0 comments on commit f8dad59

Please sign in to comment.