Skip to content

Commit 409d167

Browse files
chore: Add mobile: replacements to clipboards API wrappers (#2188)
1 parent c8e13e1 commit 409d167

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/main/java/io/appium/java_client/CommandExecutionHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static <T> T executeScript(ExecutesMethod executesMethod, String scriptNa
6262
*/
6363
@Nullable
6464
public static <T> T executeScript(
65-
ExecutesMethod executesMethod, String scriptName, @Nullable Map<String, Object> args
65+
ExecutesMethod executesMethod, String scriptName, @Nullable Map<String, ?> args
6666
) {
6767
return execute(executesMethod, Map.entry(EXECUTE_SCRIPT, Map.of(
6868
"script", scriptName,

src/main/java/io/appium/java_client/clipboard/HasClipboard.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package io.appium.java_client.clipboard;
1818

19+
import io.appium.java_client.CanRememberExtensionPresence;
1920
import io.appium.java_client.CommandExecutionHelper;
2021
import io.appium.java_client.ExecutesMethod;
22+
import org.openqa.selenium.UnsupportedCommandException;
2123

2224
import java.nio.charset.StandardCharsets;
2325
import java.util.Base64;
@@ -27,20 +29,25 @@
2729
import static io.appium.java_client.MobileCommand.SET_CLIPBOARD;
2830
import static java.util.Objects.requireNonNull;
2931

30-
public interface HasClipboard extends ExecutesMethod {
32+
public interface HasClipboard extends ExecutesMethod, CanRememberExtensionPresence {
3133
/**
3234
* Set the content of device's clipboard.
3335
*
34-
* @param contentType one of supported content types.
36+
* @param contentType one of supported content types.
3537
* @param base64Content base64-encoded content to be set.
3638
*/
3739
default void setClipboard(ClipboardContentType contentType, byte[] base64Content) {
38-
CommandExecutionHelper.execute(this, Map.entry(SET_CLIPBOARD,
39-
Map.of(
40-
"content", new String(requireNonNull(base64Content), StandardCharsets.UTF_8),
41-
"contentType", contentType.name().toLowerCase()
42-
)
43-
));
40+
final String extName = "mobile: setClipboard";
41+
var args = Map.of(
42+
"content", new String(requireNonNull(base64Content), StandardCharsets.UTF_8),
43+
"contentType", contentType.name().toLowerCase()
44+
);
45+
try {
46+
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
47+
} catch (UnsupportedCommandException e) {
48+
// TODO: Remove the fallback
49+
CommandExecutionHelper.execute(this, Map.entry(SET_CLIPBOARD, args));
50+
}
4451
}
4552

4653
/**
@@ -50,8 +57,14 @@ default void setClipboard(ClipboardContentType contentType, byte[] base64Content
5057
* @return the actual content of the clipboard as base64-encoded string or an empty string if the clipboard is empty
5158
*/
5259
default String getClipboard(ClipboardContentType contentType) {
53-
return CommandExecutionHelper.execute(this, Map.entry(GET_CLIPBOARD,
54-
Map.of("contentType", contentType.name().toLowerCase())));
60+
final String extName = "mobile: getClipboard";
61+
var args = Map.of("contentType", contentType.name().toLowerCase());
62+
try {
63+
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
64+
} catch (UnsupportedCommandException e) {
65+
// TODO: Remove the fallback
66+
return CommandExecutionHelper.execute(this, Map.entry(GET_CLIPBOARD, args));
67+
}
5568
}
5669

5770
/**

0 commit comments

Comments
 (0)