Skip to content
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

fix send_keys, use ime when pasteClipboard failed #1039

Merged
merged 1 commit into from
Sep 24, 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
17 changes: 14 additions & 3 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,12 @@

def clear_text(self):
""" clear input text """
self.jsonrpc.clearInputText()
try:

Check warning on line 590 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L590

Added line #L590 was not covered by tests
# 这个问题基本不大
# 不过考虑到u2.jar不一定升级成功了,所以还有留个兜底方案·
self.jsonrpc.clearInputText()
except:
self._clear_text_with_ime()

Check warning on line 595 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L593-L595

Added lines #L593 - L595 were not covered by tests

def send_keys(self, text: str, clear: bool = False):
"""
Expand All @@ -599,8 +604,14 @@
"""
if clear:
self.clear_text()
self.clipboard = text
self.jsonrpc.pasteClipboard()
try:

Check warning on line 607 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L607

Added line #L607 was not covered by tests
# setClipboard的兼容性并不是特别好,但是这样做有个优点就是不用装输入法了。
self.clipboard = text

Check warning on line 609 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L609

Added line #L609 was not covered by tests
if self.clipboard != text:
raise UiAutomationError("setClipboard failed")
self.jsonrpc.pasteClipboard()
except:
self._send_keys_with_ime(text)

Check warning on line 614 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L611-L614

Added lines #L611 - L614 were not covered by tests

def keyevent(self, v):
"""
Expand Down
2 changes: 0 additions & 2 deletions uiautomator2/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def _must_broadcast(self, action: str, extras: Dict[str, str] = {}):
if result.code != BORADCAST_RESULT_OK:
raise AdbBroadcastError(f"broadcast {action} failed: {result.data}")

@deprecated(reason="use send_keys instead")
def _send_keys_with_ime(self, text: str):
try:
self.set_input_ime()
Expand Down Expand Up @@ -140,7 +139,6 @@ def send_action(self, code: Union[str, int] = None):
else:
self._must_broadcast('ADB_KEYBOARD_SMART_ENTER')

@deprecated(reason="use clear_text() instead")
def _clear_text_with_ime(self):
""" clear text
Raises:
Expand Down