uiautomator2学习笔记
准备工作
要求:
Android版本 4.4+
Python版本3.6+
连接手机:
开启开发者选项,
开启usb调试,
连接电脑,adb devices可以看到设备号
安装uiautomator2:python -m pip install -U uiautomator2
验证是否连接成功:
import uiautomator2 as u2
driver = u2.connect() # connect to device
print(driver.info)
执行以上代码有正确输出
安装:
1、Install uiautomator2
python -m pip install --upgrade --pre uiautomator2
测试是否安装成功 uiautomator2 --help
2、Install weditor (UI Inspector)
python -m pip install -U weditor
在命令行运行weditor --help 确认是否安装成功
3、Install daemons to a device (Optional)
电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序
python -m uiautomator2 init
安装提示success即可
4、【可选】AppetizerIO 所见即所得脚本编辑器
AppetizerIO 提供了对uiautomator2的深度集成,可以图形化管理ATX设备,还有所见即所得脚本编辑器
到网站下载直接打开,首次使用需要注册账号
设备管理 界面里可以检查设备是否正常init,起停atx-agent,抓取atx-agent.log文件
测试脚本调出脚本助手,实时界面同步,点击界面直接插入各种代码,同时支持uiautomator和Appium
连接设备
1、通过wifi,需确保同一网络
import uiautomator2 as u2
driver = u2.connect('10.0.0.1') # alias for u2.connect_wifi('10.0.0.1')
print(driver.info)
2、通过usb线
import uiautomator2 as u2
d = u2.connect('M9N7N15930001618') # 括号内为adb devices 获取的设备号
print(d.info)
打开weditor UI查看器
cmd命令:weditor
1、浏览器打开
2、输入设备号点击连接
3、成功连接设备后刷新获取最新页面
4、鼠标点击要找的元素,进行定位
cmd命令操作
1、截图
uiautomator2 screenshot screenshot.jpg 截图保存至当前文件夹,命名为screenshot.jpg
uiautomator2 screenshot E:\liang\tools\screenshot.jpg 截图保存至指定文件夹,命名为screenshot.jpg
2、获取当前包名和activity
uiautomator2 current
{
"package": "com.android.browser",
"activity": "com.uc.browser.InnerUCMobile",
"pid": 28478
}
3、卸载
uiautomator2 uninstall <package-name> # 卸载一个包
uiautomator2 uninstall <package-name-1> <package-name-2> # 卸载多个包
uiautomator2 uninstall --all # 全部卸载
4、stop: 停止应用
uiautomator2 stop com.example.app # 停止一个app
uiautomator2 stop --all # 停止所有的app
API Documents:
1、启动/关闭APP
5、 运行/关闭app
d.app_start('包名')
d.app_stop('包名')
d.app_clear('包名')
扩展:不知道怎么获取包名的可以打开一个app,doc下输入uiautomator2 current获取
{
"package": "com.android.browser",
"activity": "com.uc.browser.InnerUCMobile",
"pid": 28478
}
连接手机,启动APP
import uiautomator2 as u2
d = u2.connect('M9N7N15930001618') #括号内为adb devices获取的设备号
d.app_start("com.tencent.wework") #括号内为要启动的APP包名
API
手势交互
点击屏幕(坐标,支持相对坐标)
d.click(x, y)
d.click(0.5, 0.5) #其中(0.235, 0.456) 代表 X(50%) Y(50%)
双击
d.double_click(x, y) #默认两次点击相隔0.1秒
d.double_click(x, y, 1) #两次点击相隔1秒
长按
d.long_click(x, y) #默认按0.5秒
d.long_click(x, y, 5) # l长按5秒
滑动(滑动屏幕)
d.swipe(sx, sy, ex, ey) 从坐标(sx,sy)滑动至坐标(ex,ey),支持相对坐标
d.swipe(sx, sy, ex, ey, 0.5) # 默认滑动时间0.5秒,可修改
SwipeExt 扩展功能
d.swipe_ext("right") # 屏幕右滑,4选1 "left", "right", "up", "down"
d.swipe_ext("right", scale=0.9) # 默认0.9, 滑动距离为屏幕宽度的90%
d.swipe_ext("right", box=(0, 0, 100, 100)) # 在 (0,0) -> (100, 100) 这个区域做滑动
拖动(例:拖动某个APP)
d.drag(sx, sy, ex, ey) #支持相对坐标
d.drag(sx, sy, ex, ey, 0.5) # 默认滑动时间0.5秒,可修改
多个点连续滑动(典型案例:9宫格解锁)
从point(x0, y0) 滑动到point(x1, y1)然后滑动到point(x2, y2)...等等
d.swipe_points([(x0, y0), (x1, y1), (x2, y2)], 0.2)) #0.2为每次两点之间滑动的时间,坐标可以是绝对坐标,也可以是相对坐标
d.swipe_points([(0.235, 0.456), (0.503, 0.449), (0.509, 0.601), (0.777, 0.603), (0.771, 0.763), (0.222, 0.75)], 0.2) #其中(0.235, 0.456) 代表 X(23.5%) Y(45.6%)
模拟按下-等待-移动-松开
d.touch.down(10, 10) # 模拟按下
time.sleep(0.01) # down 和 move 之间的延迟,自己控制
d.touch.move(15, 15) # 模拟移动
d.touch.up() # 模拟抬起
截图
d.screenshot("E:\liang\tools\screenshot.jpg")传入路径和名称
元素选择器
#定位text为'Clock'并且className为'android.widget.TextView'的元素
d(text='Clock', className='android.widget.TextView')
后代元素
#定位className为"android.widget.ListView"的元素下面text为"Bluetooth"的元素
d(className
