- 
uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作 - 安装uiautomator2
 - 使用pip安装
 
# 安装uiautomator2运行库
pip install --pre uiautomator2
  - 安装weditor,定位元素测试
pip install --pre --upgrade weditor
# 安装pillow,截图
pip install pillow- 使用前初始化
# 初始化uiautomator2库
python -m uiautomator2 init
# 打开调试工具
python -m weditor- 
主要语法 - 
启动app d.app_start("com.addcn.android.house591") 
- 
关闭app cls.d.app_stop("com.addcn.android.house591") 
- 
ResourceId定位 cls.d(resourceId="com.addcn.android.house591:id/ad_banner").click() 
- 
Text定位 d(text="精选").click() 
- 
Description定位 d(description="..").click() 
- 
ClassName定位 d(className="android.widget.TextView").click() 
- xpath定位
d.xpath("//*[@content-desc='分享']").click() 
 
- 
- 
其他操作 - 
组默认元素等待超时(秒)cls.d.wait_timeout = 20.0 #默认20 
- 元素拖拽
- 
开关点击 d(A).left(B), selects B on the left side of A. 
 d(A).right(B), selects B on the right side of A.
 d(A).up(B), selects B above A.
 d(A).down(B), selects B under A.
- 
选择“Wi-Fi”右侧的“开关” d(text="Wi‑Fi").right(className="android.widget.Switch").click() 
- 
获取/统计某个相同条件的数目 d(text="Add new").count 
 len(d(text="Add new"))
- 
得知数目之后,我们可以通过索引去定位 d(text="Add new")[0] 
 d(text="Add new")[1]
- 也可以遍历
 
- 
for view in d(text="Add new"):
    view.info- 截图
- 
截取屏幕截图并保存到计算机上的文件中,要求Android> = 4.2。 d.screenshot( “ home.jpg ”) # get PIL.Image格式化图像。当然,你需要首先安装pillow 
 image = d.screenshot() # default format =“pillow”
 image.save( “ home.jpg ”)#或home.png。目前,只有PNG和JPG支持
- 得到OpenCV的格式图像。当然,你需要先安装numpy和cv2
import cv2
image = d.screenshot(format = 'opencv') 
cv2.imwrite('home.jpg'图像)
#获取原始JPEG数据 
imagebin = d.screenshot(格式= '原始') 
打开("some.jpg","WB").WRITE(imagebin)- 手势操作
- 单击
d(text = "Settings").click() 
 
- 单击
- 
长按 d(text = "Settings").long_click() 
- 将对象拖向另一个点或另一个UI对象
#笔记:拖不能用于为Android <4.3。
#将UI对象拖动到屏幕点(x,y),0.5秒后 
d(text = "设置").drag_to(x,y, duration = 0.5)
#将UI对象拖动到另一个(中心位置)UI对象,在0.25秒 
d(text = "设置").drag_to( text = "Clock", duration = 0.25)- 在屏幕上滑动
# swipe from (sx, sy) to (ex, ey)
d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 steps
d.swipe(sx, sy, ex, ey, steps=10)- 在屏幕上拖拽
# drag from (sx, sy) to (ex, ey)
d.drag(sx, sy, ex, ey)
# drag from (sx, sy) to (ex, ey) with 10 steps
d.drag(sx, sy, ex, ey, steps=10)- 获取对象信息和状态
d(text="Settings").exists 
# 如果存在则为True,否则为假
or d.exists(text="Settings") # 进一步使用 d(text="Settings").exists(timeout=3) 
# 等待设置出现在3S,相同.wait(3)# 检索特定UI对象的信息
d(text="Settings").info# 获取/设置/清除可编辑字段的文本(例如,EditText小部件)
d(text = "Settings").get_text()   # get widget text 
d(text = "Settings").set_text("My text ...")   # 设置文本 
d(text = "Settings").clear_text()   # 清除文字- 系统常用按键
# press home key
d.press.home()
# press back key
d.press.back()
# the normal way to press back key
d.press("back")----亲测可用
# press keycode 0x07('0') with META ALT(0x02) on
d.press(0x07, 0x02)| 参数 | 描述 | 
|---|---|
| home | #手机Home键 | 
| back | #手机返回键 | 
| left | #对应键盘上的向右键<- | 
| right | #对应键盘上的向右键-> | 
| up | #对应键盘上的向上键 | 
| down | #对应键盘上的向下键 | 
| center | #选中? | 
| menu | #菜单 | 
| search | #查找? | 
| enter | #对应键盘上的Enter键 | 
| delete(or del) | #对应键盘上的DEL键 用于删除 | 
| recent(recent apps) | #任务切换 | 
| volume_up | #声音向上调整 | 
| volume_down | #声音向下调整 | 
| volume_mute | #静音按键 | 
| camera | #拍照 | 
| power | #电源键 | 
 
				 
				
暂无评论