uiautomator2
此库基于2020.12更新版本
这里有一份快速参考,适合已经入门的人
import uiautomator2 as u2 d = u2.connect("--serial-here--") # 只有一个设备也可以省略参数 d = u2.connect() # 一个设备时 d = u2.connect("10.1.2.3") # 通过设备的IP连接(需要在同一局域网且设备上的atx-agent已经安装并启动) d.app_current() # 获取前台应用 packageName, activity d.app_start("com.example.app") # 启动应用 d.app_start("com.example.app", stop=True) # 启动应用前停止应用 d.app_stop("com.example.app") # 停止应用 app = d.session("com.example.app") # 启动应用并获取session # session的用途是操作的同时监控应用是否闪退,当闪退时操作,会抛出SessionBrokenError app.click(10, 20) # 坐标点击 # 无session状态下操作 d.click(10, 20) # 坐标点击 d.swipe(10, 20, 80, 90) # 从(10, 20)滑动到(80, 90) d.swipe_ext("right") # 整个屏幕右滑动 d.swipe_ext("right", scale=0.9) # 屏幕右滑,滑动距离为屏幕宽度的90% d.press("back") # 模拟点击返回键 d.press("home") # 模拟Home键 d.send_keys("hello world") # 模拟输入,需要光标已经在输入框中才可以 d.clear_text() # 清空输入框 # 执行shell命令 output, exit_code = d.shell("ps -A", timeout=60) # 执行shell命令,获取输出和exitCode output = d.shell("pwd").output # 这样也可以 exit_code = d.shell("pwd").exit_code # 这样也可以 # 元素操作 d.xpath("立即开户").wait() # 等待元素,最长等10s(默认) d.xpath("立即开户").wait(timeout=10) # 修改默认等待时间 # 常用配置 d.settings['wait_timeout'] = 20 # 控件查找默认等待时间(默认20s) # xpath操作 d.xpath("立即开户").click() # 包含查找等待+点击操作,匹配text或者description等于立即开户的按钮 d.xpath("//*[@text='私人FM']/../android.widget.ImageView").click() d.xpath('//*[@text="私人FM"]').get().info # 获取控件信息 for el in d.xpath('//android.widget.EditText').all(): print("rect:", el.rect) # output tuple: (left_x, top_y, width, height) print("bounds:", el.bounds) # output tuple: (left, top, right, bottom) print("center:", el.center()) el.click() # click operation print(el.elem) # 输出lxml解析出来的Node # 监控弹窗(在线程中监控) d.watcher.when("跳过").click() d.watcher.start()
准备一台(不要两台)开启了开发者选项的安卓手机,连接上电脑,确保执行adb devices可以看到连接上的设备。
运行pip3 install -U uiautomator2 安装uiautomator2
运行python3 -m uiautomator2 init安装包含httprpc服务的apk到手机+atx-agent, minicap, minitouch (在过去的版本中,这一步是必须执行的,但是从1.3.0之后的版本,当运行python代码u2.connect()时就会自动推送这些文件了)
命令行运行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
- atx-agent 运行在设备上的驻守程序,go开发,用于保活设备上相关的服务
- 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
- Open Scheme
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
- Screenrecord
- Image match
相关文章推荐
常见问题
- 停止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
测试是否安装成功 uiautomator2 --help
2.Install weditor (UI Inspector)
因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,我们开发了基于浏览器技术的weditor UI查看器。https://github.com/openatx/weditor
安装方法(备注: 目前最新的稳定版为 0.1.0)
pip install -U weditor
安装好之后,就可以在命令行运行weditor --help 确认是否安装成功了。
Windows系统可以使用命令在桌面创建一个快捷方式 weditor --shortcut
命令行直接输入 weditor 会自动打开浏览器,输入设备的ip或者序列号,点击Connect即可。
具体参考文章:浅谈自动化测试工具python-uiautomator2
3.Install daemons to a device (Optional)
电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch
# init 所有的已经连接到电脑的设备 python -m uiautomator2 init
有时候init也会出错,请参考手动Init指南
安装提示success即可
4.【可选】AppetizerIO 所见即所得脚本编辑器
AppetizerIO 提供了对uiautomator2的深度集成,可以图形化管理ATX设备,还有所见即所得脚本编辑器
到网站下载直接打开,首次使用需要注册账号
设备管理 界面里可以检查设备是否正常init,起停atx-agent,抓取atx-agent.log文件
测试脚本调出脚本助手,实时界面同步,点击界面直接插入各种代码,同时支持uiautomator和Appium
视频教程 请戳这里 其他文档在此
Connect to a device
There are two ways to connect to the device.
1.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)
1.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 or ANDROID_SERIAL. 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
- screenshot: 截图
$ uiautomator2 screenshot screenshot.jpg
- current: 获取当前包名和activity
$ uiautomator2 current { "package": "com.android.browser", "activity": "com.uc.browser.InnerUCMobile", "pid": 28478 }
- uninstall: 卸载
$ uiautomator2 uninstall <package-name> # 卸载一个包 $ uiautomator2 uninstall <package-name-1> <package-name-2> # 卸载多个包 $ uiautomator2 uninstall --all # 全部卸载
- stop: 停止应用
$ uiautomator2 stop com.example.app # 停止一个app $ uiautomator2 stop --all # 停止所有的app
- install: 安装apk,apk通过URL给出 (暂时不能用)
- healthcheck: 健康检查 (暂不能用)
API Documents
New command timeout
How long (in seconds) will wait for a new command from the client before assuming the client quit and ending the uiautomator service (Default 3 minutes)
配置accessibility服务的最大空闲时间,超时将自动释放。默认3分钟。
d.set_new_command_timeout(300) # change to 5 minutes, unit seconds
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
设置元素查找等待时间(默认20s)
d.implicitly_wait(10.0) # 也可以通过d.settings['wait_timeout'] = 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
# 默认的这种方法是先通过atx-agent解析apk包的mainActivity,然后调用am start -n $package/$activity启动 d.app_start("com.example.hello_world") # 使用 monkey -p com.example.hello_world -c android.intent.category.LAUNCHER 1 启动 # 这种方法有个副作用,它自动会将手机的旋转锁定给关掉 d.app_start("com.example.hello_world", use_monkey=True) # start with package name # 通过指定main activity的方式启动应用,等价于调用am start -n com.example.hello_world/.MainActivity d.app_start("com.example.hello_world", ".MainActivity")
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") # expect output #{ # "mainActivity": "com.github.uiautomator.MainActivity", # "label": "ATX", # "versionName": "1.1.7", # "versionCode": 1001007, # "size":1760809 #} # save app icon img = d.app_icon("com.examples.demo") img.save("icon.png")
List all running apps
d.app_list_running() # expect output # ["com.xxxx.xxxx", "com.github.uiautomator", "xxxx"]
Wait until app running
pid = d.app_wait("com.example.android") # 等待应用运行, return pid(int) if not pid: print("com.example.android is not running") else: print("com.example.android pid is %d" % pid) d.app_wait("com.example.android", front=True) # 等待应用前台运行 d.app_wait("com.example.android", timeout=20.0) # 最长等待时间20s(默认)
Added in version 1.2.0
Push and pull files
- push a file to the device
# push to a folder d.push("foo.txt", "/sdcard/") # push and rename d.push("foo.txt", "/sdcard/bar.txt") # push fileobj with open("foo.txt", 'rb') as f: d.push(f, "/sdcard/") # push and change file access mode d.push("foo.sh", "/data/local/tmp/", mode=0o755)
- pull a file from the device
d.pull("/sdcard/tmp.txt", "tmp.txt") # FileNotFoundError will raise if the file is not found on the device d.pull("/sdcard/some-file-not-exists.txt", "tmp.txt")
检查并维持设备端守护进程处于运行状态
d.healthcheck()
Auto click permission dialogs
注意注意disable_popups
函数,检测发现很不稳定,暂时不要使用,等候通知。
Import in version 0.1.1
d.disable_popups() # automatic skip popups d.disable_popups(False) # disable automatic skip popups
If this method is not working on your device, You can make a pull request or create an issue to enhance this function. I’ll show you how to do it.
1.Open uiautomatorviewer.bat
2.Get popup hierarchy
Now you know the button text and current package name. Make a pull request by update function disable_popups or create an issue if you are not familar with git and python.
Open Scheme
You can do it wire adb: adb shell am start -a android.intent.action.VIEW -d "appname://appnamehost"
Also you can do it with python code
d.open_url("https://www.baidu.com") d.open_url("taobao://taobao.com") # open Taobao app d.open_url("appname://appnamehost")