Skip to content

Commit

Permalink
add test_core
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Jul 15, 2024
1 parent c189745 commit be4d698
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion QUICK_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ d.orientation = 'natural'
d.freeze_rotation(True)

print(d.last_toast) # 获取显示的toast文本
d.clear_last_toast() # 重置一下
d.clear_toast() # 重置一下

d.open_notification()
d.open_quick_settings()
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ print(d.current_ime()) # 获取当前输入法ID
### Toast
```python
print(d.last_toast) # get last toast, if not toast return None
d.clear_last_toast()
d.clear_toast()
```
> Fixed in version 3.2.0
Expand Down
41 changes: 41 additions & 0 deletions demo_tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# coding: utf-8
# author: codeskyblue

from typing import Optional
import uiautomator2 as u2


def get_app_process_pid(d: u2.Device) -> Optional[int]:
for line in d.shell("ps -u shell").output.splitlines():
fields = line.split()
if fields[-1] == 'app_process':
pid = fields[1]
return int(pid)
return None


def kill_app_process(d: u2.Device) -> bool:
pid = get_app_process_pid(d)
if not pid:
return False
d.shell(f"kill {pid}")
return True


def test_uiautomator_keeper(d: u2.Device):
kill_app_process(d)
d.sleep(.2)
assert get_app_process_pid(d) is None
d.shell('rm /data/local/tmp/u2.jar')

d.start_uiautomator()
assert get_app_process_pid(d) > 0

d.stop_uiautomator()
assert get_app_process_pid(d) is None


def test_debug(d: u2.Device):
d.debug = True
d.info

8 changes: 5 additions & 3 deletions demo_tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ def test_send_keys(app: u2.Device):
num1.set_text('1')

num2.click()
app.send_keys('6')
assert num2.get_text() == '6'

for chars in ('1', '123abcDEF +-*/_', '你好,世界!'):
app.send_keys(chars, clear=True)
assert num2.get_text() == chars

app.clear_text()
app.send_keys('2')

app(text="Add").click()
result = app(className="android.widget.EditText", instance=2).get_text()
assert result == "3"
Expand Down
4 changes: 4 additions & 0 deletions demo_tests/test_watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding: utf-8
# author: codeskyblue

# TODO
2 changes: 1 addition & 1 deletion docs/2to3.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ print(d.device_info)
- 3.x d.last_toast (property)

- 2.x d.toast.reset()
- 3.x d.clear_last_toast()
- 3.x d.clear_toast()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "uiautomator2"
version = "0.0.0"
version = "3.2.0"
description = "uiautomator for android device"
homepage = "https://github.com/openatx/uiautomator2"
authors = ["codeskyblue <codeskyblue@gmail.com>"]
Expand Down
14 changes: 10 additions & 4 deletions uiautomator2/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,27 @@ def send_keys(self, text: str, clear: bool = False):
text (str): text to set
clear (bool): clear before set text
"""
if clear:
self.clear_text()

Check warning on line 106 in uiautomator2/_input.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/_input.py#L106

Added line #L106 was not covered by tests
if re.match(r'^[-+*\/_a-zA-Z0-9 ]+$', text):
self.shell(['input', 'text', text.replace(' ', '%s')])

Check warning on line 108 in uiautomator2/_input.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/_input.py#L108

Added line #L108 was not covered by tests
else:
self.__send_keys_with_ime(text)

Check warning on line 110 in uiautomator2/_input.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/_input.py#L110

Added line #L110 was not covered by tests

def __send_keys_with_ime(self, text: str):
try:
self.set_input_ime()
btext = text.encode('utf-8')
base64text = base64.b64encode(btext).decode()
cmd = "ADB_KEYBOARD_SET_TEXT" if clear else "ADB_KEYBOARD_INPUT_TEXT"
cmd = "ADB_KEYBOARD_INPUT_TEXT"

Check warning on line 117 in uiautomator2/_input.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/_input.py#L117

Added line #L117 was not covered by tests
self._must_broadcast(cmd, {"text": base64text})
return True
except AdbBroadcastError:
warnings.warn(
"set FastInputIME failed. use \"d(focused=True).set_text instead\"",
Warning)
return self(focused=True).set_text(text)
# warnings.warn("set FastInputIME failed. use \"adb shell input text\" instead", Warning)
# self.shell(["input", "text", text.replace(" ", "%s")])


def send_action(self, code: Union[str, int] = None):
"""
Simulate input method edito code
Expand Down
2 changes: 1 addition & 1 deletion uiautomator2/_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def exists(self):
'''check if the object exists in current window.'''
return Exists(self)

# @retry(UiObjectNotFoundError, delay=.5, tries=3, jitter=0.1, logger=logging) # yapf: disable
@property
@retry(UiObjectNotFoundError, delay=.5, tries=3, jitter=0.1, logger=logging) # yapf: disable
def info(self):
'''ui object info.'''
return self.jsonrpc.objInfo(self.selector)
Expand Down
9 changes: 1 addition & 8 deletions uiautomator2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def pool(self) -> Optional[int]:

def kill(self):
self._conn.close()
self.wait()

Check warning on line 64 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L64

Added line #L64 was not covered by tests


def launch_uiautomator(dev: adbutils.AdbDevice) -> MockAdbProcess:
Expand Down Expand Up @@ -270,11 +271,3 @@ def jsonrpc_call(self, method: str, params: Any = None, timeout: float = 10) ->
self.stop_uiautomator()
self.start_uiautomator()
return _jsonrpc_call(self._dev, method, params, timeout, self._debug)

class SimpleUiautomatorServer(BasicUiautomatorServer, AbstractUiautomatorServer):
@property
def info(self) -> Dict[str, Any]:
return self.jsonrpc_call("deviceInfo")

def dump_hierarchy(self, compressed: bool = False, pretty: bool = False) -> str:
return self.jsonrpc_call("dumpWindowHierarchy", [compressed, pretty])

0 comments on commit be4d698

Please sign in to comment.