相信很多使用appium做过APP自动化的同学都深有感触:
1、appium运行慢、时间长
2、uiautomatorviewer定位元素时得关掉appium server
3、在低版本的appium上获取toast需要切换automationName
现在有一款自动化测试工具 uiautomator2,它几乎完美的避免了以上的问题。简单易学,对新手足够友好。本次我们介绍一下这款工具;
2-1 u2自动化抓取工具介绍
uiautomator2是uiautomator的升级版,uiautomator是Google开发的一款用来做安卓自动化测试的Java库,uiautomator2则是将uiautomator中的接口封装成了Python库。因此它支持的语言为Python
1. 辅助功能
Google 官方提供了一个 Android 自动化测试工具(Java 库),基于 Accessibility 服务,功能很强,可以对第三方 App 进行测试,获取屏幕上任意一个 App 的任意一个控件属性,并对其进行任意操作,但有两个缺点:
1.测试脚本只能使用 Java 语言;
2.测试脚本要打包成 jar 或者 apk 包上传到设备上才能运行;
实际工作中,我们希望测试逻辑能够用 Python 编写,能够在电脑上运行的时候就控制手机。所以基于这个目的开发了 python-uiautomator2 自动化测试开源工具,其封装了谷歌自带的 uiautomator2 测试框架,可以运行在支持 Python 的任一系统上,目前版本为 V2.10.2。
uiutomator2有很多优点,主要概括如下:
1.环境搭建简单方便
2.执行速度很快,运行稳定
3.UI控件识别有专业工具(weditor),UI元素定位便捷好用、可视化好
4.获取toast非常简单
5.UI自动化编写采用python, 学习成本低;
6.文档是中文的;
GitHub 开源地址:https://github.com/openatx/uiautomator2
2. 软件版本要求
根据官方文档,要求:
Andorid版本 4.4+
Python版本 3.6+(3.8不支持)
开发环境
开发环境:windows10
运行环境:ubuntu18.04(提供虚拟机文件),不要使用virtualbox(网卡冲突)
虚拟机:VM15 pro
SSH: SecureCrt Version 7.0.0
IDE: PyCharm 2019.3.2
安卓模拟器:夜神模拟器V6.6.0
Python : Python3.6.9
软件库
pip install -U weditor
pip install -U uiautomator2
2-2 u2通信流程介绍
1. 工作原理
如图所示,python-uiautomator2 主要分为两个部分,python 客户端,移动设备
python 端: 运行脚本,并向移动设备发送 HTTP 请求;
移动设备:移动设备上运行了封装了 uiautomator2 的 HTTP 服务,解析收到的请求,并转化成 uiautomator2 的代码;
整个过程:
1.在移动设备上安装 atx-agent(守护进程),随后 atx-agent 启动 uiautomator2 服务(默认 7912 端口)进行监听;
2.在 PC 上编写测试脚本并执行(相当于发送 HTTP 请求到移动设备的 server 端);
3.移动设备通过 WIFI 或 USB 接收到 PC 上发来的 HTTP 请求,执行制定的操作;
2-3 u2自动化抓取开发 VM Ubuntu 搭建-1
sudo su # 进入root
passwd 123456
VMware Tools安装
- 【Tools】Ubuntu20.04安装VMware Tools详解
https://blog.csdn.net/dengjin20104042056/article/details/106396644/
注意:安装一路yes,中间有no
Ubuntu16.04修改静态ip地址
2-4 u2自动化抓取开发环境搭建-2
1. virtualenv安装与配置
pip3 install virtualenv
pip3 install virtualenv -i https://pypi.douban.com/simple/ # 安装源安装
pip3 -m virtualenv venv # 创建环境
pip freeze # 已安装
2. conda安装与配置
https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/
conda env list
Linux下虚拟环境配置
nano ~/.condarc
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
show_channel_urls: true
3. SSH 连接 Pycharm
选择同步目录
# 测试
print('www.imooc.com')
#----------------------------------------
ssh://[email protected]:22/home/user1/enter/bin/python -u /home/user1/u2_project/test.py
www.imooc.com
2-5 u2自动化抓取开发环境搭建-adb
先安装夜神模拟器;
添加adb系统变量
1. 添加adb 环境变量
注意 :1. 分号;2. 重新打开cmd窗口,否则无效;
# 查看连接设备
C:\Users\dell>adb devices
List of devices attached
C:\Users\dell>adb kill-server
C:\Users\dell>adb devices
List of devices attached
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
C:\Users\dell>adb devices
List of devices attached
2. 连接设备
打开夜神模拟器
$ adb devices
List of devices attached
127.0.0.1:62001 device
注意:
显示没有连接设备:
-
推荐使用的手机
-
打开开发者模式
打开usb调试
3. 安装APK
推荐使用豌豆荚,因为它可以找到很多APP的历史版本;
# 安装考研帮
$ adb install kaoyanbang_3.5.6.272.apk
# 查看安装包;
$ adb shell pm list packages
package:com.android.providers.telephony
package:com.android.providers.calendar
package:com.android.providers.media
package:com.android.wallpapercropper
package:com.android.documentsui
package:com.android.externalstorage
package:com.android.htmlviewer
package:com.android.mms.service
package:com.android.providers.downloads
package:com.android.browser
package:com.tal.kaoyan
2-6 u2自动化抓取开发环境搭建Linux安装adb
Linux下采用IP的连接方式;
WIN10下采用USB的连接方式;
1. Linux下安装adb
(base) user1@imooc:~/test$ sudo cp adb /usr/local/sbin/
(base) user1@imooc:~/test$ ls /usr/local/sbin/
adb
(base) user1@imooc:~/test$ sudo cp fastboot /usr/local/sbin/
(base) user1@imooc:~/test$ ls
adb fastboot platform-tools_r29.0.6-linux.zip
(base) user1@imooc:~/test$ sudo unzip -d /usr/local/sbin/ platform-tools_r29.0.6-linux.zip
# 加可执行权限
(base) user1@imooc:~/test$ sudo chmod +x /usr/local/sbin/platform-tools/adb /usr/local/sbin/adb
(base) user1@imooc:~/test$ sudo chmod +x /usr/local/sbin/platform-tools/fastboot /usr/local/sbin/fastboot
# 添加环境变量;
ser1@imooc:~/test$ sudo nano /etc/profile
export PATH=/usr/local/sbin;$PATH
(base) user1@imooc:~/test$ adb
Android Debug Bridge version 1.0.41
Version 29.0.6-6198805
Installed as /usr/local/sbin/platform-tools/adb
2. ubuntu虚拟机中连接夜神模拟器
先win下改变为TCP连接方式
$ adb tcpip 5555
restarting in TCP mode port: 5555
这个IP是通过NAT之后的IP,不可以连接的;
修改桥接网络后:手机IP与Linux系统的IP(192.168.0.103)处于同一个网段上
# linux下连接adb
(base) user1@imooc:~/test$ adb devices
* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached
(base) user1@imooc:~/test$ adb connect 192.168.0.101
connected to 192.168.0.101:5555
(base) user1@imooc:~/test$ adb devices
List of devices attached
192.168.0.101:5555 device
# 安装APK
2-7 u2自动化抓取开发环境知识点 回顾
1. SSH连接与SCureCrt
2. 虚拟环境 virtualenv
3. pytharm 设置linux开发环境Python3.6.9
4. adb在linux和win下的安装
2-8 如何连接真实移动设备并安装apk-windows环境-1
1.连接真实手机设备
$ adb devices
List of devices attached
98895a353059414c5a unauthorized # 未认证设备
emulator-5584 host
127.0.0.1:62001 device
C:\Users\dell\Desktop
$ adb kill-server
C:\Users\dell\Desktop
$ adb devices
List of devices attached
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
98895a353059414c5a unauthorized
# 未认证 重新设置;
C:\Users\dell\Desktop
$ adb devices
List of devices attached
127.0.0.1:62001 device
98895a353059414c5a device #正常连接设备;
2-9 如何连接真实移动设备并安装apk-windows环境-安装APK
1. 真实手机安装APK
$ adb devices
List of devices attached
98895a353059414c5a device
C:\Users\dell\Desktop
$ adb install F:\BaiduNetdiskDownload\APP爬虫实战课程\2020版\apk\考研帮\kaoyanbang_3.5.6.272.apk
Success
$ adb uninstall com.tal.kaoyan # 卸载 ;
Success
2-10 如何连接真实移动设备并安装apk-ubuntu环境
1. 如何将手机连接到linux系统中并识别呢?**
-
将手机插到win10电脑上;
- 打开USB调试,开发者模式;允许连接
user1@imooc:~$ adb devices
List of devices attached
98895a353059414c5a device
user1@imooc:~$ adb install data/kaoyanbang_3.5.6.272.apk
user1@imooc:~$ adb uninstall com.tal/kaoyan
注意问题:
- Performing Streamed Install 卡住
2-11 u2项目初始化
1.linux开发环境中安装包
# 安装
(venv) user1@imooc:~$ pip install uiautomator2 -i https://pypi.douban.com/simple/
# 初始化
(venv) user1@imooc:~$ python -m uiautomator2 init
[I 201209 22:15:10 init:155] uiautomator2 version: 2.12.0
[I 201209 22:15:10 init:352] Install minicap, minitouch
[D 201209 22:15:10 init:62] Download https://tool.appetizer.io/openatx/stf-binaries/raw/0.2.2/node_modules/minitouch-prebuilt-beta/prebuilt/x86/bin/minitouch
minitouch |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 33.6 KB/33.6 KB
[D 201209 22:15:26 init:232] Push to /data/local/tmp/minitouch:0755
[I 201209 22:15:26 init:356] abi:x86 seems to be android emulator, skip install minicap
[D 201209 22:15:26 init:250] apk-debug package-info: None
[D 201209 22:15:26 init:251] apk-debug-test package-info: None
[I 201209 22:15:26 init:367] Install com.github.uiautomator, com.github.uiautomator.test 2.3.1
[D 201209 22:15:26 init:162] Shell: ('pm', 'uninstall', 'com.github.uiautomator')
[D 201209 22:15:26 init:162] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test')
[D 201209 22:15:26 init:62] Download https://tool.appetizer.io/openatx/android-uiautomator-server/releases/download/2.3.1/app-uiautomator.apk
app-uiautomator.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 2.1 MB/2.1 MB
[D 201209 22:15:28 init:232] Push to /data/local/tmp/app-uiautomator.apk:0644
[D 201209 22:15:28 init:162] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk')
[I 201209 22:15:29 init:332] - app-uiautomator.apk installed
[D 201209 22:15:29 init:62] Download https://tool.appetizer.io/openatx/android-uiautomator-server/releases/download/2.3.1/app-uiautomator-test.apk
[D 201209 22:15:39 init:111] download error from mirror(HTTPSConnectionPool(host='tool.appetizer.io', port=443): Max retries exceeded with url: /openatx/android-uiautomator-server/releases/download/2.3.1/app-uiautomator-test.apk (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))), use origin source
[D 201209 22:15:39 init:62] Download https://github.com/openatx/android-uiautomator-server/releases/download/2.3.1/app-uiautomator-test.apk
app-uiautomator-test.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 1.2 MB/1.2 MB
[D 201209 22:15:58 init:232] Push to /data/local/tmp/app-uiautomator-test.apk:0644
[D 201209 22:15:59 init:162] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk')
[I 201209 22:15:59 init:332] - app-uiautomator-test.apk installed
[I 201209 22:15:59 init:340] Install atx-agent 0.9.5
[D 201209 22:15:59 init:62] Download https://tool.appetizer.io/openatx/atx-agent/releases/download/0.9.5/atx-agent_0.9.5_linux_386.tar.gz
atx-agent_0.9.5_linux_386.tar.gz |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 4.2 MB/4.2 MB
[D 201209 22:16:05 init:232] Push to /data/local/tmp/atx-agent:0755
[D 201209 22:16:09 init:162] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop')
[D 201209 22:16:09 init:162] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d')
[D 201209 22:16:09 init:162] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d')
[I 201209 22:16:09 init:377] Check atx-agent version
[D 201209 22:16:09 init:390] Forward: local:tcp:46937 -> remote:tcp:7912
[D 201209 22:16:10 init:393] atx-agent version 0.9.5
Successfully init AdbDevice(serial=192.168.0.102:5555)
自动安装ATX
安装ATX之后,就可以通过代码获取手机的信息了
注意:ATX uiautomator2 init 失败及其他问题的解决办法
5个原生的包
(venv) user1@imooc:~$ ls -a
. .android .bash_logout .bashrc-miniconda3.bak .conda .config .gnupg .local .mozilla .pycharm_helpers .sudo_as_admin_successful .uiautomator2 公共的 视频 文档 音乐
.. .bash_history .bashrc .cache .condarc data .ICEauthority miniconda3 .profile .python_history u2_project .viminfo 模板 图片 下载 桌面
(venv) user1@imooc:~$ cd .uiautomator2/
(venv) user1@imooc:~/.uiautomator2$ cd cache/
(venv) user1@imooc:~/.uiautomator2/cache$ ll
总用量 24
drwxrwxr-x 6 user1 user1 4096 12月 9 22:15 ./
drwxrwxr-x 3 user1 user1 4096 12月 9 22:15 ../
drwxrwxr-x 2 user1 user1 4096 12月 9 22:15 app-uiautomator.apk-81d5349503/
drwxrwxr-x 2 user1 user1 4096 12月 9 22:15 app-uiautomator-test.apk-a0b8958cc1/
drwxrwxr-x 2 user1 user1 4096 12月 9 22:16 atx-agent_0.9.5_linux_386.tar.gz-d3bccbc602/
drwxrwxr-x 2 user1 user1 4096 12月 9 22:15 minitouch-7b41fbda87/
2-12 u2连接移动设备的三种方式-1
- 通过wifi连接
import uiautomator2 as u2
# d = u2.connect("192.168.0.101:5555")
d = u2.connect_wifi("192.168.0.102")
print(d.info)
# ----------------------------------------------------------
sudo+ssh://[email protected]:22/home/user1/.conda/envs/venv/bin/python -u /home/user1/u2_project/test.py
[D 201209 22:23:44 __init__:600] kill process(ps): uiautomator
[D 201209 22:23:45 __init__:619] uiautomator-v2 is starting ... left: 40.0s
[D 201209 22:23:46 __init__:619] uiautomator-v2 is starting ... left: 39.0s
[D 201209 22:23:47 __init__:619] uiautomator-v2 is starting ... left: 38.0s
[D 201209 22:23:48 __init__:619] uiautomator-v2 is starting ... left: 37.0s
[D 201209 22:23:49 __init__:619] uiautomator-v2 is starting ... left: 36.0s
[D 201209 22:23:51 __init__:619] uiautomator-v2 is starting ... left: 35.0s
[D 201209 22:23:52 __init__:619] uiautomator-v2 is starting ... left: 34.0s
[I 201209 22:23:52 __init__:583] uiautomator back to normal
{'currentPackageName': 'com.android.settings', 'displayHeight': 1920, 'displayRotation': 0, 'displaySizeDpX': 540, 'displaySizeDpY': 960, 'displayWidth': 1080, 'productName': 'R11 Plus', 'screenOn': True, 'sdkInt': 22, 'naturalOrientation': True}
2. USB连接:手机序列号
d = u2.connect_usb("4b5f05af7")
print(d.info)
# List of devices attached
# 4b5f05af7 device
3. 通过adb_wifi
(venv) user1@imooc:~/.uiautomator2/cache$ adb devices
List of devices attached
192.168.0.102:5555 device
import uiautomator2 as u2
d = u2.connect_adb_wifi("192.168.0.102:5555")
print(d.info)
#------------------------------------------------------
already connected to 192.168.0.102:5555
{'currentPackageName': 'com.android.settings', 'displayHeight': 1920, 'displayRotation': 0, 'displaySizeDpX': 540, 'displaySizeDpY': 960, 'displayWidth': 1080, 'productName': 'R11 Plus', 'screenOn': True, 'sdkInt': 22, 'naturalOrientation': True}
print(d.device_info) # 设备详细信息
#------------------------------------------------------
{'udid': 'android-08:00:27:e9:1b:52-AOSP_on_Shama', 'version': '5.1.1', 'serial': 'android', 'brand': 'Android', 'model': 'AOSP on Shama', 'hwaddr': '08:00:27:e9:1b:52', 'port': 7912, 'sdk': 22, 'agentVersion': '0.9.5', 'display': {'width': 1080, 'height': 1920}, 'battery': {'acPowered': False, 'usbPowered': True, 'wirelessPowered': False, 'status': 2, 'health': 2, 'present': True, 'level': 85, 'scale': 100, 'voltage': 3, 'temperature': 26, 'technology': 'Li-ion'}, 'memory': {'total': 3110960, 'around': '3 GB'}, 'arch': '', 'owner': None, 'presenceChangedAt': '0001-01-01T00:00:00Z', 'usingBeganAt': '0001-01-01T00:00:00Z', 'product': None, 'provider': None}
4. 其他命令
- 启动APP
aapt获取包名
# 获取包名
$ aapt dump badging F:\BaiduNetdiskDownload\APP爬虫实战课程\2020版\apk\抖音\aweme_aweGW_v9.6.0_7694899.apk
package: name='com.ss.android.ugc.aweme' versionCode='960' versionName='9.6.0'
sdkVersion:'16'
targetSdkVersion:'26'
import uiautomator2 as u2
import time
# d = u2.connect_wifi("192.168.0.102")
# print(d.info)
d = u2.connect_adb_wifi("192.168.0.102:5555")
print(d.device_info)
# aapt 获取包名的时候,在aapt dump badging xxx.apk 获取到的是包名;
d.app_start("com.ss.android.ugc.aweme")
time.sleep(5)
d.app_stop("com.ss.android.ugc.aweme")
ubuntu安装aapt
sudo apt-get install aapt