diff --git a/uiautomator2/__init__.py b/uiautomator2/__init__.py index 36f9671..6ded921 100644 --- a/uiautomator2/__init__.py +++ b/uiautomator2/__init__.py @@ -584,6 +584,23 @@ def set_clipboard(self, text, label=None): label: User-visible label for the clip data. ''' self.jsonrpc.setClipboard(label, text) + + def clear_text(self): + """ clear input text """ + self.jsonrpc.clearInputText() + + def send_keys(self, text: str, clear: bool = False): + """ + send text to focused input area + + Args: + text: input text + clear: clear text before input + """ + if clear: + self.clear_text() + self.clipboard = text + self.jsonrpc.pasteClipboard() def keyevent(self, v): """ diff --git a/uiautomator2/_input.py b/uiautomator2/_input.py index f867067..a753077 100644 --- a/uiautomator2/_input.py +++ b/uiautomator2/_input.py @@ -96,20 +96,8 @@ def _must_broadcast(self, action: str, extras: Dict[str, str] = {}): if result.code != BORADCAST_RESULT_OK: raise AdbBroadcastError(f"broadcast {action} failed: {result.data}") - def send_keys(self, text: str, clear: bool = False): - """ - Args: - text (str): text to set - clear (bool): clear before set text - """ - if clear: - self.clear_text() - if re.match(r'^[-+*\/_a-zA-Z0-9 ]+$', text): - self.shell(['input', 'text', text.replace(' ', '%s')]) - else: - self.__send_keys_with_ime(text) - - def __send_keys_with_ime(self, text: str): + @deprecated(reason="use send_keys instead") + def _send_keys_with_ime(self, text: str): try: self.set_input_ime() btext = text.encode('utf-8') @@ -152,7 +140,8 @@ def send_action(self, code: Union[str, int] = None): else: self._must_broadcast('ADB_KEYBOARD_SMART_ENTER') - def clear_text(self): + @deprecated(reason="use clear_text() instead") + def _clear_text_with_ime(self): """ clear text Raises: EnvironmentError diff --git a/uiautomator2/abstract.py b/uiautomator2/abstract.py index 867a997..8f52733 100644 --- a/uiautomator2/abstract.py +++ b/uiautomator2/abstract.py @@ -6,6 +6,7 @@ import abc from typing import Any, List, NamedTuple, Tuple, Union +import typing import adbutils from PIL import Image from uiautomator2._proto import Direction @@ -41,6 +42,12 @@ def shell(self, cmdargs: Union[List[str], str]) -> ShellResponse: @abc.abstractmethod def adb_device(self) -> adbutils.AdbDevice: pass + + @property + @abc.abstractmethod + def jsonrpc(self) -> typing.Any: + pass + class AbstractXPathBasedDevice(metaclass=abc.ABCMeta): @abc.abstractmethod diff --git a/uiautomator2/assets/sync.sh b/uiautomator2/assets/sync.sh index 46efedc..8e9cb1e 100755 --- a/uiautomator2/assets/sync.sh +++ b/uiautomator2/assets/sync.sh @@ -5,7 +5,7 @@ set -e APK_VERSION=$(cat ../version.py| grep apk_version | awk '{print $NF}') APK_VERSION=${APK_VERSION//[\"\']} -JAR_VERSION="0.1.3" +JAR_VERSION="0.1.4" cd "$(dirname $0)"