Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -40,10 +37,10 @@ public class AppiumNewSessionCommandPayload extends CommandPayload {
private static Map<String, Object> 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
));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +50,6 @@ public class BaseOptions<T extends BaseOptions<T>> extends MutableCapabilities i
SupportsNewCommandTimeoutOption<T>,
SupportsBrowserNameOption<T>,
SupportsPlatformVersionOption<T> {
private static final AcceptedW3CCapabilityKeys W3C_KEY_PATTERNS = new AcceptedW3CCapabilityKeys();

/**
* Creates new instance with no preset capabilities.
Expand Down Expand Up @@ -109,8 +107,7 @@ public Platform getPlatformName() {
@Override
public Map<String, Object> 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)
));
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> {
public static final W3CCapabilityKeys INSTANCE = new W3CCapabilityKeys();
private static final Predicate<String> 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);
}
}