Skip to content

Commit 9f0a946

Browse files
codeskyblue孙圣翔²⁰₂₁
and
孙圣翔²⁰₂₁
authored
update send_keys and clear_text using 0.1.4 jar (#1036)
update send_keys and clear_text using 0.1.4 jar --------- Co-authored-by: 孙圣翔²⁰₂₁ <oncwnuImVQzmrqH0o4rQfDVAePRA@git.weixin.qq.com>
1 parent 18bad1b commit 9f0a946

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,11 +1271,12 @@ UiAutomator中的超时设置(隐藏方法)
12711271
Refs: [Google uiautomator Configurator](https://developer.android.com/reference/android/support/test/uiautomator/Configurator)
12721272
12731273
### Input method
1274-
这种方法通常用于不知道控件的情况下的输入。第一步需要切换输入法,然后发送adb广播命令,具体使用方法如下
1274+
这种方法通常用于不知道控件的情况下的输入。
12751275
12761276
```python
1277-
d.send_keys("你好123abcEFG") # adb广播输入
1278-
d.send_keys("你好123abcEFG", clear=True) # adb广播输入
1277+
# 目前采用从剪贴板粘贴的方式输入
1278+
d.send_keys("你好123abcEFG")
1279+
d.send_keys("你好123abcEFG", clear=True)
12791280
12801281
d.clear_text() # 清除输入框所有内容
12811282

uiautomator2/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,23 @@ def set_clipboard(self, text, label=None):
584584
label: User-visible label for the clip data.
585585
'''
586586
self.jsonrpc.setClipboard(label, text)
587+
588+
def clear_text(self):
589+
""" clear input text """
590+
self.jsonrpc.clearInputText()
591+
592+
def send_keys(self, text: str, clear: bool = False):
593+
"""
594+
send text to focused input area
595+
596+
Args:
597+
text: input text
598+
clear: clear text before input
599+
"""
600+
if clear:
601+
self.clear_text()
602+
self.clipboard = text
603+
self.jsonrpc.pasteClipboard()
587604

588605
def keyevent(self, v):
589606
"""

uiautomator2/_input.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,8 @@ def _must_broadcast(self, action: str, extras: Dict[str, str] = {}):
9696
if result.code != BORADCAST_RESULT_OK:
9797
raise AdbBroadcastError(f"broadcast {action} failed: {result.data}")
9898

99-
def send_keys(self, text: str, clear: bool = False):
100-
"""
101-
Args:
102-
text (str): text to set
103-
clear (bool): clear before set text
104-
"""
105-
if clear:
106-
self.clear_text()
107-
if re.match(r'^[-+*\/_a-zA-Z0-9 ]+$', text):
108-
self.shell(['input', 'text', text.replace(' ', '%s')])
109-
else:
110-
self.__send_keys_with_ime(text)
111-
112-
def __send_keys_with_ime(self, text: str):
99+
@deprecated(reason="use send_keys instead")
100+
def _send_keys_with_ime(self, text: str):
113101
try:
114102
self.set_input_ime()
115103
btext = text.encode('utf-8')
@@ -152,7 +140,8 @@ def send_action(self, code: Union[str, int] = None):
152140
else:
153141
self._must_broadcast('ADB_KEYBOARD_SMART_ENTER')
154142

155-
def clear_text(self):
143+
@deprecated(reason="use clear_text() instead")
144+
def _clear_text_with_ime(self):
156145
""" clear text
157146
Raises:
158147
EnvironmentError

uiautomator2/abstract.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import abc
88
from typing import Any, List, NamedTuple, Tuple, Union
9+
import typing
910
import adbutils
1011
from PIL import Image
1112
from uiautomator2._proto import Direction
@@ -41,6 +42,12 @@ def shell(self, cmdargs: Union[List[str], str]) -> ShellResponse:
4142
@abc.abstractmethod
4243
def adb_device(self) -> adbutils.AdbDevice:
4344
pass
45+
46+
@property
47+
@abc.abstractmethod
48+
def jsonrpc(self) -> typing.Any:
49+
pass
50+
4451

4552
class AbstractXPathBasedDevice(metaclass=abc.ABCMeta):
4653
@abc.abstractmethod

uiautomator2/assets/sync.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -e
55

66
APK_VERSION=$(cat ../version.py| grep apk_version | awk '{print $NF}')
77
APK_VERSION=${APK_VERSION//[\"\']}
8-
JAR_VERSION="0.1.3"
8+
JAR_VERSION="0.1.4"
99

1010
cd "$(dirname $0)"
1111

0 commit comments

Comments
 (0)