python连接手机_python-uiautomator2 安装以及连接手机调试

本文介绍了如何使用python-uiautomator2这个自动化测试工具进行Android应用测试。该工具提供了Python接口,方便在PC上编写测试代码并远程控制手机。通过安装、初始化、设备连接和编写测试脚本的步骤,详细展示了如何实现自动化测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、简介

python-uiautomator2是一个自动化测试开源工具,仅支持Android平台的原生应用测试。

2、支持平台及语言

python-uiautomator2封装了谷歌自带的uiautomator2测试框架,提供便利的python接口。他允许测试人员直接在PC上编写Python的测试代码,操作手机应用,完成自动化,大大提高了自动化代码编写的效率。

3、python-uiautomator2主要分为两个部分

python端:运行脚本,并向移动设备发送HTTP请求

移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。

安装

pip3 install --pre -U uiautomator

pip3 install pillow

#升级

pip3 install --upgrade --pre uiautomator2

设备安装atx-agent(注意手机端,会提示安装atx-agent)

macname@MacdeMacBook-Pro testenv3.7 % python3 -m uiautomator2 init* daemon not running; starting now at tcp:5037

*daemon started successfully

macname@MacdeMacBook-Pro testenv3.7 %ls

Pipfile file.json include

bin file.yaml lib

macname@MacdeMacBook-Pro testenv3.7 % cd ~/Desktop

通过usb连接,打开usb文件传输模式,打开开发者选项USB调试模式

获取设备序列号

macname@MacdeMacBook-Pro ~ %adb devices

List of devices attached

62C6J10232191223100411276 device

macname@MacdeMacBook-Pro ~ %

编写测试脚本

macname@MacdeMacBook-Pro Desktop %cat test.py

import uiautomator2asu2

d= u2.connect_usb('62C6J10232191223100411276')

d.toast.show("Hello world", 3)

d.screenshot("home.jpg")

print(d.info)

macname@MacdeMacBook-Pro Desktop %

运行测试脚本

macname@MacdeMacBook-Pro Desktop % python3 test.py

[W 210109 14:59:40 __init__:203] atx-agent has something wrong, auto recovering

[D 210109 14:59:40 __init__:287] device 62C6J10232191223100411276 is online

[I 210109 14:59:40 init:155] uiautomator2 version: 2.12.2.dev1

[I 210109 14:59:40 init:352] Install minicap, minitouch

minitouch |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 34.1 KB/34.1 KB

minicap.so |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 28.4 KB/28.4 KB

minicap |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 526.5 KB/526.5 KB

[I 210109 14:59:48 init:367] Install com.github.uiautomator, com.github.uiautomator.test 2.3.1

app-uiautomator.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 2.1 MB/2.1 MB

[I 210109 14:59:58 init:332] - app-uiautomator.apk installed

app-uiautomator-test.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 1.2 MB/1.2 MB

[I 210109 15:00:03 init:332] - app-uiautomator-test.apk installed

[I 210109 15:00:03 init:340] Install atx-agent 0.9.5

atx-agent_0.9.5_linux_armv7.tar.gz |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 4.1 MB/4.1 MB

[I 210109 15:00:06 init:377] Check atx-agent version

Successfully init AdbDevice(serial=62C6J10232191223100411276)

[D 210109 15:00:06 __init__:600] kill process(ps): uiautomator

[D 210109 15:00:07 __init__:619] uiautomator-v2 is starting ... left: 40.0s

[D 210109 15:00:08 __init__:619] uiautomator-v2 is starting ... left: 39.0s

[I 210109 15:00:08 __init__:583] uiautomator back to normal

{'currentPackageName': 'com.huawei.android.launcher', 'displayHeight': 2163, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 748, 'displayWidth': 1080, 'productName': 'HMA-AL00', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}

macname@MacdeMacBook-Pro Desktop %

安装weditor

安装

pip3 install --pre weditor

运行

python3 -m weditor

运行上述命令后,自动打开浏览器,然后在浏览器中,

输入设备序列号 62C6J10232191223100411276

进行连接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值