
org.openqa.selenium.remote.server.CapabilitiesComparator Maven / Gradle / Ivy
Go to download
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
/*
Copyright 2011 WebDriver committers
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.openqa.selenium.remote.server;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import java.util.Collection;
import java.util.Comparator;
/**
* Compares two sets of {@link Capabilities} against a desired standard. Capabilities are compared
* by...
*
* - {@link Capabilities#getBrowserName() browser name},
*
- {@link Capabilities#getVersion() browser version},
*
- {@link Capabilities#isJavascriptEnabled() whether JavaScript is enabled},
*
- and {@link Capabilities#getPlatform() platform}
*
* For all comparisons, if the capability is missing, that particular criteria shall not factor
* into the comparison. When comparing platforms, preference will be given to an exact platform
* match over a fuzzy match (e.g. Platform.WINDOWS will match Platform.WINDOWS before it matches
* Platform.XP).
*/
class CapabilitiesComparator implements Comparator {
private final Comparator compareWith;
public CapabilitiesComparator(final Capabilities desiredCapabilities) {
final CapabilityScorer browserNameScorer = CapabilityScorer.scoreAgainst(
desiredCapabilities.getBrowserName());
Comparator byBrowserName = new Comparator() {
public int compare(Capabilities c1, Capabilities c2) {
return browserNameScorer.score(c1.getBrowserName())
- browserNameScorer.score(c2.getBrowserName());
}
};
final CapabilityScorer versionScorer = CapabilityScorer.scoreAgainst(
desiredCapabilities.getVersion());
Comparator byVersion = new Comparator() {
public int compare(Capabilities c1, Capabilities c2) {
return versionScorer.score(c1.getVersion())
- versionScorer.score(c2.getVersion());
}
};
final CapabilityScorer jsScorer = CapabilityScorer.scoreAgainst(
desiredCapabilities.isJavascriptEnabled());
Comparator byJavaScript = new Comparator() {
public int compare(Capabilities c1, Capabilities c2) {
return jsScorer.score(c1.isJavascriptEnabled())
- jsScorer.score(c2.isJavascriptEnabled());
}
};
Platform desiredPlatform = desiredCapabilities.getPlatform();
if (desiredPlatform == null) {
desiredPlatform = Platform.ANY;
}
final CapabilityScorer strictPlatformScorer = CapabilityScorer.scoreAgainst(
desiredPlatform);
Comparator byStrictPlatform = new Comparator() {
public int compare(Capabilities c1, Capabilities c2) {
return strictPlatformScorer.score(c1.getPlatform())
- strictPlatformScorer.score(c2.getPlatform());
}
};
final CapabilityScorer fuzzyPlatformScorer = new FuzzyPlatformScorer(
desiredPlatform);
Comparator byFuzzyPlatform = new Comparator() {
public int compare(Capabilities c1, Capabilities c2) {
return fuzzyPlatformScorer.score(c1.getPlatform())
- fuzzyPlatformScorer.score(c2.getPlatform());
}
};
compareWith = Ordering.compound(Lists.newArrayList(
byBrowserName,
byVersion,
byJavaScript,
byStrictPlatform,
byFuzzyPlatform));
}
public static T getBestMatch(
Capabilities against, Collection toCompare) {
return Ordering.from(new CapabilitiesComparator(against)).max(toCompare);
}
public int compare(final Capabilities a, final Capabilities b) {
return compareWith.compare(a, b);
}
private static class CapabilityScorer {
final T scoreAgainst;
public CapabilityScorer(T scoreAgainst) {
this.scoreAgainst = scoreAgainst;
}
public int score(T value) {
if (value == null) {
return 0;
} else if (value.equals(scoreAgainst)) {
return 1;
}
return -1;
}
static CapabilityScorer scoreAgainst(T value) {
return new CapabilityScorer(value);
}
}
private static class FuzzyPlatformScorer extends CapabilityScorer {
public FuzzyPlatformScorer(Platform scoreAgainst) {
super(scoreAgainst);
}
@Override
public int score(Platform value) {
if (value == null) {
value = Platform.ANY;
}
if (value.equals(Platform.ANY)) {
return 0;
}
return value.is(scoreAgainst) || scoreAgainst.is(value) ? 1 : -1;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy