ATX ATX-uiautomator2 使用 QPython 在 Android 手机内执行自动化

linpengcheng · February 11, 2018 · Last by 芋头不吃 replied at July 05, 2019 · 17224 hits

根据帖子
在 Android 手机内执行 UI 自动化测试
动手实践了一把,把相关的操作步骤记录一下,由于uiautomator2 版本的升级 ,需要依赖的库在原帖上增加了progressretry
手机上直接push电脑上的uiautomator2库,
uiautomator2在手机上还需要安装依赖库为:
huamanizeprogressrequestsretry 四个

下面是具体的实践步骤

电脑端uiautomator2安装

1、安装uiautomator2,执行命令
pip install --pre -U uiautomator2

2、设备安装atx-agent
首先Android设备连接到PC,并能够adb devices发现该设备,执行命令
python -m uiautomator2 init

最后提示success,代表atx-agent初始化成功。

C:\Users\LiN>python -m uiautomator2 init
2018-02-11 20:15:21,139 - __main__.py:243 - INFO - Device(C4Y5T16810004018) init
ialing ...
2018-02-11 20:15:21,917 - __main__.py:110 - INFO - install minicap
2018-02-11 20:15:22,247 - __main__.py:117 - INFO - install minitouch
2018-02-11 20:15:22,976 - __main__.py:132 - INFO - apk(1.0.9) already installed,
skip
2018-02-11 20:15:23,139 - __main__.py:164 - INFO - atx-agent(0.2.0) already inst
alled, skip
2018-02-11 20:15:23,140 - __main__.py:198 - INFO - launch atx-agent daemon
2018-02-11 20:15:25,371 - __main__.py:213 - INFO - atx-agent output: server star
ted, listening on 192.168.31.128:7912
2018-02-11 20:15:25,373 - __main__.py:214 - INFO - success

QPython安装

QPython是一个能让安卓手机运行和编写Python的APP,Github下载地址下载链接
下载qpython-release.apk,然后使用adb install安装即可。
安装好之后,由于uiautomator2的库依赖huamanize、progress和requests,打开QPython,点击QPYPI,然后点击INSTALL WITH PYTHON'S PYPI,分别执行:

pip install requests
pip install humanize
pip install progress

安装成功即可。

将相关的库及脚本文件push到手机内

在手机上运行编写的自动化脚本文件,需要用到uiautomator2库和相关的依赖库retry,由于INSTALL WITH PYTHON'S PYPI无法直接安装,所以直接将电脑上uiautomator2库的整个文件夹复制到制定的文件夹下就好了。
经过观察QPython中路径在这个下面:
/storage/emulated/0/qpython

将uiautomator2库复制到qpython/lib/python2.7/site-packages下,执行命令
adb push C:\Python35\Lib\site-packages\uiautomator2 /storage/emulated/0/qpython/lib/python2.7/site-packages

将retry库复制到qpython/lib/python2.7/site-packages下,执行命令
adb push C:\Python35\Lib\site-packages\retry /storage/emulated/0/qpython/lib/python2.7/site-packages

将写好的自动化脚本文件atx_agent_demo.py复制到qpython/scripts3下,执行命令
C:\Users\LiN>adb push D:\PycharmProjects\autotest\atx_agent_demo.py /storage/emulated/0/qpython/scripts3

手机端运行脚本

文件atx_agent_demo.py用网易云音乐来进行简单的demo演示

import uiautomator2 as ut2


def main():
u = ut2.connect('http://0.0.0.0:7912')
print(u.info)
u.app_start('com.netease.cloudmusic')
u(text='私人FM').click()
u(description='转到上一层级').click()
u(text='每日推荐').click()
u(description='转到上一层级').click()
u(text='歌单').click()
u(description='转到上一层级').click()
u(text='排行榜').click()
u(description='转到上一层级').click()


if __name__ == '__main__':
main()

打开QPython,点击文件,然后点击dcripts找到atx_agent_demo.py,运行即可。附上运行的GIF

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 16 条回复 时间 点赞

点赞

—— 来自TesterHome官方 安卓客户端

非常值得点赞

我之前一直想实现自动打开微信的方法,发现是主机IP没有配置好,要配置为0.0.0.0

—— 来自TesterHome官方 安卓客户端

别的小米魅族手机运行都成功,就只有华为的荣耀手机这个奇葩,首先是包装不上,再者我从电脑拷贝包OK后,运行程序全是Segmentation default,真的是服了!!请问楼主有遇到类似情况吗

dengehello 回复

没在很多手机上试过诶 只在一台华为P9上边实践了下 之后就没深入下去了

应该是script 文件夹。script3 是个文件

为什么我import uiautomator2这一步就报错了啊,我已经把PC端的uiautomator2、retry都放到安卓的site-packages里面了啊!
然后,我看了日志,看到好像是import requests的问题,我把PC端的那些个包都给丢过去了,结果还是一样

莲蓬1 回复

requests不是qpython可以直接安装的吗 我记得好像会顺带装上其他的一些东西的 你用qpython安装requests试试看

python -m uiautomator2 init这步会报错,装不了包-华为荣耀9。求问!

[?25h2018-08-09 22:26:01,530 - main.py:143 - INFO - app-uiautomator.apk(1.1.0) installing ...
Traceback (most recent call last):
File "F:\Python2.7.15\lib\runpy.py", line 174, in run_module_as_main
"
main", fname, loader, pkg_name)
File "F:\Python2.7.15\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "F:\Python2.7.15\lib\site-packages\uiautomator2__main
.py", line 356, in
main()
File "F:\Python2.7.15\lib\site-packages\uiautomator2__main
.py", line 352, in main
fire.Fire(MyFire)
File "F:\Python2.7.15\lib\site-packages\fire\core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "F:\Python2.7.15\lib\site-packages\fire\core.py", line 366, in _Fire
component, remaining_args)
File "F:\Python2.7.15\lib\site-packages\fire\core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "F:\Python2.7.15\lib\site-packages\uiautomator2__main
.py", line 273, in init
ignore_apk_check)
File "F:\Python2.7.15\lib\site-packages\uiautomator2__main
.py", line 291, in _init_with_serial
ins.install_uiautomator_apk(apk_version, reinstall)
File "F:\Python2.7.15\lib\site-packages\uiautomator2__main
.py", line 144, in install_uiautomator_apk
path = cache_download(app_url)
File "F:\Python2.7.15\lib\site-packages\uiautomator2__main
_.py", line 73, in cache_download
r = requests.get(url, stream=True)
File "F:\Python2.7.15\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "F:\Python2.7.15\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "F:\Python2.7.15\lib\site-packages\requests\sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "F:\Python2.7.15\lib\site-packages\requests\sessions.py", line 644, in send
history = [resp for resp in gen] if allow_redirects else []
File "F:\Python2.7.15\lib\site-packages\requests\sessions.py", line 222, in resolve_redirects
**adapter_kwargs
File "F:\Python2.7.15\lib\site-packages\requests\sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "F:\Python2.7.15\lib\site-packages\requests\adapters.py", line 513, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='github-production-release-asset-2e65be.s3.amazonaws.com', port=443): Max retries exceeded with url: /53296967/04557cd4-6a6f-11e8-8756-b86d54a7b2c7?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20180809%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180809T142705Z&X-Amz-Expires=300&X-Amz-Signature=158a23651683c57e96afcd3f463272859a66e400c0bb63f473516c9dcaae2715&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dapp-uiautomator.apk&response-content-type=application%2Fvnd.android.package-archive (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 10060] ',))

python -m uiautomator2 init --mirror 试试这个

dengehello 回复

使用荣耀9 atx-agent总是装不上,换oppo的就好了

ATX -uiautomator2如何让脚本并行执行?谢谢

我的测试机是oppo:
按照以上步骤执行出现了如下问题,请大佬帮忙指点:

LiFuQingGitHab 回复

oppo貌似有的手机init之后 一拔掉数据线 u2就GG了 你重新init之后 再试试

15Floor has been deleted

macos init失败

linpengcheng 回复

你好 我也遇到这种情况.
华为p20 8.1系统,init后拔出USB,U2就GG了. 只有一直插着USB才可以. 你有找到解决方法么?
我试过5.1 6.X 7.X 系统的都没有这种问题.

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up