uiautomator2 + weditor

之前呢一直用的python+appium做安卓自动化测试,最近看大佬的博客,发现了又一个神器,简单介绍一下哈:

转载地址:https://mp.weixin.qq.com/s/TWhKS1EORLT4Ua26heM8aA

uiautomator2 的api文档:https://www.cnblogs.com/jec1999/p/8976960.html

iautomator是Google提供的用来做安卓自动化测试的一个Java库。功能很强,可以对第三方App进行测试,获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点:

  1. 测试脚本只能使用Java语言
  2. 测试脚本必须每次被上传到设备上运行。

我们希望测试能够用Python编写,能够在电脑上运行的时候就控制手机,于是就有了python-uiautomator,后来又有了python-uiautomator2,详情查看github地址https://github.com/openatx/uiautomator2

1.安装uiautomator2

使用pip3安装uiautomator2 ,安装的过程中有点坑,用pip install --pre -U uiautomator2一直安装失败,找了很多教程都是这个。
最后去github上找了下教程https://github.com/openatx/uiautomator2,发现用pip3安装,抱着试试的心态,安装成功了
需cd到python的Scripts目录,再使用pip3安装

E:python36Scripts>pip3 install -U uiautomator2

2.adb连上手机

确保手机和电脑能连得上,使用adb devices查看链接状态,adb相关的环境本篇就不多说了,搞android测试的都知道

  1.  
    E:python36Scripts>adb devices
  2.  
    List of devices attached
  3.  
    adb server version (31) doesn't match this client (40); killing...
  4.  
    * daemon started successfully
  5.  
    emulator-5554 device 

安装atx-agent

python -m uiautomator2 init

  1.  
    E:python36Scripts>python -m uiautomator2 init
  2.  
    [D 190716 23:34:53 __main__:28] use cache directory: C:Usersdell.uiautomator2
  3.  
    Namespace(serial=None, server=None, subparser='init') init
  4.  
    [I 190716 23:34:53 __main__:88] >>> Initial device AdbDevice(serial=emulator-5554)
  5.  
    [I 190716 23:34:53 __main__:172] Install minicap, minitouch
  6.  
    [Kminitouch |################################| 33.5K / 33.5K
  7.  
    [?25h[D 190716 23:34:56 __main__:157] Push https://github.com/openatx/stf-binaries/raw/master/node_modules/minitouch-prebuilt/prebuilt/x86/bin/minitouch -> /data/local/tmp/minitouch:0755
  8.  
    [I 190716 23:34:56 __main__:176] abi:x86 seems to be android emulator, skip install minicap
  9.  
    [I 190716 23:34:56 __main__:182] Install com.github.uiautomator, com.github.uiautomator.test
  10.  
    [D 190716 23:34:56 __main__:100] Shell: ('pm', 'uninstall', 'com.github.uiautomator')
  11.  
    [D 190716 23:34:56 __main__:100] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test')
  12.  
    [Kapp-uiautomator.apk |################################| 1.7M / 1.7M.7M
  13.  
    [?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
  14.  
    [D 190716 23:35:05 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk')
  15.  
    [Kapp-uiautomator-test.apk |################################| 966.8K / 966.8K
  16.  
    [?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
  17.  
    [D 190716 23:35:11 __main__:100] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk')
  18.  
    [I 190716 23:35:18 __main__:193] Install atx-agent
  19.  
    [Katx-agent_0.6.0_linux_386.tar.gz |################################| 4.0M / 4.0M.0M
  20.  
    [?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
  21.  
    [D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop')
  22.  
    [D 190716 23:35:26 __main__:100] Shell: ('/data/local/tmp/atx-agent', 'server', '-d')
  23.  
    [I 190716 23:35:26 __main__:202] Check install
  24.  
    [D 190716 23:35:26 __main__:212] Forward: local:tcp:53461 -> remote:tcp:7912
  25.  
    [D 190716 23:35:28 __main__:214] atx-agent version 0.6.0
  26.  
    Successfully init AdbDevice(serial=emulator-5554)

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

连接手机

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

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

1.usb连接手机

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

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

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

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

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

2.wifi连接

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

  1.  
    >>> import uiautomator2 as u2
  2.  
    >>> d = u2.connect('192.168.1.xx')
  3.  
    >>> d.info

weditor环境准备

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

pip install —pre weditor

创建桌面快捷图标

python -m weditor —shortcut

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

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

python -m weditor

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

  1.  
    C:Usersdell>python -m weditor
  2.  
    listening on http://192.168.1.125:17310
  3.  
    [I 190717 23:50:55 web:2246] 304 GET / (::1) 7.98ms
  4.  
    [I 190717 23:50:55 web:2246] 304 GET /static/libs/css/buttons.css (::1) 196.47ms
  5.  
    [I 190717 23:50:55 web:2246] 304 GET /static/loading.svg (::1) 5.99ms
  6.  
    [I 190717 23:50:55 web:2246] 304 GET /static/ace/ace.js (::1) 12.97ms
  7.  
    [I 190717 23:50:55 web:2246] 304 GET /static/running.svg (::1) 13.96ms
  8.  
    [I 190717 23:50:55 web:2246] 304 GET /static/ace/mode-python.js (::1) 19.96ms
  9.  
    [I 190717 23:50:55 web:2246] 304 GET /static/libs/fontawesome/css/font-awesome.min.css (::1) 1.99ms
  10.  
    [I 190717 23:50:59 web:2246] 304 GET /api/v1/version (::1) 0.00ms

weditor连接手机

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

  1.  
    C:Usersdell>adb devices
  2.  
    List of devices attached
  3.  
    adb server version (31) doesn't match this client (40); killing...
  4.  
    * daemon started successfully
  5.  
    emulator-5554 device

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

页面元素抓取

定位元素

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

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

在线Coding调试

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

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

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

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

支持wifi连接

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

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

脚本如下:

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

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

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

———————————————————————————

转载自:https://blog.csdn.net/xgh1951/article/details/97241656

原文地址:https://www.cnblogs.com/charles2018/p/13903022.html