Skip to content

Commit

Permalink
update send_keys and clear_text using 0.1.4 jar (#1036)
Browse files Browse the repository at this point in the history
update send_keys and clear_text using 0.1.4 jar

---------

Co-authored-by: 孙圣翔²⁰₂₁ <oncwnuImVQzmrqH0o4rQfDVAePRA@git.weixin.qq.com>
  • Loading branch information
codeskyblue and 孙圣翔²⁰₂₁ authored Sep 11, 2024
1 parent 18bad1b commit 9f0a946
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() # 清除输入框所有内容
Expand Down
17 changes: 17 additions & 0 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
19 changes: 4 additions & 15 deletions uiautomator2/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions uiautomator2/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion uiautomator2/assets/sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand Down

0 comments on commit 9f0a946

Please sign in to comment.