最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Mac中初始化安卓手机红米Note10X的uiautomator2开发环境

Mac crifan 696浏览 0评论
折腾:
【未解决】Mac中用uiautomator2自动操作安卓手机浏览器实现百度搜索
期间,去Mac中安装和初始化uiautomator2的环境。
把安卓手机 红米Note10X 插入Mac
弹框提示:
以及确定USB是开启的:
然后Mac端,去确保adb能找到设备:
 crifan@licrifandeMacBook-Pro  ~  which adb
/Users/crifan/dev/dev_tool/android/AndroidSDK/platform-tools/adb
 crifan@licrifandeMacBook-Pro  ~  adb devices
* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached


 crifan@licrifandeMacBook-Pro  ~  adb devices
List of devices attached
orga4pmzee4ts47t    device
然后去Mac中安装uiautomator2
openatx/uiautomator2: Android Uiautomator2 Python Wrapper
以及自己教程:
安卓自动化测试利器:uiautomator2
https://book.crifan.com/books/android_automation_uiautomator2/website/
环境搭建 · 安卓自动化测试利器:uiautomator2
先进来让Python版本高点,设置本地是3.7.3的
✘ crifan@licrifandeMacBook-Pro  ~/dev/dev_root/python/baiduSearchAutomation/uiautomator2DemoBaiduSearch  pyenv global      
3.6.6
 crifan@licrifandeMacBook-Pro  ~/dev/dev_root/python/baiduSearchAutomation/uiautomator2DemoBaiduSearch  pyenv local 3.7.3
 crifan@licrifandeMacBook-Pro  ~/dev/dev_root/python/baiduSearchAutomation/uiautomator2DemoBaiduSearch  pyenv local 
3.7.3
然后:
 pip install -U uiautomator2 
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting uiautomator2
  Downloading http://mirrors.aliyun.com/pypi/packages/20/b1/db5d40f38d349188011a988c83e688ed111133d33cfb0db5d26b2ef5b30b/uiautomator2-2.14.1.tar.gz (676 kB)
     |████████████████████████████████| 676 kB 4.7 MB/s 
Requirement already satisfied: six in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from uiautomator2) (1.15.0)
Requirement already satisfied: requests in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from uiautomator2) (2.22.0)
Collecting whichcraft
。。。
Collecting py<2.0.0,>=1.4.26
  Downloading http://mirrors.aliyun.com/pypi/packages/67/32/6fe01cfc3d1a27c92fdbcdfc3f67856da8cbadf0dd9f2e18055202b2dc62/py-1.10.0-py2.py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 2.9 MB/s 
Collecting decorator>=3.4.2
  Downloading http://mirrors.aliyun.com/pypi/packages/ed/1b/72a1821152d07cf1d8b6fce298aeb06a7eb90f4d6d41acec9861e7cc6df0/decorator-4.4.2-py2.py3-none-any.whl (9.2 kB)
Requirement already satisfied: certifi>=2017.4.17 in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from requests->uiautomator2) (2019.11.28)
Requirement already satisfied: idna<2.9,>=2.5 in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from requests->uiautomator2) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from requests->uiautomator2) (1.25.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/crifan/.pyenv/versions/3.6.6/lib/python3.6/site-packages (from requests->uiautomator2) (3.0.4)
Using legacy 'setup.py install' for uiautomator2, since package 'wheel' is not installed.
Using legacy 'setup.py install' for apkutils2, since package 'wheel' is not installed.
Using legacy 'setup.py install' for progress, since package 'wheel' is not installed.
Using legacy 'setup.py install' for wrapt, since package 'wheel' is not installed.
Installing collected packages: pyparsing, xmltodict, pyelftools, py, packaging, decorator, cigam, wrapt, whichcraft, retry, deprecation, apkutils2, progress, Pillow, logzero, Deprecated, cached-property, adbutils, uiautomator2
    Running setup.py install for wrapt ... done
    Running setup.py install for apkutils2 ... done
    Running setup.py install for progress ... done
    Running setup.py install for uiautomator2 ... done
Successfully installed Deprecated-1.2.12 Pillow-8.2.0 adbutils-0.10.0 apkutils2-1.0.0 cached-property-1.5.2 cigam-0.0.3 decorator-4.4.2 deprecation-2.1.0 logzero-1.7.0 packaging-20.9 progress-1.5 py-1.10.0 pyelftools-0.27 pyparsing-2.4.7 retry-0.9.2 uiautomator2-2.14.1 whichcraft-0.6.1 wrapt-1.12.1 xmltodict-0.12.0
然后再去初始化:
python3 -m uiautomator2 init
输出:
 python3 -m uiautomator2 init
[I 210402 21:27:03 init:154] uiautomator2 version: 2.14.1
[I 210402 21:27:03 init:346] Install minicap, minitouch
[D 210402 21:27:03 init:62] Download https://tool.appetizer.io/openatx/stf-binaries/raw/0.3.0/node_modules/@devicefarmer/minitouch-prebuilt/prebuilt/arm64-v8a/bin/minitouch
minitouch |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 34.1 KB/34.1 KB
[D 210402 21:27:04 init:231] Push to /data/local/tmp/minitouch:0755
[D 210402 21:27:04 init:62] Download https://tool.appetizer.io/openatx/stf-binaries/raw/0.3.0/node_modules/@devicefarmer/minicap-prebuilt/prebuilt/arm64-v8a/lib/android-29/minicap.so
minicap.so |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 28.4 KB/28.4 KB
[D 210402 21:27:05 init:231] Push to /data/local/tmp/minicap.so:0755
[D 210402 21:27:05 init:62] Download https://tool.appetizer.io/openatx/stf-binaries/raw/0.3.0/node_modules/@devicefarmer/minicap-prebuilt/prebuilt/arm64-v8a/bin/minicap
minicap |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 526.5 KB/526.5 KB
[D 210402 21:27:05 init:231] Push to /data/local/tmp/minicap:0755
[D 210402 21:27:05 init:249] apk-debug package-info: {'package_name': 'com.github.uiautomator', 'version_name': '2.3.1', 'version_code': '2003001', 'flags': ['DEBUGGABLE', 'HAS_CODE', 'ALLOW_CLEAR_USER_DATA', 'ALLOW_BACKUP'], 'first_install_time': datetime.datetime(2020, 12, 21, 10, 11, 35), 'last_update_time': datetime.datetime(2020, 12, 21, 10, 11, 35), 'signature': '8376b3fb], past signatures:['}
[D 210402 21:27:05 init:250] apk-debug-test package-info: {'package_name': 'com.github.uiautomator.test', 'version_name': '', 'version_code': '', 'flags': ['DEBUGGABLE', 'HAS_CODE', 'ALLOW_CLEAR_USER_DATA', 'ALLOW_BACKUP'], 'first_install_time': datetime.datetime(2020, 12, 21, 10, 11, 38), 'last_update_time': datetime.datetime(2020, 12, 21, 10, 11, 38), 'signature': '8376b3fb], past signatures:['}
[I 210402 21:27:05 init:256] package com.github.uiautomator version 2.3.1, latest 2.3.3
[I 210402 21:27:05 init:361] Install com.github.uiautomator, com.github.uiautomator.test 2.3.3
[D 210402 21:27:05 init:161] Shell: ('pm', 'uninstall', 'com.github.uiautomator')
[D 210402 21:27:06 init:161] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test')
[D 210402 21:27:06 init:62] Download https://tool.appetizer.io/openatx/android-uiautomator-server/releases/download/2.3.3/app-uiautomator.apk
app-uiautomator.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 2.1 MB/2.1 MB
[D 210402 21:27:07 init:231] Push to /data/local/tmp/app-uiautomator.apk:0644
[D 210402 21:27:07 init:161] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk')
[I 210402 21:27:14 init:330] - app-uiautomator.apk installed
[D 210402 21:27:14 init:62] Download https://tool.appetizer.io/openatx/android-uiautomator-server/releases/download/2.3.3/app-uiautomator-test.apk
app-uiautomator-test.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 1.0 MB/1.0 MB
[D 210402 21:27:14 init:231] Push to /data/local/tmp/app-uiautomator-test.apk:0644
[D 210402 21:27:14 init:161] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk')
[I 210402 21:27:18 init:330] - app-uiautomator-test.apk installed
[D 210402 21:27:18 init:289] Real version: [0, 9, 5], Expect version: [0, 10, 0]
[I 210402 21:27:18 init:338] Install atx-agent 0.10.0
[D 210402 21:27:18 init:62] Download https://tool.appetizer.io/openatx/atx-agent/releases/download/0.10.0/atx-agent_0.10.0_linux_armv7.tar.gz
atx-agent_0.10.0_linux_armv7.tar.gz |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 4.1 MB/4.1 MB
[D 210402 21:27:19 init:231] Push to /data/local/tmp/atx-agent:0755
[D 210402 21:27:19 init:161] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop')
[D 210402 21:27:19 init:161] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d', '--addr', '127.0.0.1:7912')
[I 210402 21:27:19 init:371] Check atx-agent version
[D 210402 21:27:19 init:384] Forward: local:tcp:62212 -> remote:tcp:7912
[D 210402 21:27:20 init:384] Forward: local:tcp:62212 -> remote:tcp:7912
[D 210402 21:27:20 init:387] atx-agent version 0.10.0
Successfully init AdbDevice(serial=orga4pmzee4ts47t)
期间,手机中弹框,确认去安装:
com.github.uiautomator.test
然后去写基本测试代码:
import uiautomator2 as u2
d = u2.connect() # connect to device
print(d.info)
去调试看看
竟然始终导入错误
发生异常: ModuleNotFoundError
No module named 'uiautomator2'
File "/Users/crifan/dev/dev_root/python/baiduSearchAutomation/uiautomator2DemoBaiduSearch/uiautomator2DemoBaiduSearch.py", line 5, in <module> import uiautomator2 as u2
算了,重新切换到3.6.6,重新初始化
 ✘ crifan@licrifandeMacBook-Pro  ~/dev/dev_root/python/baiduSearchAutomation/uiautomator2DemoBaiduSearch  pyenv versions     
  system
  3.5.2
  3.6.6
* 3.7.3 (set by PYENV_VERSION environment variable)
 crifan@licrifandeMacBook-Pro  ~/dev/dev_root/python/baiduSearchAutomation/uiautomator2DemoBaiduSearch  pyenv local 3.6.6
真就可以正常导入和调试了
-》反推前面是:安装的uiautomator2,是安装到了:pyenv的global的3.6.6了
继续调试:
注:
此处处于微信打开中
所以显示的是:
'currentPackageName':'com.tencent.mm'
完整输出:
{'currentPackageName': 'com.tencent.mm', 'displayHeight': 2201, 'displayRotation': 0, 'displaySizeDpX': 393, 'displaySizeDpY': 873, 'displayWidth': 1080, 'productName': 'atom', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
即说明环境没问题了。
接着去:
【已解决】Mac中安装和初始化weditor
【总结】
此处去Mac中初始化安装uiautomator2:
(1)安装uiautomator2
pip install -U uiautomator2
(2)确保adb可以查看到设备:
adb devices
此处输出:
 adb devices
List of devices attached
orga4pmzee4ts47t    device
其中:orga4pmzee4ts47t 是安卓设备的id
(3)初始化uiautomator2:安装ATX和com.github.uiautomator.test
python3 -m uiautomator2 init
会自动安装 最新版的:
  • ATX
  • com.github.uiautomator.test
apk到安卓手机中
(4)用测试代码确认u2正常工作
import uiautomator2 as u2
d = u2.connect() # connect to device
print(d.info)
能正常导入和打印出信息即可
另外:
【安装和初始化调试利器:weditor】
pip install -U weditor 
去安装weditor
然后用:
python -m weditor
启动。
打开网页:
http://localhost:17310
  • 连接设备
    • 输入 (从adb devices中得到的)安卓设备id
    • 点击Connect,看到绿色🚦红绿灯标识,标识连接上了。
  • 看安卓设备画面
    • 点击 实时,即可看到安卓手机的实时画面。
    • 如果只是想要调试页面元素,则可以切换会 静态
  • 查看属性
    • 点击画面中元素,即可看到属性
  • 查看元素结构
    • 点击顶部的 Dump Hierarchy,点击右边切换到 Hierarchy,可以看到最新页面的结构
详见:
【已解决】Mac中安装和初始化weditor

转载请注明:在路上 » 【已解决】Mac中初始化安卓手机红米Note10X的uiautomator2开发环境

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.186 seconds, using 23.34MB memory