uiautomator2(用python控制android), 安装和连接脱坑

https://github.com/openatx/uiautomator2

安装

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

Install weditor (UI Inspector)

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

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

pip install -U weditor

安装好之后,就可以在命令行运行weditor --help 确认是否安装成功了。

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

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

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

Install daemons to a device (Optional)

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

用usb连接手机

运行adb devices 查看是否存在设备, 如果没有参考最下面错误

# init 所有的已经连接到电脑的设备
python -m uiautomator2 init

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

安装提示success即可

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

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

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

之后参考github 学习使用

错误

adb devices没有设备

以小米手机为例, 需要开启开发者选项

打开设置 -> 我的设备 -> 全部参数 -> 连续点击【MIUI版本】选项知道提示【已开启开发者模式】

然后返回设置 -> 更多设置 -> 最下方开发者选项 -> 开启USB调试和USB安装

uiautomator2.exceptions.ConnectError或者RuntimeError: USB device xxx.xxx.xxx.xxx is offline 以及ATX显示ATXAgent Stopped

https://blog.csdn.net/weixin_44045331/article/details/107354853

这是没有开启atxagent, 手动用adb开启

USB连接

adb shell
chmod 755 /data/local/tmp/atx-agent
data/local/tmp/atx-agent version  # 查看版本
/data/local/tmp/atx-agent server -d  # 启动atx-agent并切换到后台运行

你可能感兴趣的:(python,python,android)