最近偶尔玩玩自己安卓手机小米9中的游戏:
欢乐大作战


欢乐大作战 – 搜索结果 – 哔哩哔哩弹幕视频网 – ( ゜- ゜)つロ 乾杯~ – bilibili
平台:Android
游戏简介:一款好玩刺激的策略卡牌类游戏
发现其中一些页面,是比较无聊的,只是单纯多次点击即可的
比如:
主线关卡中的每次单次过关的挑战:




试炼的升级:


后续操作,和上面的战斗 类似的。
而最近接触到uiautomator2可以实现安卓的自动化点击操作
所以想着,去试试,自己写脚本,实现自动挂机功能,至少实现主线关卡和试炼能够自动化操作。
省去每次自己浪费时间点击即可。
主线或试炼的战斗,结果大都是胜利,偶尔,或者说直到失败,即可。
此处小米9,之前被另外项目调试过,所以安装了ATX或uiautomator2相关的东西。
此处尽量去删除掉,完整的重新搭建一遍调试环境。
先去卸载之前安装的ATX。

重新搭建整个开发环境,包括Mac中VSCode调试环境和小米9的环境。
uiautomator2 mac
UIAutomator2的使用 | Vicの博客
UI Automator is a UI testing framework suitable for cross-app functional UI testing across system and installed apps.
还是看官网吧
此处mac中之前有pipenv,所以尽量用虚拟环境去安装python环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ➜ autoTestAndroidGameHappyBigBattle pipenv shell Creating a virtualenv for this project… Pipfile: /Users/crifan/dev/dev_root/python/autoTestAndroid/autoTestAndroidGameHappyBigBattle/Pipfile Using /usr/local/opt/python/bin/python3 .7 (3.7.3) to create virtualenv… ⠦ Creating virtual environment...Already using interpreter /usr/local/opt/python/bin/python3 .7 Using base prefix '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7' New python executable in /Users/crifan/ . local /share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/bin/python3 .7 Also creating executable in /Users/crifan/ . local /share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/bin/python Installing setuptools, pip, wheel... done .
Virtualenv location: /Users/crifan/ . local /share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z Creating a Pipfile for this project… Launching subshell in virtual environment… . /Users/crifan/ . local /share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/bin/activate ➜ autoTestAndroidGameHappyBigBattle . /Users/crifan/ . local /share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/bin/activate ➜ autoTestAndroidGameHappyBigBattle which python /Users/crifan/ . local /share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/bin/python ➜ autoTestAndroidGameHappyBigBattle python --version Python 3.7.3 |
然后去安装uiautomator2:
【记录】mac中用pipenv安装uiautomator2
另外去看看mac上的adb:
1 2 3 4 5 6 7 8 9 10 | ➜ ~ which adb /Users/crifan/dev/dev_tool/android/AndroidSDK/platform-tools/adb ➜ ~ adb --version Android Debug Bridge version 1.0.41 Version 29.0.1-5644136 Installed as /Users/crifan/dev/dev_tool/android/AndroidSDK/platform-tools/adb ➜ ~ adb devices * daemon not running; starting now at tcp:5037 * daemon started successfully List of devices attached |
把小米9手机连接到Mac后
首次连接,提示,勾选信任:


能否找到:
1 2 3 | ➜ ~ adb devices List of devices attached 8c8a4d4d device |
是可以找到的。
再去pipenv中安装其他库:
1 | python -m uiautomator2 init |
此处安卓小米9中需要点击 继续安装:
- ATX
- com.github.uiautomator.test
但是过了会,报错了:
【基本解决】uiautomator2初始化到安卓手机小米9中报错:http.client.RemoteDisconnected Remote end closed connection without response
之后,就可以正常连接了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ➜ autoTestAndroidGameHappyBigBattle python Python 3.7 . 3 (default, May 22 2019 , 10 : 55 : 14 ) [Clang 10.0 . 1 (clang - 1001.0 . 46.4 )] on darwin Type "help" , "copyright" , "credits" or "license" for more information. >>> import uiautomator2 as u2 >>> d = u2.connect( '8c8a4d4d' ) >>> print (d.info) / Users / crifan / .local / share / virtualenvs / autoTestAndroidGameHappyBigBattle - F2NzwZ7Z / lib / python3. 7 / site - packages / uiautomator2 / __init__.py: 462 : RuntimeWarning: uiautomator2 is not reponding, restart uiautomator2 automatically stacklevel = 1 ) [D 191120 21 : 35 : 39 __init__: 639 ] force reset uiautomator [D 191120 21 : 35 : 39 __init__: 655 ] uiautomator( 1.0 ) is starting ... [D 191120 21 : 35 : 40 __init__: 655 ] uiautomator( 1.0 ) is starting ... [I 191120 21 : 35 : 40 __init__: 643 ] uiautomator back to normal { 'currentPackageName' : 'com.miui.home' , 'displayHeight' : 2135 , 'displayRotation' : 0 , 'displaySizeDpX' : 393 , 'displaySizeDpY' : 851 , 'displayWidth' : 1080 , 'productName' : 'cepheus' , 'sdkInt' : 29 , 'naturalOrientation' : True } >>> |
然后:
【未解决】Mac中安装uiautomator2的UI界面工具:weditor
然后想办法去获取界面中的元素:
【未解决】uiautomator2中如何获取到欢乐大作战游戏界面中的元素
后来为了调试方便,试试WiFi的IP

去连接看看
192.168.31.17
1 | AndroidDeviceWifiIP_Xiaomi9 = "192.168.31.17" |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ➜ autoTestAndroidGameHappyBigBattle / Users / crifan / .local / share / virtualenvs / autoTestAndroidGameHappyBigBattle - F2NzwZ7Z / bin / python / Users / crifan / .vscode / extensions / ms - python.python - 2019.10 . 44104 / pythonFiles / ptvsd_launcher.py - - default - - client - - host localhost - - port 64779 / Users / crifan / dev / dev_root / python / autoTestAndroid / autoTestAndroidGameHappyBigBattle / autoTestAndroidGame.py Traceback (most recent call last): File "/Users/crifan/.vscode/extensions/ms-python.python-2019.10.44104/pythonFiles/ptvsd_launcher.py" , line 43 , in <module> main(ptvsdArgs) File "/Users/crifan/.vscode/extensions/ms-python.python-2019.10.44104/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py" , line 432 , in main run() File "/Users/crifan/.vscode/extensions/ms-python.python-2019.10.44104/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py" , line 316 , in run_file runpy.run_path(target, run_name = '__main__' ) File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py" , line 263 , in run_path pkg_name = pkg_name, script_name = fname) File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py" , line 96 , in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py" , line 85 , in _run_code exec (code, run_globals) File "/Users/crifan/dev/dev_root/python/autoTestAndroid/autoTestAndroidGameHappyBigBattle/autoTestAndroidGame.py" , line 67 , in <module> autoTestAndroidGame() File "/Users/crifan/dev/dev_root/python/autoTestAndroid/autoTestAndroidGameHappyBigBattle/autoTestAndroidGame.py" , line 37 , in autoTestAndroidGame d = u2.connect(AndroidDeviceWifiIP_Xiaomi9) File "/Users/crifan/.local/share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/lib/python3.7/site-packages/uiautomator2/__init__.py" , line 122 , in connect return connect_usb(addr) File "/Users/crifan/.local/share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/lib/python3.7/site-packages/uiautomator2/__init__.py" , line 164 , in connect_usb lport = device.forward_port( 7912 ) File "/Users/crifan/.local/share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/lib/python3.7/site-packages/adbutils/__init__.py" , line 388 , in forward_port self ._client.forward( self ._serial, "tcp:" + str (local_port), remote) File "/Users/crifan/.local/share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/lib/python3.7/site-packages/adbutils/__init__.py" , line 250 , in forward c.check_okay() File "/Users/crifan/.local/share/virtualenvs/autoTestAndroidGameHappyBigBattle-F2NzwZ7Z/lib/python3.7/site-packages/adbutils/__init__.py" , line 137 , in check_okay raise AdbError( self .read_string()) adbutils.errors.AdbError: device '192.168.31.17' not found |
mac中:
1 2 | ➜ ~ adb devices List of devices attached |
无线连接是找不到设备的。
转载请注明:在路上 » 【未解决】给安卓手机小米9中欢乐大作战的游戏实现自动挂机