Skip to content

Commit 19d4f4b

Browse files
chore: Add mobile: replacements to clipboard API wrappers (#998)
* chore: Add mobile: replacements to clipboard API wrappers * Fix order * update tests
1 parent 6e06805 commit 19d4f4b

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
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, CanExecuteScripts, CanRememberExtensionPresence):
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:

test/unit/webdriver/device/clipboard_test.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,31 @@ def test_set_clipboard_with_url(self):
2525
httpretty.register_uri(
2626
httpretty.POST, appium_command('/session/1234567890/appium/device/set_clipboard'), body='{"value": ""}'
2727
)
28+
httpretty.register_uri(
29+
httpretty.POST,
30+
appium_command('/session/1234567890/execute/sync'),
31+
body='{"value": ""}',
32+
)
2833
driver.set_clipboard(bytes(str('http://appium.io/'), 'UTF-8'), ClipboardContentType.URL, 'label for android')
2934

3035
d = get_httpretty_request_body(httpretty.last_request())
31-
assert d['content'] == 'aHR0cDovL2FwcGl1bS5pby8='
32-
assert d['contentType'] == 'url'
33-
assert d['label'] == 'label for android'
36+
assert d['args'][0]['content'] == 'aHR0cDovL2FwcGl1bS5pby8='
37+
assert d['args'][0]['contentType'] == 'url'
38+
assert d['args'][0]['label'] == 'label for android'
3439

3540
@httpretty.activate
3641
def test_set_clipboard_text(self):
3742
driver = ios_w3c_driver()
3843
httpretty.register_uri(
3944
httpretty.POST, appium_command('/session/1234567890/appium/device/set_clipboard'), body='{"value": ""}'
4045
)
46+
httpretty.register_uri(
47+
httpretty.POST,
48+
appium_command('/session/1234567890/execute/sync'),
49+
body='{"value": ""}',
50+
)
4151
driver.set_clipboard_text('hello')
4252

4353
d = get_httpretty_request_body(httpretty.last_request())
44-
assert d['content'] == 'aGVsbG8='
45-
assert d['contentType'] == 'plaintext'
54+
assert d['args'][0]['content'] == 'aGVsbG8='
55+
assert d['args'][0]['contentType'] == 'plaintext'

0 commit comments

Comments
 (0)