Skip to content

Commit b4aeb6e

Browse files
fix: Add prefix to platformVersion capability name (#1704)
1 parent c68d503 commit b4aeb6e

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

src/main/java/io/appium/java_client/remote/AppiumNewSessionCommandPayload.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.common.collect.ImmutableSet;
21+
import io.appium.java_client.remote.options.BaseOptions;
2122
import org.openqa.selenium.Capabilities;
2223
import org.openqa.selenium.internal.Require;
23-
import org.openqa.selenium.AcceptedW3CCapabilityKeys;
2424
import org.openqa.selenium.remote.CommandPayload;
2525

2626
import java.util.Map;
2727

28-
import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
2928
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;
3029

3130
public class AppiumNewSessionCommandPayload extends CommandPayload {
32-
private static final AcceptedW3CCapabilityKeys ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys();
33-
3431
/**
3532
* Appends "appium:" prefix to all non-prefixed non-standard capabilities.
3633
*
@@ -40,10 +37,10 @@ public class AppiumNewSessionCommandPayload extends CommandPayload {
4037
private static Map<String, Object> makeW3CSafe(Capabilities possiblyInvalidCapabilities) {
4138
return Require.nonNull("Capabilities", possiblyInvalidCapabilities)
4239
.asMap().entrySet().stream()
43-
.collect(ImmutableMap.toImmutableMap(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey())
44-
? entry.getKey()
45-
: APPIUM_PREFIX + entry.getKey(),
46-
Map.Entry::getValue));
40+
.collect(ImmutableMap.toImmutableMap(
41+
entry -> BaseOptions.toW3cName(entry.getKey()),
42+
Map.Entry::getValue
43+
));
4744
}
4845

4946
/**

src/main/java/io/appium/java_client/remote/options/BaseOptions.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.openqa.selenium.Platform;
2222
import org.openqa.selenium.WebDriverException;
2323
import org.openqa.selenium.internal.Require;
24-
import org.openqa.selenium.AcceptedW3CCapabilityKeys;
2524
import org.openqa.selenium.remote.CapabilityType;
2625

2726
import javax.annotation.Nullable;
@@ -51,7 +50,6 @@ public class BaseOptions<T extends BaseOptions<T>> extends MutableCapabilities i
5150
SupportsNewCommandTimeoutOption<T>,
5251
SupportsBrowserNameOption<T>,
5352
SupportsPlatformVersionOption<T> {
54-
private static final AcceptedW3CCapabilityKeys W3C_KEY_PATTERNS = new AcceptedW3CCapabilityKeys();
5553

5654
/**
5755
* Creates new instance with no preset capabilities.
@@ -109,8 +107,7 @@ public Platform getPlatformName() {
109107
@Override
110108
public Map<String, Object> asMap() {
111109
return unmodifiableMap(super.asMap().entrySet().stream()
112-
.collect(Collectors.toMap(entry -> W3C_KEY_PATTERNS.test(entry.getKey())
113-
? entry.getKey() : APPIUM_PREFIX + entry.getKey(), Map.Entry::getValue)
110+
.collect(Collectors.toMap(entry -> toW3cName(entry.getKey()), Map.Entry::getValue)
114111
));
115112
}
116113

@@ -145,16 +142,25 @@ public T clone() {
145142
@Override
146143
public void setCapability(String key, @Nullable Object value) {
147144
Require.nonNull("Capability name", key);
148-
super.setCapability(W3C_KEY_PATTERNS.test(key) ? key : APPIUM_PREFIX + key, value);
145+
super.setCapability(toW3cName(key), value);
149146
}
150147

151148
@Override
152149
@Nullable
153150
public Object getCapability(String capabilityName) {
154151
Object value = super.getCapability(capabilityName);
155-
if (value == null) {
156-
value = super.getCapability(APPIUM_PREFIX + capabilityName);
157-
}
158-
return value;
152+
return value == null
153+
? super.getCapability(APPIUM_PREFIX + capabilityName)
154+
: value;
155+
}
156+
157+
/**
158+
* Adds the 'appium:' prefix to the given capability name if necessary.
159+
*
160+
* @param capName the original capability name.
161+
* @return The preformatted W3C-compatible capability name.
162+
*/
163+
public static String toW3cName(String capName) {
164+
return W3CCapabilityKeys.INSTANCE.test(capName) ? capName : APPIUM_PREFIX + capName;
159165
}
160166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.remote.options;
18+
19+
import java.util.function.Predicate;
20+
import java.util.regex.Pattern;
21+
import java.util.stream.Stream;
22+
23+
public class W3CCapabilityKeys implements Predicate<String> {
24+
public static final W3CCapabilityKeys INSTANCE = new W3CCapabilityKeys();
25+
private static final Predicate<String> ACCEPTED_W3C_PATTERNS = Stream.of(
26+
"^[\\w-]+:.*$",
27+
"^acceptInsecureCerts$",
28+
"^browserName$",
29+
"^browserVersion$",
30+
"^platformName$",
31+
"^pageLoadStrategy$",
32+
"^proxy$",
33+
"^setWindowRect$",
34+
"^strictFileInteractability$",
35+
"^timeouts$",
36+
"^unhandledPromptBehavior$",
37+
"^webSocketUrl$") // from webdriver-bidi
38+
.map(Pattern::compile)
39+
.map(Pattern::asPredicate)
40+
.reduce(identity -> false, Predicate::or);
41+
42+
protected W3CCapabilityKeys() {
43+
}
44+
45+
@Override
46+
public boolean test(String capabilityName) {
47+
return ACCEPTED_W3C_PATTERNS.test(capabilityName);
48+
}
49+
}

0 commit comments

Comments
 (0)