Skip to content

Commit c25a3e5

Browse files
authored
use u2.jar instead of test apk (#1004)
* use u2.jar instead of test apk * update toast api, use d.last_toast to get toast, d.clear_toast() to reset it * add more unittests * fix multi thread when use connect
1 parent 7ba4852 commit c25a3e5

36 files changed

+757
-278
lines changed

.coveragerc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
branch = True
33

44
omit =
5-
"tests/*"
6-
"docs/*"
5+
/tests/**
6+
/docs/*
7+
/*_tests/**
78

89
[report]
910
; Regexes for lines to exclude from consideration
@@ -23,4 +24,7 @@ exclude_also =
2324
; Don't complain about abstract methods, they aren't run:
2425
@(abc\.)?abstractmethod
2526

27+
except adbutils.AdbError
28+
@deprecated
29+
2630
ignore_errors = True

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ format:
66
test:
77
poetry run pytest -v mobile_tests/
88

9+
covtest:
10+
poetry run coverage run -m pytest -v demo_tests tests
11+
poetry run coverage html --include 'uiautomator2/**'
12+
13+
914
cov:
1015
poetry run pytest -v tests/ \
1116
--cov-config=.coveragerc \

QUICK_REFERENCE.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import uiautomator2 as u2
66
d = u2.connect("--serial-here--") # 只有一个设备也可以省略参数
77
d = u2.connect() # 一个设备时, read env-var ANDROID_SERIAL
88

9+
# 信息获取
10+
print(d.info)
11+
print(d.device_info)
12+
width, height = d.window_size()
13+
print(d.wlan_ip)
14+
print(d.serial)
15+
16+
## 截图
17+
d.screenshot() # Pillow.Image.Image格式
18+
d.screenshot().save("current_screen.jpg")
19+
20+
# 获取hierarchy
21+
d.dump_hierarchy() # str
22+
23+
# 设置查找元素等待时间,单位秒
24+
d.implicitly_wait(10)
25+
926
d.app_current() # 获取前台应用 packageName, activity
1027
d.app_start("io.appium.android.apis") # 启动应用
1128
d.app_start("io.appium.android.apis", stop=True) # 启动应用前停止应用
@@ -18,29 +35,58 @@ app.click(10, 20) # 坐标点击
1835

1936
# 无session状态下操作
2037
d.click(10, 20) # 坐标点击
38+
d.long_click(10, 10)
39+
d.double_click(10, 20)
40+
2141
d.swipe(10, 20, 80, 90) # 从(10, 20)滑动到(80, 90)
2242
d.swipe_ext("right") # 整个屏幕右滑动
2343
d.swipe_ext("right", scale=0.9) # 屏幕右滑,滑动距离为屏幕宽度的90%
44+
d.drag(10, 10, 80, 80)
2445

2546
d.press("back") # 模拟点击返回键
2647
d.press("home") # 模拟Home键
48+
d.long_press("volume_up")
2749

2850
d.send_keys("hello world") # 模拟输入,需要光标已经在输入框中才可以
2951
d.clear_text() # 清空输入框
3052

53+
d.screen_on() # wakeUp
54+
d.screen_off() # sleep screen
55+
56+
print(d.orientation) # left|right|natural|upsidedown
57+
d.orientation = 'natural'
58+
d.freeze_rotation(True)
59+
60+
print(d.last_toast) # 获取显示的toast文本
61+
d.clear_toast() # 重置一下
62+
63+
d.open_notification()
64+
d.open_quick_settings()
65+
66+
d.open_url("https://www.baidu.com")
67+
d.keyevent("HOME") # same as: input keyevent HOME
68+
3169
# 执行shell命令
3270
output, exit_code = d.shell("ps -A", timeout=60) # 执行shell命令,获取输出和exitCode
3371
output = d.shell("pwd").output # 这样也可以
3472
exit_code = d.shell("pwd").exit_code # 这样也可以
3573

74+
# Selector操作
75+
sel = d(text="Gmail")
76+
sel.wait()
77+
sel.click()
78+
79+
```
80+
81+
```python
82+
# XPath操作
3683
# 元素操作
3784
d.xpath("立即开户").wait() # 等待元素,最长等10s(默认)
3885
d.xpath("立即开户").wait(timeout=10) # 修改默认等待时间
3986

4087
# 常用配置
4188
d.settings['wait_timeout'] = 20 # 控件查找默认等待时间(默认20s)
4289

43-
# xpath操作
4490
d.xpath("立即开户").click() # 包含查找等待+点击操作,匹配text或者description等于立即开户的按钮
4591
d.xpath("//*[@text='私人FM']/../android.widget.ImageView").click()
4692

README.md

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,30 +1292,13 @@ print(d.current_ime()) # 获取当前输入法ID
12921292
12931293
> 更多参考: [IME_ACTION_CODE](https://developer.android.com/reference/android/view/inputmethod/EditorInfo)
12941294
1295-
### Toast (2.2版本之后有添加回来)
1296-
Show Toast (好像有点bug)
1297-
1295+
### Toast
12981296
```python
1299-
d.toast.show("Hello world")
1300-
d.toast.show("Hello world", 1.0) # show for 1.0s, default 1.0s
1297+
print(d.last_toast) # get last toast, if not toast return None
1298+
d.clear_toast()
13011299
```
13021300
1303-
Get Toast
1304-
1305-
```python
1306-
# [Args]
1307-
# 5.0: max wait timeout. Default 10.0
1308-
# 10.0: cache time. return cache toast if already toast already show up in recent 10 seconds. Default 10.0 (Maybe change in the furture)
1309-
# "default message": return if no toast finally get. Default None
1310-
d.toast.get_message(5.0, 10.0, "default message")
1311-
1312-
# common usage
1313-
assert "Short message" in d.toast.get_message(5.0, default="")
1314-
1315-
# clear cached toast
1316-
d.toast.reset()
1317-
# Now d.toast.get_message(0) is None
1318-
```
1301+
> Fixed in version 3.2.0
13191302
13201303
### XPath
13211304
Java uiautoamtor中默认是不支持xpath的,所以这里属于扩展的一个功能。速度不是这么的快。
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)