环境准备
确保设备已安装ADB工具并启用USB调试模式。通过以下命令验证设备连接:
bash
adb devices
输出应显示设备序列号和"device"状态。
基础ADB操作
获取当前活动窗口信息:
bash
adb shell dumpsys window windows | grep -E 'mCurrentFocus'
模拟点击事件需获取屏幕坐标:
bash
adb shell getevent -p
输入自动化
执行文本输入命令:
bash
adb shell input text "test123"
模拟物理按键操作:
bash
adb shell input keyevent 4 # 返回键
屏幕截图与分析
截取屏幕并导出到本地:
bash
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png ~/Desktop/
使用OpenCV进行图像识别(需Python环境):
python
import cv2
template = cv2.imread('button_template.png', 0)
screen = cv2.imread('screen.png', 0)
res = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
事件序列封装
创建可复用的Shell函数:
bash
function tap() {
adb shell input tap $1 $2
}
function swipe() {
adb shell input swipe $1 $2 $3 $4 $5
}
循环测试逻辑
实现重复压力测试:
bash
for i in {1..100}; do
tap 500 800
sleep 1
adb shell input keyevent 3
done
日志收集
定向抓取错误日志:
bash
adb logcat -v time | grep "E/"
按时间戳保存日志文件:
bash
adb logcat -d -v time > $(date +"%Y%m%d_%H%M%S").log
性能监控
获取CPU使用率:
bash
adb shell top -n 1 | grep com.target.app
监测内存占用:
bash
adb shell dumpsys meminfo com.target.app
异常处理
检测ANR并重启应用:
bash
if adb logcat -d | grep -q "ANR in"; then
adb shell am force-stop com.target.app
adb shell am start -n com.target.app/.MainActivity
fi
设备控制
重启设备并等待恢复:
bash
adb reboot
until adb devices | grep -w device; do sleep 5; done
设置系统属性值:
bash
adb shell setprop debug.layout false