python +uiautomator2 app测试环境搭建,摒弃appium复杂的环境依赖

1.前言关于uiautomator2:

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

工作原理

以下图片使用Windows画图软件

 

如图所示,python-uiautomator2主要分为两个部分,python客户端,移动设备

  • python端: 运行脚本,并向移动设备发送HTTP请求
  • 移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。

版本要求:

本案例以genymotion模拟器为例sdk api version 5.0 :

Requirements uiautomator2

  • Android版本 4.4+
  • Python 3.6+ (社区反馈3.8.0不支持, 但是3.8.2支持)

如果用python2的pip安装,会安装本库的老版本0.2.3;如果用python3.5的pip安装,会安装本库的老版本0.3.3;两者均已经不会再维护;PYPI上的最近版本是这个:https://pypi.org/project/uiautomator2/

一.环境搭建:

adb connect  ip:port genymotion 默认端口5555,ip为安卓机器ip)这里我以wifi连接为案例:实际usb,wifi,自己决定

python -m uiautomator2 init

QUICK START

先准备一台(不要两台)开启了开发者选项的安卓手机,连接上电脑,确保执行adb devices可以看到连接上的设备。

运行pip3 install -U uiautomator2 安装uiautomator2

运行python3 -m uiautomator2 init安装包含httprpc服务的apk到手机+atx-agent, minicap, minitouch (在过去的版本中,这一步是必须执行的,但是从1.3.0之后的版本,当运行python代码u2.connect()时就会自动推送这些文件了)

命令行运行python打开python交互窗口。然后将下面的命令输入到窗口中。

import uiautomator2 as u2

d = u2.connect() # connect to device print(d.info)

这时看到类似下面的输出,就可以正式开始用我们这个库了。因为这个库功能太多,后面还有很多的内容,需要慢慢去看 ....

{'currentPackageName': 'net.oneplus.launcher', 'displayHeight': 1920, 'displayRotation': 0, 'displaySizeDpX': 411, 'displaySizeDpY': 731, 'displayWidth': 1080, 'productName': 'OnePlus5', '
screenOn': True, 'sdkInt': 27, 'naturalOrientation': True}

二.示范代码:
import uiautomator2 as u2
"""
https://github.com/openatx/atx-agent/releases
https://github.com/openatx/android-uiautomator-server/releases
https://github.com/openatx/uiautomator2/wiki/Manual-Init
adb shell rm /data/local/tmp/minicap
adb shell rm /data/local/tmp/minicap.so
adb shell rm /data/local/tmp/minitouch
adb shell /data/local/tmp/atx-agent server --stop
adb shell rm /data/local/tmp/atx-agent
adb uninstall com.github.uiautomator
adb uninstall com.github.uiautomator.test
"""
dev=u2.connect("192.168.172.102")
print(dev.info)

调试工具:weditor:

what is weditor ?:用来抓取手机上应用的控件,坐标以及元素属性id,text,description etc;

虽然很想用Android SDK内置工具uiautomatorviewer.bat,但是运行uiautomator2的时候,uiautomatorviewer.bat运行不起来,两者之间冲突太严重。
于是参考着uiautomatorviewer的界面,我又写了一个weditor,调用python-uiautomator2的两个接口screenshotdump_hierarchy这样就不会有冲突问题了

Install weditor (UI Inspector)

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

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

pip install -U weditor

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

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

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

定位方式

  1. ResourceId定位: d(resourceId="com.smartisanos.clock:id/text_stopwatch").click()
  2. Text定位 d(text="秒表").click()
  3. Description定位 d(description="..").click()
  4. ClassName定位 d(className="android.widget.TextView").click()

xpath定位并不支持,一开始打算做支持的,但是发现不用也能搞定。就是代码写的长一点而已。

操作控件

# click
d(text="Settings").click()

# long click
d(text="Settings").long_click()

# 等待元素的出现
d(text="Settings").wait(timeout=10.0)

九宫格解锁:以前文章写过 https://testerhome.com/topics/11034

中文字符的输入
如果可以定位到元素,直接通过set_text就可以输入中文

d(text="Settings").set_text("你好")

如果定位不到元素需要使用send_keys方法,以及切换输入法

d.set_fastinput_ime(True)
d.send_keys("你好 Hello")
d.set_fastinput_ime(False) # 输入法用完关掉

截图:d.screenshot("home.jpg")
获取图层信息:xml = d.dump_hierarchy()

Connect to a device

There are two ways to connect to the device.

  1. Through WiFi

Suppose device IP is 10.0.0.1 and your PC is in the same network.

import uiautomator2 as u2

d = u2.connect('10.0.0.1') # alias for u2.connect_wifi('10.0.0.1') print(d.info)
  1. Through USB

Suppose the device serial is 123456f (seen from adb devices)

import uiautomator2 as u2

d = u2.connect('123456f') # alias for u2.connect_usb('123456f') print(d.info)
  1. Through ADB WiFi
import uiautomator2 as u2

d = u2.connect_adb_wifi("10.0.0.1:5555") # Equals to # + Shell: adb connect 10.0.0.1:5555 # + Python: u2.connect_usb("10.0.0.1:5555")

Calling u2.connect() with no argument, uiautomator2 will obtain device IP from the environment variable ANDROID_DEVICE_IP or ANDROID_SERIAL. If this environment variable is empty, uiautomator will fall back to connect_usb and you need to make sure that there is only one device connected to the computer.

Command line

其中的$device_ip代表设备的ip地址

如需指定设备需要传入--serial 如 python3 -m uiautomator2 --serial bff1234 <SubCommand>, SubCommand为子命令(init,或者screenshot等)

1.0.3 Added: python3 -m uiautomator2可以简写为uiautomator2

  • screenshot: 截图

    $ uiautomator2 screenshot screenshot.jpg
  • current: 获取当前包名和activity

    $ uiautomator2 current
    {
        "package": "com.android.browser", "activity": "com.uc.browser.InnerUCMobile", "pid": 28478 }
  • uninstall: 卸载

    $ uiautomator2 uninstall <package-name> # 卸载一个包
    $ uiautomator2 uninstall <package-name-1> <package-name-2> # 卸载多个包 $ uiautomator2 uninstall --all # 全部卸载
  • stop: 停止应用

    $ uiautomator2 stop com.example.app # 停止一个app
    $ uiautomator2 stop --all # 停止所有的app
  • install: 安装apk,apk通过URL给出 (暂时不能用)

  • healthcheck: 健康检查 (暂不能用)

还有太多太多的操作,我就不写了。更多更详细的API接口文档,直接去Github上看文档吧, 记得留下你的Starhttps://github.com/openatx/uiautomator2

有问题可以来我的技术群咨询,本文系作者原创,禁止抄袭以及商业化使用,如有转载声明作者出处,尊重技术探索:

 

猜你喜欢

转载自www.cnblogs.com/SunshineKimi/p/12565251.html