Skip to content

Commit 330af23

Browse files
committed
fix clear_text not working on some device
1 parent 7b35d0b commit 330af23

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,4 @@ docs/*.rst
114114

115115
.DS_Store
116116
*.lock
117+
junit.xml

uiautomator2/__init__.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,31 +410,22 @@ def set_clipboard(self, text, label=None):
410410

411411
def clear_text(self):
412412
""" clear input text """
413-
try:
414-
# 这个问题基本不大
415-
# 不过考虑到u2.jar不一定升级成功了,所以还有留个兜底方案·
416-
self.jsonrpc.clearInputText()
417-
except:
418-
self._clear_text_with_ime()
413+
self.jsonrpc.clearInputText()
419414

420-
def send_keys(self, text: str, clear: bool = False):
415+
def send_keys(self, text: str):
421416
"""
422417
send text to focused input area
423418
424419
Args:
425420
text: input text
426421
clear: clear text before input
427422
"""
428-
if clear:
429-
self.clear_text()
430-
try:
431-
# setClipboard的兼容性并不是特别好,但是这样做有个优点就是不用装输入法了。
432-
self.clipboard = text
433-
if self.clipboard != text:
434-
raise UiAutomationError("setClipboard failed")
435-
self.jsonrpc.pasteClipboard()
436-
except:
437-
self._send_keys_with_ime(text)
423+
# 使用el =self(focused=True); el.set_text(el.get_text()+text)不可取
424+
# 因为placeholder中的文字也会加进去
425+
self.clipboard = text
426+
if self.clipboard != text:
427+
raise UiAutomationError("setClipboard failed")
428+
self.jsonrpc.pasteClipboard()
438429

439430
def keyevent(self, v):
440431
"""
@@ -869,7 +860,32 @@ def swipe_ext(self) -> SwipeExt:
869860

870861
class Device(_Device, _AppMixIn, _PluginMixIn, InputMethodMixIn, _DeprecatedMixIn):
871862
""" Device object """
872-
pass
863+
864+
def clear_text(self):
865+
""" clear input text """
866+
if self.is_input_ime_installed():
867+
InputMethodMixIn.clear_text(self)
868+
else:
869+
_Device.clear_text(self)
870+
871+
def send_keys(self, text: str, clear: bool = False):
872+
"""
873+
send text to focused input area
874+
875+
Args:
876+
text: input text
877+
clear: clear text before input
878+
"""
879+
if clear:
880+
self.clear_text()
881+
if self.is_input_ime_installed():
882+
InputMethodMixIn.send_keys(self, text)
883+
return
884+
try:
885+
_Device.send_keys(self, text)
886+
except:
887+
# 安装输入法后继续输入
888+
InputMethodMixIn.send_keys(self, text)
873889

874890

875891
class Session(Device):

uiautomator2/_input.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _must_broadcast(self, action: str, extras: Dict[str, str] = {}):
106106
if result.code != BORADCAST_RESULT_OK:
107107
raise AdbBroadcastError(f"broadcast {action} failed: {result.data}")
108108

109-
def _send_keys_with_ime(self, text: str):
109+
def send_keys(self, text: str):
110110
try:
111111
self.set_input_ime()
112112
btext = text.encode('utf-8')
@@ -149,17 +149,9 @@ def send_action(self, code: Union[str, int] = None):
149149
else:
150150
self._must_broadcast('ADB_KEYBOARD_SMART_ENTER')
151151

152-
def _clear_text_with_ime(self):
153-
""" clear text
154-
Raises:
155-
EnvironmentError
156-
"""
157-
try:
158-
self.set_input_ime(True)
159-
self._must_broadcast('ADB_KEYBOARD_CLEAR_TEXT')
160-
except AdbBroadcastError:
161-
# for Android simulator
162-
self(focused=True).clear_text()
152+
def clear_text(self):
153+
self.set_input_ime(True)
154+
self._must_broadcast('ADB_KEYBOARD_CLEAR_TEXT')
163155

164156
def current_ime(self) -> str:
165157
""" Current input method

0 commit comments

Comments
 (0)