App压力测试MonkeyRunner整理

压力测试结果:
CRASH:崩溃,应用程序在使用过程中,非正常退出
ANR:Application Not Responding

命令很多,不用死记,用到复制、粘贴就行,达到目的最重要。

简单通俗易懂点讲就是,跟猴子一样乱点,查看日志是否有崩溃的日志,

配置好环境,设置好操作次数,关屏幕执行,第二天来看结果就行

MonkeyRunner APIs
MonkeyRunner:用来连接设备或模拟器
MonkeyDevice:提供安装、卸载应用,发送模拟事件
MonkeyImage :完成图像保存,及对比的操作

Monkey:在adb shell中,生成用户或系统的伪随机事件
MonkeyRunner:通过API定义特定命令和事件控制设备

MonkeyRunner提供了一系列的API
可以完成模拟事件及截图操作
MonkeyScript
是一组可以被Monkey识别的命令集合
可以完成重复固定的操作

1.为什么要进行压力测试?
App不稳定
提高产品的留存率

2.什么时候开展压力测试
首轮功能测试通过后
下班后的夜间进行

扫描二维码关注公众号,回复: 3691905 查看本文章

如何实现自动化测试:
模拟各种事件流

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)

猜你喜欢

转载自www.cnblogs.com/qq909283/p/9837428.html