Maxim 日常工作中使用频率比较高,就用 python 进行封装,结合 u2 稳定的无线连接,基本上抛弃了 USB 线,跟凌乱的桌面说再见。
maxim 执行流程
![2e6e025cc6204a5748f3177122530e88.gif](https://i-blog.csdnimg.cn/blog_migrate/90356c1ced7ac058495115b76674d445.gif)
1、初始化 u2,通过 USB 或无线连接设备;
2、通过 u2 调用 adb shell,初始化 maxim 的执行环境;
3、执行 maxim 命令,打印执行日志;
4、获取输出日志,如果有闪退日志,则推送至电脑,方便查看。
实现过程中,直接通过 u2 调用 d.shell() 执行 maxim 命令时,点击事件出现严重报错,导致程序直接停止,报错信息如下:
java.lang.IllegalStateException: UiAutomation not connected!
这是由于 u2 和 maxim 都需要调用 uiautomator 来连接安卓系统的 AccessibilityService,被 u2 占用后,再调用 maxim 会引起冲突,导致 maxim 直接挂掉。
appetizer 文档内提供了一种解决办法:
经过研究,uiautomator2 中 android-uiautomator-server 只是提供 selector、click 等相关的服务;atx-agent 提供执行设备端 shell 命令的功能。
所以,先启动所有 u2 组件待 Wi-Fi 无线操作可用后,暂时关闭 android-uiautomator-server,然后通过 atx-agent 启动 maxim,执行完成后再通过 atx-agent 重新拉活 android-uiautomator-server 恢复 uiautomator2 的功能。
重新梳理下步骤,使用伪代码展示:
# 1. 创建实例,传入设备,包名等参数
maxim = Maxim("device", "pkg_name", ...)
# 2. 初始化;
# a. 推送所有依赖到设备,jar包、配置文件等;
# b. 处理设备锁屏
# c. 删除设备内历史日志
maxim.setup()
# 3. 停掉 u2 的 uiaitomator 服务
maxim.stop_uiautomator()
# 4. 执行 maxim 命令,实时打印执行日志
maxim.exec_maxim()
# 5. 重启 uiautomator 服务
maxim.start_uiautomator()
# 6. 如果出现闪退,则将日志推送到 PC
maxim.teardown()
![2e6e025cc6204a5748f3177122530e88.gif](https://i-blog.csdnimg.cn/blog_migrate/90356c1ced7ac058495115b76674d445.gif)
"""
@lan 20200512@github https://github.com/lanyangbi/maxim_test_by_u2
"""
设备锁屏处理
u2 自带的 d.unlock() 是左滑解锁,由于我的设备都是固定上滑解锁,所以特殊处理。
本来想做个兼容,左滑没解锁成功就上滑,没想到 锁屏/非锁屏 状态下,获取到的 app_current 都是一样的,暂时就先这样了,留个 TODO 在这里吧。
if not d.info.get('screenOn'):
before_activity = d.app_current()["activity"]
print(">>> before:" + before_activity)
d.unlock()
d.sleep(0.5)
after_activity = d.app_current()["activity"]
print(">>> after:" + after_activity)
if before_activity == after_activity:
print("before = after")
d.swipe(0.5, 0.8, 0.5, 0.2) # 上滑解锁
参考:
uiautomator2:https://github.com/openatx/uiautomator2
maxim:https://github.com/zhangzhao4444/Maxim
appetizer 文档:https://www.appetizer.io