1616
1717package io .appium .java_client .clipboard ;
1818
19+ import io .appium .java_client .CanRememberExtensionPresence ;
1920import io .appium .java_client .CommandExecutionHelper ;
2021import io .appium .java_client .ExecutesMethod ;
22+ import org .openqa .selenium .UnsupportedCommandException ;
2123
2224import java .nio .charset .StandardCharsets ;
2325import java .util .Base64 ;
2729import static io .appium .java_client .MobileCommand .SET_CLIPBOARD ;
2830import 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