Android Studio使用uiautomator2实现自动化测试及环境变量配置
Posted LQS_Android
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio使用uiautomator2实现自动化测试及环境变量配置相关的知识,希望对你有一定的参考价值。
首先来看UIAutomator2是什么?
UiAutomator是Google提供的用来做安卓自动化测试的一个Java库,基于Accessibility服务。功能很强,可以对第三方App进行测试,获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点:1. 测试脚本只能使用Java语言 2. 测试脚本要打包成jar或者apk包上传到设备上才能运行。
GitHub:https://github.com/openatx/uiautomator2
配置uiautomator2所需环境
1.安装adb工具并配置环境变量,在android studio中SDK Manager中进行Android SDK Platform Tools的安装,这个很简单相信你一定会。
下面复制上面的SDK安装的路径:C:\\Users\\123\\AppData\\Local\\Android\\Sdk,进入到这个路径你会看到platform-tools这个文件夹。
进入platform-tools这个文件夹,adb工具用到的就是这三个程序:
复制包含这三个程序(包含adb)的地址:C:\\Users\\123\\AppData\\Local\\Android\\Sdk\\platform-tools,粘贴到“系统变量”的path变量中:
回到cmd命令行终端,输入:adb
看到如下信息,说明配置成功。
2.安装并配置python环境变量,到如下的python官网下载python安装包,在安装界面第一页面一定要勾选上创建python的环境变量。这要安装过程会自动加入python的环境变量,不需要手动安装了。
安装很简单,不用展示了。在cmd命令行终端输入命令:python
有如下信息说明安装成功。
关闭执行过python命令的cmd命令行终端窗口。我们暂时不需要在python命令行窗口中执行命令。重新打开一个cmd窗口,输入命令pip -V
pip是python的应用包管理工具。这里我们的pip工具是最新的。你可以到官网去对比看一下版本。
3.安装我们的主角: uiautomator2 :uiautomator2 · PyPI
安装完python之后,复制上图pip的安装命令到命令行终端执行:pip install uiautomator2
执行结果:截图版和文字版如下:
执行结果文字版拷贝如下:
C:\\Users\\123>
C:\\Users\\123>pip install uiautomator2
Collecting uiautomator2
Downloading uiautomator2-2.16.14-py3-none-any.whl (3.1 MB)
---------------------------------------- 3.1/3.1 MB 816.4 kB/s eta 0:00:00
Collecting Pillow
Downloading Pillow-9.0.1-cp310-cp310-win_amd64.whl (3.2 MB)
---------------------------------------- 3.2/3.2 MB 666.2 kB/s eta 0:00:00
Collecting filelock<4.0,>=3.0.12
Downloading filelock-3.6.0-py3-none-any.whl (10.0 kB)
Collecting lxml>=4.3
Downloading lxml-4.8.0-cp310-cp310-win_amd64.whl (3.6 MB)
---------------------------------------- 3.6/3.6 MB 854.6 kB/s eta 0:00:00
Collecting adbutils<1.0,>=0.11.0
Downloading adbutils-0.14.0-py3-none-win_amd64.whl (1.4 MB)
---------------------------------------- 1.4/1.4 MB 952.5 kB/s eta 0:00:00
Collecting logzero~=1.5
Downloading logzero-1.7.0-py2.py3-none-any.whl (16 kB)
Collecting retry~=0.9
Downloading retry-0.9.2-py2.py3-none-any.whl (8.0 kB)
Collecting packaging~=20.3
Downloading packaging-20.9-py2.py3-none-any.whl (40 kB)
---------------------------------------- 40.9/40.9 KB 387.7 kB/s eta 0:00:00
Collecting requests
Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
---------------------------------------- 63.1/63.1 KB 839.6 kB/s eta 0:00:00
Collecting six
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting Deprecated~=1.2.6
Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting progress~=1.3
Downloading progress-1.6.tar.gz (7.8 kB)
Preparing metadata (setup.py) ... done
Collecting whichcraft
Downloading whichcraft-0.6.1-py2.py3-none-any.whl (5.2 kB)
Collecting cached-property<2.0,>=1.5.1
Downloading cached_property-1.5.2-py2.py3-none-any.whl (7.6 kB)
Collecting apkutils2<2.0,>=1.0.0
Downloading apkutils2-1.0.0.tar.gz (60 kB)
---------------------------------------- 61.0/61.0 KB 141.4 kB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Collecting deprecation<3.0,>=2.0.6
Downloading deprecation-2.1.0-py2.py3-none-any.whl (11 kB)
Collecting wrapt<2,>=1.10
Downloading wrapt-1.14.0-cp310-cp310-win_amd64.whl (36 kB)
Collecting colorama
Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Collecting pyparsing>=2.0.2
Downloading pyparsing-3.0.7-py3-none-any.whl (98 kB)
---------------------------------------- 98.0/98.0 KB 160.5 kB/s eta 0:00:00
Collecting py<2.0.0,>=1.4.26
Downloading py-1.11.0-py2.py3-none-any.whl (98 kB)
---------------------------------------- 98.7/98.7 KB 297.9 kB/s eta 0:00:00
Collecting decorator>=3.4.2
Downloading decorator-5.1.1-py3-none-any.whl (9.1 kB)
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
---------------------------------------- 139.0/139.0 KB 634.2 kB/s eta 0:00:00
Collecting idna<4,>=2.5
Downloading idna-3.3-py3-none-any.whl (61 kB)
---------------------------------------- 61.2/61.2 KB 808.3 kB/s eta 0:00:00
Collecting charset-normalizer~=2.0.0
Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting certifi>=2017.4.17
Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
---------------------------------------- 149.2/149.2 KB 555.8 kB/s eta 0:00:00
Collecting pyelftools
Downloading pyelftools-0.28-py2.py3-none-any.whl (155 kB)
---------------------------------------- 155.4/155.4 KB 772.4 kB/s eta 0:00:00
Collecting cigam
Downloading cigam-0.0.3-py3-none-any.whl (3.8 kB)
Collecting xmltodict
Downloading xmltodict-0.12.0-py2.py3-none-any.whl (9.2 kB)
Using legacy 'setup.py install' for progress, since package 'wheel' is not installed.
Using legacy 'setup.py install' for apkutils2, since package 'wheel' is not installed.
Installing collected packages: whichcraft, pyelftools, progress, cigam, certifi, cached-property, xmltodict, wrapt, urllib3, six, pyparsing, py, Pillow, lxml, idna, filelock, decorator, colorama, charset-normalizer, retry, requests, packaging, logzero, Deprecated, apkutils2, deprecation, adbutils, uiautomator2
Running setup.py install for progress ... done
Running setup.py install for apkutils2 ... done
Successfully installed Deprecated-1.2.13 Pillow-9.0.1 adbutils-0.14.0 apkutils2-1.0.0 cached-property-1.5.2 certifi-2021.10.8 charset-normalizer-2.0.12 cigam-0.0.3 colorama-0.4.4 decorator-5.1.1 deprecation-2.1.0 filelock-3.6.0 idna-3.3 logzero-1.7.0 lxml-4.8.0 packaging-20.9 progress-1.6 py-1.11.0 pyelftools-0.28 pyparsing-3.0.7 requests-2.27.1 retry-0.9.2 six-1.16.0 uiautomator2-2.16.14 urllib3-1.26.9 whichcraft-0.6.1 wrapt-1.14.0 xmltodict-0.12.0
最后一行输出安装成功:
Successfully installed Deprecated-1.2.13 Pillow-9.0.1 adbutils-0.14.0 apkutils2-1.0.0 cached-property-1.5.2 certifi-2021.10.8 charset-normalizer-2.0.12 cigam-0.0.3 colorama-0.4.4 decorator-5.1.1 deprecation-2.1.0 filelock-3.6.0 idna-3.3 logzero-1.7.0 lxml-4.8.0 packaging-20.9 progress-1.6 py-1.11.0 pyelftools-0.28 pyparsing-3.0.7 requests-2.27.1 retry-0.9.2 six-1.16.0 uiautomator2-2.16.14 urllib3-1.26.9 whichcraft-0.6.1 wrapt-1.14.0 xmltodict-0.12.0
4.初始化手机设备(这步你可以先跳过,进行第五步安装weditor):
# init就是所有USB连接电脑的手机上都安装uiautomator2
python -m uiautomator2 init
或者
# 指定手机安装uiautomator2, 用 --mirror
python -m uiautomator2 init --mirror --serial $SERIAL
5.安装可视化网页视图工具weditor,执行下面命令:
如果你直接输入如下命令行:
pip install weditor
或者:
pip install -U weditor
没有在weditor后面指定版本号的话,默认安装最新版的weditor。众所周知,最新版会出各种各样的幺蛾子和坑,然后你就会填坑,从而浪费生命。比如我现在官网(weditor · PyPI)看到的weditor最新版是weditor 0.6.5
这个新版本就会报如下的错误:
完整报错如下:
C:\\Users\\123>pip install weditor
Collecting weditor
Using cached weditor-0.6.5.tar.gz (2.2 MB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [29 lines of output]
D:\\User\\python\\lib\\site-packages\\setuptools\\dist.py:717: UserWarning: Usage of dash-separated 'author-email' will not be supported in future versions. Please use the underscore name 'author_email' instead
warnings.warn(
D:\\User\\python\\lib\\site-packages\\setuptools\\dist.py:717: UserWarning: Usage of dash-separated 'home-page' will not be supported in future versions. Please use the underscore name 'home_page' instead
warnings.warn(
D:\\User\\python\\lib\\site-packages\\setuptools\\dist.py:717: UserWarning: Usage of dash-separated 'description-file' will not be supported in future versions. Please use the underscore name 'description_file' instead
warnings.warn(
WARNING: The wheel package is not available.
ERROR:root:Error parsing
Traceback (most recent call last):
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\core.py", line 111, in pbr
attrs = util.cfg_to_args(path, dist.script_args)
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\util.py", line 272, in cfg_to_args
pbr.hooks.setup_hook(config)
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\hooks\\__init__.py", line 25, in setup_hook
metadata_config.run()
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\hooks\\base.py", line 27, in run
self.hook()
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\hooks\\metadata.py", line 25, in hook
self.config['version'] = packaging.get_version(
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\packaging.py", line 860, in get_version
version = _get_version_from_pkg_metadata(package_name)
File "c:\\users\\123\\appdata\\local\\temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\.eggs\\pbr-5.8.1-py3.10.egg\\pbr\\packaging.py", line 828, in _get_version_from_pkg_metadata
pkg_metadata = email.message_from_file(pkg_metadata_file)
File "D:\\User\\python\\lib\\email\\__init__.py", line 54, in message_from_file
return Parser(*args, **kws).parse(fp)
File "D:\\User\\python\\lib\\email\\parser.py", line 53, in parse
data = fp.read(8192)
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 825: illegal multibyte sequence
error in setup command: Error parsing C:\\Users\\123\\AppData\\Local\\Temp\\pip-install-ibbriknw\\weditor_dd4b241b3d8b465a81734c820f336eb0\\setup.cfg: UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 825: illegal multibyte sequence
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
C:\\Users\\123>pip
度娘一下总之是不成功的:
来到这个项目的GitHub官网,你发现确实是有问题的,不是你配置出错或电脑的问题:win10系统安装0.6.5版本时报错 · Issue #103 · alibaba/web-editor · GitHub
推荐我们使用指定版本0.6.4的:
没办法只能接受推荐,安装weditor 0.6.4版本:
使用命令:pip install weditor==0.6.4
我们就可以成功安装。输出信息如下:
完整信息如下:
C:\\Users\\123>pip install weditor==0.6.4
Collecting weditor==0.6.4
Downloading weditor-0.6.4-py3-none-any.whl (1.6 MB)
---------------------------------------- 1.6/1.6 MB 2.3 MB/s eta 0:00:00
Requirement already satisfied: pillow in d:\\user\\python\\lib\\site-packages (from weditor==0.6.4) (9.0.1)
Requirement already satisfied: uiautomator2>=2.10.1 in d:\\user\\python\\lib\\site-packages (from weditor==0.6.4) (2.16.14)
Collecting facebook-wda>=1.0.5
Downloading facebook_wda-1.4.6-py3-none-any.whl (25 kB)
Collecting tornado>=4.3
Downloading tornado-6.1.tar.gz (497 kB)
---------------------------------------- 497.4/497.4 KB 3.9 MB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Requirement already satisfied: logzero in d:\\user\\python\\lib\\site-packages (from weditor==0.6.4) (1.7.0)
Requirement already satisfied: six in d:\\user\\python\\lib\\site-packages (from weditor==0.6.4) (1.16.0)
Requirement already satisfied: cached-property in d:\\user\\python\\lib\\site-packages (from weditor==0.6.4) (1.5.2)
Collecting pywin32
Downloading pywin32-303-cp310-cp310-win_amd64.whl (9.2 MB)
---------------------------------------- 9.2/9.2 MB 3.4 MB/s eta 0:00:00
Requirement already satisfied: Deprecated~=1.2.6 in d:\\user\\python\\lib\\site-packages (from facebook-wda>=1.0.5->weditor==0.6.4) (1.2.13)
Requirement already satisfied: requests>=2.9.1 in d:\\user\\python\\lib\\site-packages (from facebook-wda>=1.0.5->weditor==0.6.4) (2.27.1)
Requirement already satisfied: retry in d:\\user\\python\\lib\\site-packages (from facebook-wda>=1.0.5->weditor==0.6.4) (0.9.2)
Requirement already satisfied: lxml>=4.3 in d:\\user\\python\\lib\\site-packages (from uiautomator2>=2.10.1->weditor==0.6.4) (4.8.0)
Requirement already satisfied: filelock<4.0,>=3.0.12 in d:\\user\\python\\lib\\site-packages (from uiautomator2>=2.10.1->weditor==0.6.4) (3.6.0)
Requirement already satisfied: packaging~=20.3 in d:\\user\\python\\lib\\site-packages (from uiautomator2>=2.10.1->weditor==0.6.4) (20.9)
Requirement already satisfied: progress~=1.3 in d:\\user\\python\\lib\\site-packages (from uiautomator2>=2.10.1->weditor==0.6.4) (1.6)
Requirement already satisfied: whichcraft in d:\\user\\python\\lib\\site-packages (from uiautomator2>=2.10.1->weditor==0.6.4) (0.6.1)
Requirement already satisfied: adbutils<1.0,>=0.11.0 in d:\\user\\python\\lib\\site-packages (from uiautomator2>=2.10.1->weditor==0.6.4) (0.14.0)
Requirement already satisfied: colorama in d:\\user\\python\\lib\\site-packages (from logzero->weditor==0.6.4) (0.4.4)
Requirement already satisfied: apkutils2<2.0,>=1.0.0 in d:\\user\\python\\lib\\site-packages (from adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor==0.6.4) (1.0.0)
Requirement already satisfied: deprecation<3.0,>=2.0.6 in d:\\user\\python\\lib\\site-packages (from adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor==0.6.4) (2.1.0)
Requirement already satisfied: wrapt<2,>=1.10 in d:\\user\\python\\lib\\site-packages (from Deprecated~=1.2.6->facebook-wda>=1.0.5->weditor==0.6.4) (1.14.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:\\user\\python\\lib\\site-packages (from packaging~=20.3->uiautomator2>=2.10.1->weditor==0.6.4) (3.0.7)
Requirement already satisfied: idna<4,>=2.5 in d:\\user\\python\\lib\\site-packages (from requests>=2.9.1->facebook-wda>=1.0.5->weditor==0.6.4) (3.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in d:\\user\\python\\lib\\site-packages (from requests>=2.9.1->facebook-wda>=1.0.5->weditor==0.6.4) (2.0.12)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\\user\\python\\lib\\site-packages (from requests>=2.9.1->facebook-wda>=1.0.5->weditor==0.6.4) (1.26.9)
Requirement already satisfied: certifi>=2017.4.17 in d:\\user\\python\\lib\\site-packages (from requests>=2.9.1->facebook-wda>=1.0.5->weditor==0.6.4) (2021.10.8)
Requirement already satisfied: py<2.0.0,>=1.4.26 in d:\\user\\python\\lib\\site-packages (from retry->facebook-wda>=1.0.5->weditor==0.6.4) (1.11.0)
Requirement already satisfied: decorator>=3.4.2 in d:\\user\\python\\lib\\site-packages (from retry->facebook-wda>=1.0.5->weditor==0.6.4) (5.1.1)
Requirement already satisfied: pyelftools in d:\\user\\python\\lib\\site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor==0.6.4) (0.28)
Requirement already satisfied: cigam in d:\\user\\python\\lib\\site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor==0.6.4) (0.0.3)
Requirement already satisfied: xmltodict in d:\\user\\python\\lib\\site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.11.0->uiautomator2>=2.10.1->weditor==0.6.4) (0.12.0)
Using legacy 'setup.py install' for tornado, since package 'wheel' is not installed.
Installing collected packages: pywin32, tornado, facebook-wda, weditor
Running setup.py install for tornado ... done
Successfully installed facebook-wda-1.4.6 pywin32-303 tornado-6.1 weditor-0.6.4
C:\\Users\\123>weditor --shortcut
Shortcut created. C:\\Users\\123\\Desktop\\WEditor.lnk
C:\\Users\\123>
安装好之后,就可以在命令行运行 weditor --help 确认是否安装成功了。
下面,我们在Windows系统中可以使用命令在桌面创建一个快捷方式:
1 |
|
在windows命令行终端cmd中执行上述命令后,会在桌面上创建一个快捷方式,如下图:
这时,确保你的手机已经通过USB链接到电脑并打开开发者模式并允许USB调试,使用命令adb adevices也能够检测到设备。然后双击这个WEditor图标,他们自动在浏览器中打开如下界面:
点击底部POWER开机键,会将你的手机屏幕映射到如图的位置,并在你的手机中安装App工具ATX(黄色小汽车图标。)和一个测试软件包,允许就行了。
至此。恭喜我们都成功配置了所有的环境。
关于这个WEditor使用请参见这篇文章(ios上的操作,但是API的用法一样):
这里顺便说一下:在Android SDK工具包中也自带了一个可视化的界面工具UI Automator Viewer工具,文件路径如下:
C:\\Users\\123\\AppData\\Local\\Android\\Sdk\\tools\\bin
双击即可运行,界面如下:
具体使用说明参见我的另一篇文章:Android SDK 自带UI Automator Viewer说明
这两种方式的使用都一样。但是大部分开发者都说WEditor更好用。
由于上面的环境配置太多、太麻烦,所以记录下来,方便需要的人参考。
以上是关于Android Studio使用uiautomator2实现自动化测试及环境变量配置的主要内容,如果未能解决你的问题,请参考以下文章