Skip to content

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

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

Merged
merged 3 commits 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
23 changes: 19 additions & 4 deletions appium/webdriver/extensions/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import base64
from typing import TYPE_CHECKING, Optional, cast

from selenium.common.exceptions import UnknownMethodException

from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands
from appium.protocols.webdriver.can_execute_scripts import CanExecuteScripts
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
from appium.webdriver.clipboard_content_type import ClipboardContentType

from ..mobilecommand import MobileCommand as Command
Expand All @@ -24,7 +28,7 @@
from appium.webdriver.webdriver import WebDriver


class Clipboard(CanExecuteCommands):
class Clipboard(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
def set_clipboard(
self, content: bytes, content_type: str = ClipboardContentType.PLAINTEXT, label: Optional[str] = None
) -> 'WebDriver':
Expand All @@ -39,13 +43,18 @@ def set_clipboard(
Returns:
Union['WebDriver', 'Clipboard']: Self instance
"""
ext_name = 'mobile: setClipboard'
options = {
'content': base64.b64encode(content).decode('UTF-8'),
'contentType': content_type,
}
if label:
options['label'] = label
self.execute(Command.SET_CLIPBOARD, options)
try:
self.assert_extension_exists(ext_name).execute_script(ext_name, options)
except UnknownMethodException:
# TODO: Remove the fallback
self.mark_extension_absence(ext_name).execute(Command.SET_CLIPBOARD, options)
return cast('WebDriver', self)

def set_clipboard_text(self, text: str, label: Optional[str] = None) -> 'WebDriver':
Expand All @@ -68,9 +77,15 @@ def get_clipboard(self, content_type: str = ClipboardContentType.PLAINTEXT) -> b
is supported on Android

Returns:
base64-encoded string: Clipboard content. Or return an empty string if the clipboard is empty
Clipboard content as bytearray. Or empty bytes if the clipboard is empty
"""
base64_str = self.execute(Command.GET_CLIPBOARD, {'contentType': content_type})['value']
ext_name = 'mobile: getClipboard'
options = {'contentType': content_type}
try:
base64_str = self.assert_extension_exists(ext_name).execute_script(ext_name, options)
except UnknownMethodException:
# TODO: Remove the fallback
base64_str = self.mark_extension_absence(ext_name).execute(Command.GET_CLIPBOARD, options)['value']
return base64.b64decode(base64_str)

def get_clipboard_text(self) -> str:
Expand Down
20 changes: 15 additions & 5 deletions test/unit/webdriver/device/clipboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,31 @@ def test_set_clipboard_with_url(self):
httpretty.register_uri(
httpretty.POST, appium_command('/session/1234567890/appium/device/set_clipboard'), body='{"value": ""}'
)
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/execute/sync'),
body='{"value": ""}',
)
driver.set_clipboard(bytes(str('http://appium.io/'), 'UTF-8'), ClipboardContentType.URL, 'label for android')

d = get_httpretty_request_body(httpretty.last_request())
assert d['content'] == 'aHR0cDovL2FwcGl1bS5pby8='
assert d['contentType'] == 'url'
assert d['label'] == 'label for android'
assert d['args'][0]['content'] == 'aHR0cDovL2FwcGl1bS5pby8='
assert d['args'][0]['contentType'] == 'url'
assert d['args'][0]['label'] == 'label for android'

@httpretty.activate
def test_set_clipboard_text(self):
driver = ios_w3c_driver()
httpretty.register_uri(
httpretty.POST, appium_command('/session/1234567890/appium/device/set_clipboard'), body='{"value": ""}'
)
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/execute/sync'),
body='{"value": ""}',
)
driver.set_clipboard_text('hello')

d = get_httpretty_request_body(httpretty.last_request())
assert d['content'] == 'aGVsbG8='
assert d['contentType'] == 'plaintext'
assert d['args'][0]['content'] == 'aGVsbG8='
assert d['args'][0]['contentType'] == 'plaintext'
Loading