Skip to content

Commit

Permalink
chore: Add mobile: replacements to clipboards API wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jun 27, 2024
1 parent c8e13e1 commit 57b2f22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static <T> T executeScript(ExecutesMethod executesMethod, String scriptNa
*/
@Nullable
public static <T> T executeScript(
ExecutesMethod executesMethod, String scriptName, @Nullable Map<String, Object> args
ExecutesMethod executesMethod, String scriptName, @Nullable Map<String, ?> args
) {
return execute(executesMethod, Map.entry(EXECUTE_SCRIPT, Map.of(
"script", scriptName,
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/io/appium/java_client/clipboard/HasClipboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package io.appium.java_client.clipboard;

import io.appium.java_client.CanRememberExtensionPresence;
import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecutesMethod;
import org.openqa.selenium.UnsupportedCommandException;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand All @@ -27,20 +29,25 @@
import static io.appium.java_client.MobileCommand.SET_CLIPBOARD;
import static java.util.Objects.requireNonNull;

public interface HasClipboard extends ExecutesMethod {
public interface HasClipboard extends ExecutesMethod, CanRememberExtensionPresence {
/**
* Set the content of device's clipboard.
*
* @param contentType one of supported content types.
* @param contentType one of supported content types.
* @param base64Content base64-encoded content to be set.
*/
default void setClipboard(ClipboardContentType contentType, byte[] base64Content) {
CommandExecutionHelper.execute(this, Map.entry(SET_CLIPBOARD,
Map.of(
"content", new String(requireNonNull(base64Content), StandardCharsets.UTF_8),
"contentType", contentType.name().toLowerCase()
)
));
final String extName = "mobile: setClipboard";
var args = Map.of(
"content", new String(requireNonNull(base64Content), StandardCharsets.UTF_8),
"contentType", contentType.name().toLowerCase()
);
try {
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
CommandExecutionHelper.execute(this, Map.entry(SET_CLIPBOARD, args));
}
}

/**
Expand All @@ -50,8 +57,14 @@ default void setClipboard(ClipboardContentType contentType, byte[] base64Content
* @return the actual content of the clipboard as base64-encoded string or an empty string if the clipboard is empty
*/
default String getClipboard(ClipboardContentType contentType) {
return CommandExecutionHelper.execute(this, Map.entry(GET_CLIPBOARD,
Map.of("contentType", contentType.name().toLowerCase())));
final String extName = "mobile: getClipboard";
var args = Map.of("contentType", contentType.name().toLowerCase());
try {
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return CommandExecutionHelper.execute(this, Map.entry(GET_CLIPBOARD, args));
}
}

/**
Expand Down

0 comments on commit 57b2f22

Please sign in to comment.