uiautomator2使用教程

UiAutomator是Google提供的用来做安卓自动化测试的一个Java库,基于Accessibility服务。功能很强,可以对第三方App进行测试,获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点:1. 测试脚本只能使用Java语言 2. 测试脚本要打包成jar或者apk包上传到设备上才能运行。

我们希望测试逻辑能够用Python编写,能够在电脑上运行的时候就控制手机。这里要非常感谢 Xiaocong He (@xiaocong),他将这个想法实现了出来(见xiaocong/uiautomator),原理是在手机上运行了一个http rpc服务,将uiautomator中的功能开放出来,然后再将这些http接口封装成Python库。 因为xiaocong/uiautomator这个库,已经很久不见更新。所以我们直接fork了一个版本,为了方便做区分我们就在后面加了个2 openatx/uiautomator2

除了对原有的库的bug进行了修复,还增加了很多新的Feature。主要有以下部分:

设备和开发机可以脱离数据线,通过WiFi互联(基于atx-agent)
集成了openstf/minicap达到实时屏幕投频,以及实时截图
集成了openstf/minitouch达到精确实时控制设备
修复了xiaocong/uiautomator经常性退出的问题
代码进行了重构和精简,方便维护
实现了一个设备管理平台(也支持iOS) atxserver2
这里要先说明下,因为经常有很多人问 openatx/uiautomator2 并不支持iOS测试,需要iOS自动化测试,可以转到这个库 openatx/facebook-wda。

PS: 这个库 https://github.com/NeteaseGame/ATX 目前已经不维护了,请尽快更换。

Requirements
Android版本 4.4+
Python 3.6+
如果仍在用python2, 需要使用命令pip install -U "uiautomator2<=1.0.0"安装

QUICK START
先准备一台(不要两台)开启了开发者选项的安卓手机,连接上电脑,确保执行adb devices可以看到连接上的设备。

运行pip3 install -U uiautomator2安装uiautomator2
运行python3 -m uiautomator2 init安装包含httprpc服务的apk到手机+atx-agent, minicap, minitouch
一般情况下都会成功,不过也可能会有意外。可以加QQ群反馈问题,群里有很多大佬可以帮你解决问题。

命令行运行python打开python交互窗口。然后将下面的命令输入到窗口中。

import uiautomator2 as u2

d = u2.connect() # connect to device
print(d.info)
这时看到类似下面的输出,就可以正式开始用我们这个库了。因为这个库功能太多,后面还有很多的内容,需要慢慢去看 …

{‘currentPackageName’: ‘net.oneplus.launcher’, ‘displayHeight’: 1920, ‘displayRotation’: 0, ‘displaySizeDpX’: 411, ‘displaySizeDpY’: 731, ‘displayWidth’: 1080, ‘productName’: ‘OnePlus5’, ’
screenOn’: True, ‘sdkInt’: 27, ‘naturalOrientation’: True}
相关项目
设备管理平台,设备多了就会用到 atxserver2
专门与adb进行交互的库 adbutils
https://github.com/openatx/atx-agent
weditor[https://github.com/openatx/weditor] 类似于uiautomatorviewer,专门为本项目开发的辅助编辑器
Installation

Connect to a device

Command line

Global settings

Debug HTTP requests
Implicit wait
App management

Install an app
Launch an app
Stop an app
Stop all running apps
Push and pull files
Auto click permission dialogs
UI automation

Shell commands
Session
Retrieve the device info
Key Events
Gesture interaction with the device
Screen-related
Selector
Watcher
Global settings
Input method
Toast
XPath
常见问题

停止UiAutomator守护服务,释放AccessibilityService
502错误
Connection Error, 深度睡眠, 点击偏差 等
实验性功能

远程投屏
htmlreport
诊断uiautomator2方法
Plugin
Hooks
失败时弹出提示框
项目历史

Contributors

LICENSE

Installation
Install uiautomator2

Since uiautomator2 is still under development, you have to add --pre to install the development version

pip install --upgrade --pre uiautomator2

Or you can install directly from github source

git clone https://github.com/openatx/uiautomator2
pip install -e uiautomator2
Optionally, pillow is needed to process screenshot data.

pip install pillow
Install daemons to a device 电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

init 所有的已经连接到电脑的设备

python -m uiautomator2 init
有时候init也会出错,请参考手动Init指南

安装提示success即可

Install weditor (UI Inspector)

因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,我们开发了基于浏览器技术的weditor UI查看器。https://github.com/openatx/weditor

安装方法(备注: 目前最新的稳定版为 0.1.0)

pip install -U weditor
Windows系统可以使用命令在桌面创建一个快捷方式 python -m weditor --shortcut

命令行启动 python -m weditor 会自动打开浏览器,输入设备的ip或者序列号,点击Connect即可。

具体参考文章:浅谈自动化测试工具python-uiautomator2

【可选】AppetizerIO 所见即所得脚本编辑器

AppetizerIO 提供了对uiautomator2的深度集成,可以图形化管理ATX设备,还有所见即所得脚本编辑器

到网站下载直接打开,首次使用需要注册账号
设备管理 界面里可以检查设备是否正常init,起停atx-agent,抓取atx-agent.log文件
APP测试->脚本助手调出脚本助手,实时界面同步,点击界面直接插入各种代码,同时支持uiautomator和Appium
视频教程 请戳这里 其他文档在此
Connect to a device
There are two ways to connect to the device.

Through WiFi
Suppose device IP is 10.0.0.1 and your PC is in the same network.

import uiautomator2 as u2

d = u2.connect(‘10.0.0.1’) # alias for u2.connect_wifi(‘10.0.0.1’)
print(d.info)
Through USB
Suppose the device serial is 123456f (seen from adb devices)

import uiautomator2 as u2

d = u2.connect(‘123456f’) # alias for u2.connect_usb(‘123456f’)
print(d.info)
Through ADB WiFi
import uiautomator2 as u2

d = u2.connect_adb_wifi(“10.0.0.1:5555”)

Equals to

+ Shell: adb connect 10.0.0.1:5555

+ Python: u2.connect_usb(“10.0.0.1:5555”)

Calling u2.connect() with no argument, uiautomator2 will obtain device IP from the environment variable ANDROID_DEVICE_IP. If this environment variable is empty, uiautomator will fall back to connect_usb and you need to make sure that there is only one device connected to the computer.

Command line
其中的$device_ip代表设备的ip地址

如需指定设备需要传入–serial 如 python3 -m uiautomator2 --serial bff1234 , SubCommand为子命令(init,或者screenshot等)

1.0.3 Added: python3 -m uiautomator2可以简写为uiautomator2

init: 为设备安装所需要的程序

uiautomator2 init

If you need specify device to init, pass --serial

python3 -m uiautomator2 init --serial your-device-serial
screenshot: 截图

$ python -m uiautomator2 screenshot screenshot.jpg
uninstall: 卸载

python -m uiautomator2 uninstall # 卸载一个包
python -m uiautomator2 uninstall # 卸载多个包
python -m uiautomator2 uninstall --all # 全部卸载
install: 安装apk,apk通过URL给出 (暂时不能用)

clear-cache: 清空缓存 (废弃中,目前已经不需要改接口)

app-stop-all: 停止所有应用 (暂不能用)

healthcheck: 健康检查 (咱不能用)

API Documents
Global settings
This part contains some global settings

Debug HTTP requests
Trace HTTP requests and response to find out how it works.

d.debug = True
d.info
12:32:47.182 $ curl -X POST -d ‘{“jsonrpc”: “2.0”, “id”: “b80d3a488580be1f3e9cb3e926175310”, “method”: “deviceInfo”, “params”: {}}’ ‘http://127.0.0.1:54179/jsonrpc/0’
12:32:47.225 Response >>>
{“jsonrpc”:“2.0”,“id”:“b80d3a488580be1f3e9cb3e926175310”,“result”:{“currentPackageName”:“com.android.mms”,“displayHeight”:1920,“displayRotation”:0,“displaySizeDpX”:360,“displaySizeDpY”:640,“displayWidth”:1080,“productName”
:“odin”,“screenOn”:true,“sdkInt”:25,“naturalOrientation”:true}}
<<< END
Implicit wait
Set default element wait time, unit seconds

d.implicitly_wait(10.0)
d(text=“Settings”).click() # if Settings button not show in 10s, UiObjectNotFoundError will raised

print(“wait timeout”, d.implicitly_wait()) # get default implicit wait
This function will have influence on click, long_click, drag_to, get_text, set_text, clear_text, etc.

App management
This part showcases how to perform app management

Install an app
We only support installing an APK from a URL

d.app_install(‘http://some-domain.com/some.apk’)
Launch an app
d.app_start(“com.example.hello_world”) # start with package name
Stop an app

equivalent to am force-stop, thus you could lose data

d.app_stop(“com.example.hello_world”)

equivalent to pm clear

d.app_clear(‘com.example.hello_world’)
Stop all running apps

stop all

d.app_stop_all()

stop all app except for com.examples.demo

d.app_stop_all(excludes=[‘com.examples.demo’])
Get app info
d.app_info(“com.examples.demo”)

e

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值