首页 > 编程知识 正文

uiautomator2从零,android studio编写登录界面

时间:2023-05-05 15:15:07 阅读:129950 作者:777

在此安装目录标题1.uiautomator22。 ADB连接手机2.1atx-agent2.2连接手机2.2.1 usb连接手机2.2.2 wifi连接3. weditor环境准备3.1 weditor连接手机3.2页面要素把持3.2.1定位要素3.2

之前使用的python appium做了安卓自动化测试,最近看了大人物的博客,找到了另一个神器。 我来简单介绍一下:

转载地址: https://MP.weixin.QQ.com/s/tw hk S1 EOR lt 4u a 26 he m8aa

uiautomator2的api文档: https://www.cn blogs.com/jec 1999/p/8976960.html

uiautomator是谷歌提供的用于安卓自动化测试的Java库。 这是一个强大的功能,可以测试第三方APP,获取屏幕上任何APP的任何控制属性,并执行任何操作,但有两个缺点。

测试脚本只能用Java语言运行。 每次测试脚本都必须上传到设备上运行。 我们想在测试可以在Python上制作,可以在电脑上运行的时候控制手机,有python-uiautomator,然后有python-uiautomator2。 有关详细信息,请访问github地址https://github.com/open atx/uiautor

1 .安装uiautomator2使用pip3安装uiautomator2。 安装过程中出现了一点小孔,pip install--pre- uu iautomator 2一直安装失败,我找了很多教程,就是这个。

最后,我在github上查找教程3359 github.com/open atx/uiautomator 2,发现它是用pip3安装的。 抱着试试的心情,需cd到python的Scripts目录,再使用pip3安装

e :python 36script spip 3安装- uu iautomator 2

2. adb连接智能手机,使智能手机和电脑连接,使用adb devices查看链接状态。 虽然adb相关的环境本篇鲜有讲述,但做安卓测试的人都知道

e :python 36scriptsadbdeviceslistofdevicesattachedadbserversion (31 ) doesn'tmatchthisclient ) 40; killing . *安装daemonstartedsuccessfullyemulator-5554 device 2.1 atx-agent python-mu iautomator2init

e :python 36scripts python-mu iautomator2init [ d 1907162336034336053 _ _ main _ :28 ] _ secachedirectory. subparser='init ' ) init [ I 1907162336034336053 _ _ main _ :88 ] initialdeviceadbdevice (serial=emulator-() emulator-() ) ) 652 [ I 1907162336034336053 _ _ main _ _ 333333333653 ] mini touch [ kminitouch|# # # # # # # # ] 25h [ d 1907162336034336056 _ _ main _ _ :157 ] push 33https://github.com/oper local/tmp/mini touch :0755 [ I 1907162336034336056 _ _ main _ :176 ] ABI : x 8860 sipinstalllminicap [ I 1907162333336 installcom.github.uiautomator,com.github.uiautomator.test [ d 1907162336034336056 _ _ main _ 3333653360100 ] shel 33333333333333656 ' com.github.uiautomator ' ) [ d 1907162336034336056 _ _ main _ :100 ] shell : (pm )、(uninstall )和。

/ 1.7M.7M[?25h[D 190716 23:35:05 __main__:157] Push https://github.com/openatx/android-uiautomator-server/releases/download/1.1.7/app-uiautomator.apk -> /data/local/tmp/app-uiautomator.apk:0644[D 190716 23:35:05 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk')[Kapp-uiautomator-test.apk |################################| 966.8K / 966.8K[?25h[D 190716 23:35:11 __main__:157] Push https://github.com/openatx/android-uiautomator-server/releases/download/1.1.7/app-uiautomator-test.apk -> /data/local/tmp/app-uiautomator-test.apk:0644[D 190716 23:35:11 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk')[I 190716 23:35:18 __main__:193] Install atx-agent[Katx-agent_0.6.0_linux_386.tar.gz |################################| 4.0M / 4.0M.0M[?25h[D 190716 23:35:23 __main__:157] Push https://github.com/openatx/atx-agent/releases/download/0.6.0/atx-agent_0.6.0_linux_386.tar.gz -> /data/local/tmp/atx-agent:0755[D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop')[D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '-d')[I 190716 23:35:26 __main__:202] Check install[D 190716 23:35:26 __main__:212] Forward: local:tcp:53461 -> remote:tcp:7912[D 190716 23:35:28 __main__:214] atx-agent version 0.6.0Successfully init AdbDevice(serial=emulator-5554)

看到Successfully就是安装成功了(当然总是有几个小伙伴的手机需要换下了!!!)

2.2 连接手机

python-uiautomator2连接手机的方式有两种,一种是通过WIFI,另外一种是通过USB

首先查看uiautomator状态是启动状态

2.2.1 usb连接手机

先使用adb devices获取手机的唯一识别序列号,也就是devices前面的那一串:emulator-5554

E:python36Scripts>adb devices
List of devices attached
emulator-5554 device

测试是否连接成功,先打开你的手机上的任意app,比如淘宝,cmd输入以下信息

E:python36Scripts>pythonPython 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import uiautomator2 as u2>>> d = u2.connect_usb('emulator-5554')>>> d.info{'currentPackageName': 'com.taobao.taobao', 'displayHeight': 1280, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 640, 'displayWidth': 720, 'productName': 'h60-l01', 'screenOn': True, 'sdkInt': 22, 'naturalOrientation': True}>>>

看到输出’currentPackageName’: ‘com.taobao.taobao’说明连接成功了

2.2.2 wifi连接

首先保证手机和电脑在一个局域网,可以先ping 手机ip,看能不能ping的通

>>> import uiautomator2 as u2>>> d = u2.connect('192.168.1.xx')>>> d.info 3. weditor环境准备

使用pip安装weditor,github地址https://github.com/openatx/weditor

pip install —pre weditor

创建桌面快捷图标

python -m weditor —shortcut

创建完成后,桌面会出现一个WEditor快捷图标,双击就能启动了

也可以不创建图标,用命令行启动

python -m weditor

看到窗口出现如下信息,就是启动成功了(窗口别关掉了)

C:Usersdell>python -m weditorlistening on http://192.168.1.125:17310[I 190717 23:50:55 web:2246] 304 GET / (::1) 7.98ms[I 190717 23:50:55 web:2246] 304 GET /static/libs/css/buttons.css (::1) 196.47ms[I 190717 23:50:55 web:2246] 304 GET /static/loading.svg (::1) 5.99ms[I 190717 23:50:55 web:2246] 304 GET /static/ace/ace.js (::1) 12.97ms[I 190717 23:50:55 web:2246] 304 GET /static/running.svg (::1) 13.96ms[I 190717 23:50:55 web:2246] 304 GET /static/ace/mode-python.js (::1) 19.96ms[I 190717 23:50:55 web:2246] 304 GET /static/libs/fontawesome/css/font-awesome.min.css (::1) 1.99ms[I 190717 23:50:59 web:2246] 304 GET /api/v1/version (::1) 0.00ms 3.1 weditor连接手机

先使用adb连上手机,输入adb devices查看手机设备号:emulator-5554

C:Usersdell>adb devicesList of devices attachedadb server version (31) doesn't match this client (40); killing...* daemon started successfullyemulator-5554 device

在页面上输入手机设备号,点Connet按钮,看到出现一个小草的图标,就是连接成功了

3.2 页面元素抓取 3.2.1 定位元素

定位元素的方法支持uiautomator的定位方法,也可以支持xpath和坐标定位

1.可以查看元素的resourceId属性2.自动生成xpath定位方法3.支持坐标点击,根据屏幕的比例4.自动生成uiautomator定位方法参考代码5.可以勾选强制使用xpath

3.2.2 在线Coding调试

可以在页面的右边调试运行代码,这点比appium强很多,可以编写代码边调试,还是非常方便的

操作步骤:点勾选高铁动车后,点查询按钮

d(resourceId="com.yipiao:id/checkGaotie").click()d.xpath('//*[@resource-id="com.yipiao:id/layQueryCard"]/android.widget.FrameLayout[1]').click()

Ctr+Enter运行代码,会发现手机上已经点击成功

3.3 支持wifi连接

首先保证电脑和手机wifi在一个局域网,电脑上能ping的通手机的ip,输入手机的ip也可以抓到手机上的页面元素

看完上面的文章再加上文章开头的api文档相信基本使用应该没问题了,下面呢,我写一个用uiautomator2去支付宝收集能量的一个脚本哈:

脚本如下:

# coding:utf-8import uiautomator2 as u2d = u2.connect_usb('740dc3d1')print('打开支付宝')d.app_start('com.eg.android.AlipayGphone')print('点击蚂蚁森林')d.xpath('//*[@text="蚂蚁森林"]').click()n = d(descriptionContains='收集').countfor i in range(n): d.xpath('//*[contains(@content-desc,"收集能量")]').click() print('收集能量成功')print('收集能量结束!')

怎么样?只要将这个脚本设置未定时任务就可以自己每天定时去收取能量了,这样你的小树才会长得快呀!

总体来说呢,uiautomator2比appium安装搭建环境要简单一点,代码呢也十分简洁!其次就是这个weditor 也比appium查看页面元素要方便,反应快,可实时调代码这是关键!期待后面的自动化项目可以用uiautomaor2 来试试手,届时会继续更新uiautomator2的详细使用,及踩坑的记录!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。