From 9f0a9461061ef276b5c29abaf686773fe00a5dc3 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Wed, 11 Sep 2024 20:19:25 +0800 Subject: [PATCH] update send_keys and clear_text using 0.1.4 jar (#1036) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update send_keys and clear_text using 0.1.4 jar --------- Co-authored-by: 孙圣翔²⁰₂₁ --- README.md | 7 ++++--- uiautomator2/__init__.py | 17 +++++++++++++++++ uiautomator2/_input.py | 19 ++++--------------- uiautomator2/abstract.py | 7 +++++++ uiautomator2/assets/sync.sh | 2 +- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 35d3b88f..23a3d587 100644 --- a/README.md +++ b/README.md @@ -1271,11 +1271,12 @@ UiAutomator中的超时设置(隐藏方法) Refs: [Google uiautomator Configurator](https://developer.android.com/reference/android/support/test/uiautomator/Configurator) ### Input method -这种方法通常用于不知道控件的情况下的输入。第一步需要切换输入法,然后发送adb广播命令,具体使用方法如下 +这种方法通常用于不知道控件的情况下的输入。 ```python -d.send_keys("你好123abcEFG") # adb广播输入 -d.send_keys("你好123abcEFG", clear=True) # adb广播输入 +# 目前采用从剪贴板粘贴的方式输入 +d.send_keys("你好123abcEFG") +d.send_keys("你好123abcEFG", clear=True) d.clear_text() # 清除输入框所有内容 diff --git a/uiautomator2/__init__.py b/uiautomator2/__init__.py index 36f9671c..6ded921e 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 f867067e..a7530773 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 867a9977..8f52733a 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 46efedcd..8e9cb1e2 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)"