Android测试自动化(篇一):UiAutomator工具的安装

一、UiAutomator介绍

uiautomator2是一个自动化测试开源工具,仅支持android平台的自动化测试,其封装了谷歌自带的uiautomator2测试框架,可以运行在支持Python的任一系统上。

要使用该工具,需要满足如下条件:

  • Android SDK Tools, Revision 21 or higher

  • Android SDK Platform, API 18 or higher

开源地址:

  • https://github.com/openatx/uiautomator2

二、工作原理

引用自:

  • https://testerhome.com/topics/11357

67ee85df-47dd-4629-abb1-064e8354a026.png

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

  • python 端: 运行脚本,并向移动设备发送 HTTP 请求

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

整个过程:

  1. 在移动设备上安装atx-agent(守护进程), 随后atx-agent启动 uiautomator2 服务 (默认 7912 端口) 进行监听

  2. 在 PC 上编写测试脚本并执行(相当于发送 HTTP 请求到移动设备的 server 端)

  3. 移动设备通过 WIFI 或 USB 接收到 PC 上发来的 HTTP 请求,执行制定的操作

三、安装工具

  • Python2或Python3都可以

  • Android模拟器或Android真机

3.1搭建环境

  • 1)安装adb

从谷歌官网下载Platform Tools,解压下载好的压缩包,并加包含adb.exe的目录加入到系统变量的 PATH 变量中。(选中PATH变量后,点击新建。如果不小心删除了,不要点确认!取消即可,直到退出系统属性窗口

查看安装是否成功

adb version

输出的结果如下,则环境配置成功,安装的目录会不一样。

Android Debug Bridge version 1.0.41Version 33.0.2-8557947
Installed as D:\Sdk\Android\platform-tools\adb.exe
  • 2)安装Python

Python官网下载,在这里笔者选择的是Windows x86-64 executable installer,打开安装包进行安装。

如果没有添加到环境变量中,需要手动配置环境变量。

将Python的安装目录(如:D:\Programs\Python\Python38)添加到系统变量的 PATH 变量中。在这里需要用到pip,需要将Python的安装目录\Scripts(如:D:\Programs\Python\Python38\Scripts)添加到系统变量的 PATH 变量中。

查看安装是否成功

python

输出的结果如下,则环境配置成功,安装的版本会不一样。

Python 3.8.5 (tags/v3.8.5:580fbb0,Jul 20 2020,15:57:54)[MSC v.1924 64 bit (AMD64)] on win32
Type "help","copyright","credits" or "license" for more information.>>> exit(0)
  • 3)安装uiautomator2

文档:openatx/uiautomator2: Android Uiautomator2 Python Wrapper (github.com)

#Since uiautomator2 is still under development, you have to add --pre to install the development version
pip install --upgrade --pre uiautomator2
#Or you can install directly from github source
git clone  
pip install -e uiautomator2

在安装包时,笔者遇到的问题及解决方法:

  • WARNING: Ignoring invalid distribution

WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)

解决方法:

找到警告信息中报错的目录,然后删掉~开头的文件夹,那种事之前安装插件失败/中途退出,导致插件安装出现异常导致的,虽说警告信息不影响,但是有强迫症,把文件夹删掉就好了。


查看安装是否成功:

uiautomator2 -h

输出的结果如下,则安装成功。

usage: uiautomator2 [-h] [-d] [-s SERIAL]
                    {version,init,screenshot,identify,install,uninstall,healthcheck,check,start,stop,current,doctor,console,purge}
                    ...

positional arguments:
  {version,init,screenshot,identify,install,uninstall,healthcheck,check,start,stop,current,doctor,console,purge}
    version             show version
    init                install enssential resources to device
    screenshot          take device screenshot
    identify            quickly find your device by change device screen color
    install             install packages
    uninstall           uninstall packages
    healthcheck         recover uiautomator service
    check               alias of healthcheck
    start               start application
    stop                stop application
    current             show current application
    doctor              detect connect problem
    console             launch interactive python console
    purge               remove minitouch, minicap, atx app etc, from device
  • 4)安装WEditor在线元素查看器

weditor是一款基于浏览器的UI查看器,用来帮助我们查看UI元素定位。

因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,就需要用到此工具。

pip install -U weditor

查看安装是否成功:

weditor -h

输出的结果如下,则安装成功。

usage: weditor [-h] [-v] [-q] [-p PORT] [-f] [--debug] [--shortcut] [--quit]

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show version (default: False)
  -q, --quiet           quite mode, no open new browser (default: False)
  -p PORT, --port PORT  local listen port for weditor (default: 17310)
  -f, --force-quit      force quit before start (default: False)
  --debug               open debug mode (default: False)
  --shortcut            create shortcut in desktop (default: False)
  --quit                stop weditor (default: False)

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

在安装包时,笔者遇到的问题及解决方法:

  • subprocess-exited-with-error

解决方法:

git clone https://github.com/openatx/weditorpip3 install -e weditor
  • 出现'git' 不是内部或外部命令,也不是可运行的程序

Ⅰ.从git官网下载对应的版本:官网下载链接

Ⅱ.一般使用默认设置即可:一路next,git安装完毕!

Ⅲ.但是如果这时你打开windows的cmd,在里面打git命令会提示“不是内部或外部命令,也不是可运行的程序”,想要直接在windows的cmd里使用git命令要多加如下两步

找到git安装路径中bin的位置,如:D:\Program Files\Git\bin,找到git安装路径中git-core的位置,如:D:\Program Files\Git\libexec\git-core;

这里的D:\Program Files\Git需要改成自己的安装目录

右键“计算机”->“属性”->“高级系统设置”->“环境变量”->在下方的“系统变量”中找到“path”->选中“path”并选择“编辑”->将找到的bin和git-core路径复制到其中->保存并退出

四、使用工具

  • 1)打开Android模拟器,在这里笔者使用的是MuMu模拟器。打开模拟器的USB调试,找到模拟器的安装目录D:\Program Files\MuMu\emulator\nemu\vmonitor\bin,在cmd中切换到这个目录,输入adb connect 127.0.0.1:7555

  • 2)运行python3 -m uiautomator2 init安装包含httprpc服务的apk到手机

  • 3)打开在桌面成功创建的快捷方式WEditor,之后浏览器会自动打开一个网页http://localhost:17310/

  • 4)浏览器截图如下图所示。

QQ截图20220706105624.png

#转载请注明出处!
快来制作你的简历吧 ,请猛戳这里→点击在线制作
宝塔服务器面板,一键全能部署及管理,送你3188元礼包。请猛戳这里→点我领取

本文标题:《Android测试自动化(篇一):UiAutomator工具的安装》作者:xuanzhe
原文链接:https://blog.xuanzhe.club/?id=63
特别注明外均为原创,转载请注明。

分享到微信

扫描二维码

可在微信查看或分享至朋友圈。

相关文章

发表评论:
验证码

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

你好,朋友

真是美好的一天!

访客信息

  • IP: 3.128.203.143
  • 地点: United StatesOhioDublin

标签列表

站点信息

  • 文章总数:69
  • 页面总数:2
  • 分类总数:19
  • 标签总数:34
  • 评论总数:7
  • 浏览总数:147010
您好,欢迎到访网站!
忘记密码

网站分类

文章归档

歌曲 - 歌手
0:00