uiautomator2,这是一款开源的UI自动化测试框架,可结合Python用于Android应用的自动化测试。
uiautomator2,其底层基于谷歌下的uiautomator库进行了封装,提供了便利的Python接口,允许测试人员直接在PC上编写Python代码,来获取屏幕上任一App的任意控件属性,并对其进行任意操作,极大提高了自动化代码编写的效率。
更多的介绍,大家可以前往 Github 进行学习:https://github.com/openatx/uiautomator2
安装 ADB
命令行窗口下,输入命令:adb devices,如果出现报错则说明未安装配置好adb相关环境。
如果未安装 adb,那么需要到谷歌官网下载 Android SDK Platform Tools,地址:https://developer.android.com/studio/releases/platform-tools.html,下载并解压,完成后把包含 adb.exe 的目录添加到系统的环境变量PATH中。
安装 uiautomator2
通过 Python 的 pip 工具进行安装,命令如下:pip install --pre -U uiautomator2 。
如果是多版本的共存的,可能会出现no module uiautomator2的提示。这是常碰到的问题,不要折腾,就卸载一个版本就是,然后看一下现用python版本
Python -V
看一下版本。如果提示no path .........啥的,说明你卸载了系统当前默认的版本,去python目录,把现有的版本文件夹整个复制一份,改成提示中的目录名即可。
点我复制(venv) D:\pycharm\Code\atxDemo2\venv\Scripts>pip install --pre -U uiautomator2
Collecting uiautomator2
Downloading https://files.pythonhosted.org/packages/3a/3d/731395da6692447598786f35a6921f816498dcdaf7247aaf5d788a2a11a8/uiautomator2-2.15.2.tar.gz (677kB)
100% |████████████████████████████████| 686kB 7.8kB/s
Collecting six (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl
Collecting requests (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl (61kB)
100% |████████████████████████████████| 61kB 6.0kB/s
Collecting whichcraft (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/b5/a2/81887a0dae2e4d2adc70d9a3557fdda969f863ced51cd3c47b587d25bce5/whichcraft-0.6.1-py2.py3-none-any.whl
Collecting logzero~=1.5 (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/b3/68/aa714515d65090fcbcc9a1f3debd5a644b14aad11e59238f42f00bd4b298/logzero-1.7.0-py2.py3-none-any.whl
Collecting progress~=1.3 (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/38/ef/2e887b3d2b248916fc2121889ce68af8a16aaddbe82f9ae6533c24ff0d2b/progress-1.5.tar.gz
Collecting retry~=0.9 (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/4b/0d/53aea75710af4528a25ed6837d71d117602b01946b307a3912cb3cfcbcba/retry-0.9.2-py2.py3-none-any.whl
Collecting adbutils<1.0,>=0.11.0 (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/e7/d7/cf5d49c69ebc480c4422ead0d16e22c22df0a0bd6cfd1ae292f7499568fe/adbutils-0.11.0-py3-none-win_amd64.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 5.7kB/s
Collecting Deprecated~=1.2.6 (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/fb/73/994edfcba74443146c84b91921fcc269374354118d4f452fb0c54c1cbb12/Deprecated-1.2.12-py2.py3-none-any.whl
Collecting Pillow (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/84/41/b233abcfb9bae12768bffae92e2c96d2d807cd92a3cef7d24987df0e3d64/Pillow-8.2.0-cp37-cp37m-win_amd64.whl (2.2MB)
100% |████████████████████████████████| 2.2MB 27kB/s
Collecting lxml>=4.3 (from uiautomator2)
Downloading https://files.pythonhosted.org/packages/9e/5e/171ee9d40a600f565fe691ec5bf7596247ec62cfb2edc00c91afe8ea837b/lxml-4.6.3-cp37-cp37m-win_amd64.whl (3.5MB)
100% |████