压力测试结果:
CRASH: 崩溃, 应用程序在使用过程中, 非正常退出
- ANR:Application Not Responding
- MonkeyRunner APIs
MonkeyRunner: 用来连接设备或模拟器
MonkeyDevice: 提供安装, 卸载应用, 发送模拟事件
MonkeyImage : 完成图像保存, 及对比的操作
Monkey: 在 adb shell 中, 生成用户或系统的伪随机事件
MonkeyRunner: 通过 API 定义特定命令和事件控制设备
MonkeyRunner 提供了一系列的 API
可以完成模拟事件及截图操作
MonkeyScript
是一组可以被 Monkey 识别的命令集合
可以完成重复固定的操作
1. 为什么要进行压力测试?
App 不稳定
提高产品的留存率
2. 什么时候开展压力测试
首轮功能测试通过后
下班后的夜间进行
如何实现自动化测试:
模拟各种事件流
adb:Android Debug Bridge
adb 与手机里的 monkey 进行通讯
adb 调试应用的入口
- adb shell monkey 1000
- activityResuming(com.android.documentsui)
表示 app 从前台调入后台... 又从后台恢复到前台的过程
获取 App 包名
adb logcat|findstr START (cmp 后面的是 app 的包名)
给指定包打压力
- adb shell monkey -p package 1000
- -v
作用: 命令行上的每一个 - v 都将增加反馈信息的详细级别
比较常用的是 - v -v -v , 即最多详细, 一般会保存到指定
文件中供开发人员查找 bug 原因时使用
--throttle 参数
指定事件之间的间隔
adb shell monkey -p packagename --throttle 间隔毫秒 执行次数
seed 参数
指定随机生成数的 seed 值
如果用相同的 seed 值再次运行 monkey, 将生成相同的事件序列
- adb shell monkey -s<seed><event-count>
- adb shell monkey -p com.bit_health.android -s 7672 1000
触摸事件
设定触摸事件百分比
- adb shell monkey --pct-touch <percent>
- adb shell monkey -v -p com.bit_health.android --pct-touch 100 1000
可以在输出结果中的 Event percentages 中查看是否是
触摸事件百分百, 0 表示触摸事件
动作事件
设定动作事件百分比
adb shell monkey --pct-motion <percent>
这里设定的百分比要与其他事件的百分比之和等于 100
adb shell monkey -v -p com.bit_health.android --pct-touch 50 --pct-motion 30 500
轨迹球事件
设定轨迹球事件百分比
adb shell monkey --pct-trackball <percent>
基本导航事件
设定基本导肮事件百分比, 输入设备的上, 下, 左, 右
adb shell monkey --pct-nav <percent>
主要导航事件
设定主要导航事件百分比, 兼容中间键, 返回键, 菜单按键
adb shell monkey --pct-majornav <percent>
系统导航事件
设定系统导航事件百分比, HOME,Back, 拨号, 音量键
adb shell monkey --pct-syskeys <percent>
启动 Activity 事件
设定启动 Activity 的事件百分比
adb shell monkey --pct-appswitch <percent>
不常用事件
设定不常用事件的百分比
adb shell monkey --pct-anyevent <percent>
崩溃事件
忽略崩溃和异常
adb shell monkey --ignore-crashes <event-count>
超时事件
忽略超时事件
adb shell monkey --ignore-timeouts <event-count>
如果发生了 ANR 事件, 可以在手机中查看
1. 输入 adb shell 命令, 进入到手机
2. 跳转 anr 目录, cd /data/anr/
3. 在 anr 目录中, more traces.txt 查看报错信息
Monkey Script
1.DispatchTrackball 命令
轨迹球事件
- DispatchTrackball(long downtime,long eventide,
- int action,float x,float y,float pressure,
- float size,int metastate,float xprecision,
- float ypercision,int device,int edgeflags)
action 0 代表按下, 1 代表谈起, x 和 y 代表坐标点
2.DispatchPointer 命令
点击事件
- DispatchPointer(long downtime,long eventide,
- int action,float x,float y,float pressure,
- float size,int metastate,float xprecision,
- float ypercision,int device,int edgeflags)
action 0 代表按下, 1 代表谈起, x 和 y 代表坐标点
3.DispatchString 命令
输入字符串事件
DispatchString(String text)
4.LaunchActivity 命令
启动应用
LaunchActivity(package,Activity)
5.UserWait 命令
等待事件
UserWait(1000)
6.DispatchPress 命令
按下键值
DispatchPress(int keycode)#keycode 66 回车键
实例: 重复点击搜索 100 次
1. 启动 App
2. 点击输入框
3. 输入查询词
4. 点击键盘的回车
5. 点击搜索按钮
6. 等待结果的出现
7. 点击 clear 按钮
手机不能执行电脑上的脚本
将脚本 push 到手机中
adb push mook.script /data/local/tmp/
之后执行 adb shell monkey -f mook.script 2
执行的效果是无法正确启动 app
(!!!) 需要在 mainfest 清单文件中声明 android:exported="true"
意思是允许该 activity 被外部调用
使用 monkey script 编写脚本最大的困难是没有调试功能
也没有截屏操作
- MonkeyRunner
- 1.MonkeyRunnner API-alert
警告框
- void alert(String message,String title,String okTitle)
- 2.MonkeyRunner Api-waitForConnection
等待设备连接, 有多个 device id, 需要指明具体哪个设备
- waitForConnection(float timeout,String deviceid)
- 3.MonkeyDevice API -drag
拖动
drag(tuple start,tuple end ,float duration,integer steps)
start 起点位置
end 终点位置
duration 手势持续的时间
steps 插值点的步数, 默认 10
4.MonkeyDevice API -press
按键
press(String keycode,dictionary type)
keycode 名, Down,UP,DOWN_AND_UP
5.MonkeyDevice API -startActivity
启动应用
- startActivity(package+'/'+activity)
- 6.MonkeyDevice API - touch
点击
touch(integer x,integer y,integer type)
x 坐标值, y 坐标值
- type:DOWN,UP,DOWN_AND_UP
- 7.MonkeyDevice API - type
输入
- type(String message)
- 8.MonkeyDevice API - takeSnapshot
截屏
- MonkeyImage takeSnapshot()
- 9.MonkeyImage API - sameAs
图像对比
- boolean sameAS(MonkeyImage other,float percent)
- 10.MonkeyImage API - writetoFile
保存图像文件
void writetoFile(String path,String format)
来源: http://www.bubuko.com/infodetail-2734898.html