Skip to content

Commit 4e2624e

Browse files
chore: Add mobile: replacements to clipboard API wrappers
1 parent 6e06805 commit 4e2624e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

appium/webdriver/extensions/clipboard.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import base64
1616
from typing import TYPE_CHECKING, Optional, cast
1717

18+
from selenium.common.exceptions import UnknownMethodException
19+
1820
from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands
21+
from appium.protocols.webdriver.can_execute_scripts import CanExecuteScripts
22+
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
1923
from appium.webdriver.clipboard_content_type import ClipboardContentType
2024

2125
from ..mobilecommand import MobileCommand as Command
@@ -24,7 +28,7 @@
2428
from appium.webdriver.webdriver import WebDriver
2529

2630

27-
class Clipboard(CanExecuteCommands):
31+
class Clipboard(CanExecuteCommands, CanRememberExtensionPresence, CanExecuteScripts):
2832
def set_clipboard(
2933
self, content: bytes, content_type: str = ClipboardContentType.PLAINTEXT, label: Optional[str] = None
3034
) -> 'WebDriver':
@@ -39,13 +43,18 @@ def set_clipboard(
3943
Returns:
4044
Union['WebDriver', 'Clipboard']: Self instance
4145
"""
46+
ext_name = 'mobile: setClipboard'
4247
options = {
4348
'content': base64.b64encode(content).decode('UTF-8'),
4449
'contentType': content_type,
4550
}
4651
if label:
4752
options['label'] = label
48-
self.execute(Command.SET_CLIPBOARD, options)
53+
try:
54+
self.assert_extension_exists(ext_name).execute_script(ext_name, options)
55+
except UnknownMethodException:
56+
# TODO: Remove the fallback
57+
self.mark_extension_absence(ext_name).execute(Command.SET_CLIPBOARD, options)
4958
return cast('WebDriver', self)
5059

5160
def set_clipboard_text(self, text: str, label: Optional[str] = None) -> 'WebDriver':
@@ -68,9 +77,15 @@ def get_clipboard(self, content_type: str = ClipboardContentType.PLAINTEXT) -> b
6877
is supported on Android
6978
7079
Returns:
71-
base64-encoded string: Clipboard content. Or return an empty string if the clipboard is empty
80+
Clipboard content as bytearray. Or empty bytes if the clipboard is empty
7281
"""
73-
base64_str = self.execute(Command.GET_CLIPBOARD, {'contentType': content_type})['value']
82+
ext_name = 'mobile: getClipboard'
83+
options = {'contentType': content_type}
84+
try:
85+
base64_str = self.assert_extension_exists(ext_name).execute_script(ext_name, options)
86+
except UnknownMethodException:
87+
# TODO: Remove the fallback
88+
base64_str = self.mark_extension_absence(ext_name).execute(Command.GET_CLIPBOARD, options)['value']
7489
return base64.b64decode(base64_str)
7590

7691
def get_clipboard_text(self) -> str:

0 commit comments

Comments
 (0)