diff --git a/src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java b/src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java index 5ebbdfbc3..a57170d6c 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java +++ b/src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java @@ -18,19 +18,16 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import io.appium.java_client.remote.options.BaseOptions; import org.openqa.selenium.Capabilities; import org.openqa.selenium.internal.Require; -import org.openqa.selenium.AcceptedW3CCapabilityKeys; import org.openqa.selenium.remote.CommandPayload; import java.util.Map; -import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX; import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION; public class AppiumNewSessionCommandPayload extends CommandPayload { - private static final AcceptedW3CCapabilityKeys ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys(); - /** * Appends "appium:" prefix to all non-prefixed non-standard capabilities. * @@ -40,10 +37,10 @@ public class AppiumNewSessionCommandPayload extends CommandPayload { private static Map makeW3CSafe(Capabilities possiblyInvalidCapabilities) { return Require.nonNull("Capabilities", possiblyInvalidCapabilities) .asMap().entrySet().stream() - .collect(ImmutableMap.toImmutableMap(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey()) - ? entry.getKey() - : APPIUM_PREFIX + entry.getKey(), - Map.Entry::getValue)); + .collect(ImmutableMap.toImmutableMap( + entry -> BaseOptions.toW3cName(entry.getKey()), + Map.Entry::getValue + )); } /** diff --git a/src/main/java/io/appium/java_client/remote/options/BaseOptions.java b/src/main/java/io/appium/java_client/remote/options/BaseOptions.java index c157fc684..7ef52b7e4 100644 --- a/src/main/java/io/appium/java_client/remote/options/BaseOptions.java +++ b/src/main/java/io/appium/java_client/remote/options/BaseOptions.java @@ -21,7 +21,6 @@ import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.internal.Require; -import org.openqa.selenium.AcceptedW3CCapabilityKeys; import org.openqa.selenium.remote.CapabilityType; import javax.annotation.Nullable; @@ -51,7 +50,6 @@ public class BaseOptions> extends MutableCapabilities i SupportsNewCommandTimeoutOption, SupportsBrowserNameOption, SupportsPlatformVersionOption { - private static final AcceptedW3CCapabilityKeys W3C_KEY_PATTERNS = new AcceptedW3CCapabilityKeys(); /** * Creates new instance with no preset capabilities. @@ -109,8 +107,7 @@ public Platform getPlatformName() { @Override public Map asMap() { return unmodifiableMap(super.asMap().entrySet().stream() - .collect(Collectors.toMap(entry -> W3C_KEY_PATTERNS.test(entry.getKey()) - ? entry.getKey() : APPIUM_PREFIX + entry.getKey(), Map.Entry::getValue) + .collect(Collectors.toMap(entry -> toW3cName(entry.getKey()), Map.Entry::getValue) )); } @@ -145,16 +142,25 @@ public T clone() { @Override public void setCapability(String key, @Nullable Object value) { Require.nonNull("Capability name", key); - super.setCapability(W3C_KEY_PATTERNS.test(key) ? key : APPIUM_PREFIX + key, value); + super.setCapability(toW3cName(key), value); } @Override @Nullable public Object getCapability(String capabilityName) { Object value = super.getCapability(capabilityName); - if (value == null) { - value = super.getCapability(APPIUM_PREFIX + capabilityName); - } - return value; + return value == null + ? super.getCapability(APPIUM_PREFIX + capabilityName) + : value; + } + + /** + * Adds the 'appium:' prefix to the given capability name if necessary. + * + * @param capName the original capability name. + * @return The preformatted W3C-compatible capability name. + */ + public static String toW3cName(String capName) { + return W3CCapabilityKeys.INSTANCE.test(capName) ? capName : APPIUM_PREFIX + capName; } } \ No newline at end of file diff --git a/src/main/java/io/appium/java_client/remote/options/W3CCapabilityKeys.java b/src/main/java/io/appium/java_client/remote/options/W3CCapabilityKeys.java new file mode 100644 index 000000000..b29150311 --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/W3CCapabilityKeys.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * 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 io.appium.java_client.remote.options; + +import java.util.function.Predicate; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +public class W3CCapabilityKeys implements Predicate { + public static final W3CCapabilityKeys INSTANCE = new W3CCapabilityKeys(); + private static final Predicate ACCEPTED_W3C_PATTERNS = Stream.of( + "^[\\w-]+:.*$", + "^acceptInsecureCerts$", + "^browserName$", + "^browserVersion$", + "^platformName$", + "^pageLoadStrategy$", + "^proxy$", + "^setWindowRect$", + "^strictFileInteractability$", + "^timeouts$", + "^unhandledPromptBehavior$", + "^webSocketUrl$") // from webdriver-bidi + .map(Pattern::compile) + .map(Pattern::asPredicate) + .reduce(identity -> false, Predicate::or); + + protected W3CCapabilityKeys() { + } + + @Override + public boolean test(String capabilityName) { + return ACCEPTED_W3C_PATTERNS.test(capabilityName); + } +}