Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add mobile: replacements to clipboard API wrappers #2188

Merged
merged 1 commit into from
Jun 28, 2024
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 @@ -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