Android自动化测试


Android  Sdk给我们提供了Monkeyrunner(\sdk\tools\中)自动化测试工具。
Monkey程序由 Android 系统自带 ,使用Java语言写成,在Android文件系统中的存放路径是:/system/framework/monkey.jar。

Monkey是Android中的一个 命令行工具 ,可以运行在模拟器里或实际设备中。 Monkey 测试是 Android 平台下自动化测试的一种快速有效的手段,通过 Monkey 工具可以 模拟用户触摸屏幕、滑动轨迹球、按键等操作来对模拟器或者手机设备上的软件进行压力测试 ,检测该软件的 稳定性、健壮性 。它的 原理 是向系统发送 伪随机的用户事件流(如按键输入、触摸输入、手势输入等),实现对正在开发的应用程序进行压力测试。
MonkeyRunner: 提供API定义特定事件和命令,完成模拟事件和截图操作。
MonkeyScript: Monkey识别的命令集合,完成重复固定操作。

目的
提高产品稳定性
提高产品留存率

时间
首轮功能测试通过后的夜间进行

压力测试结果需要关注
Crash:应用程序崩溃
ANR:Application Not Responding

Monkey
测试流程
1)连接手机和电脑,测试连接成功命令: adb devices
2)安装APP:命令 adb install myApp.apk
3)发送压力测试指令:adb shell monkey 1000
1000指随机的点击操作。
Event injected 1000,出现表示完成了1000个事件操作。小于1000说明测试有问题
4)获取包名: adb logcat | grep START
最后一条 cmp=com.wzy.myapplication/.MainActivity
包名:com.wzy.myapplication
5)给指定包打压力:adb shell monkey -p com.wzy.myapplication 1000
高级参数
throttle :指定事件间隔
adb shell monkey --throttle 1000 1000 执行1000个事件,每个事件间隔1s
seed:指定随机生成数seed值
adb shell monkey -s 100 1000 定义seed值为100,复现操作相同序列,产生相同操作结果
pct-touch 设置触摸事件百分比
adb shell monkey -v --pct-touch 80 1000 触摸事件占操作事件的80%
-v表示显示的操作的过程
pct-motion 设置动作事件占百分比
adb shell monkey -v -p com.wzy.myapplication -- pct-touch 80 --pct-motion 10 1000
触摸事件占操作事件的80%;动作事件占10%;剩余10%执行随机操作
pct-trackball 设置轨迹球事件占百分比
pct-nav 设置基本导航事件(上、下、左、右键)占百分比
pct-majornav 设置基本导航事件( 中间键、返回键、菜单键)占百分比
pct-syskeys 设置系统导航事件(Home键、back键、拨号键)占百分比
pct-appswitch 设置启动Activity事件占百分比
pct-anyevent 设置不常用事件占百分比
ignore-crashes 忽略崩溃和异常
当app有crash和异常时继续往下执行
adb shell monkey -p com.example.wzy.myapplication --ignore-crashes 1000
ignore-timeouts 忽略超时事件
当app有ANR时继续往下执行

Monkey Script(执行过程中不能实现截屏)
命令:adb shell monkey -f<scriptfile> <event count>
轨迹球事件: DispatchTrackball(long downtime,long enentide, int action,float x,float y,float pressure,float size,int metaatate,float xprecision,float yprecision,int device,int edgeflage)
action 0:按下 1:弹起 x\y坐标点
事件: DispatchPointer(long downtime,long enentide, int action,float x,float y,float pressure,float size,int metaatate,float xprecision,float yprecision,int device,int edgeflage)
输入字符串: DispatchString(String s)
启动应用: LaunchActivity(package,Activity);
等待时间UserWait(1000);//毫秒
按下键值: DispatchPress(int keycode); //66回车键


1) 创建编写Monkey Script文件(上面4行动):
typ=user
count=10
speed=1.0
start data >>

LaunchActivity(com.example.wzy.myapplication,com.example.wzy.myapplication.RecyclerViewActivity)
UserWait(2000)
DisPatchPointer(10,10,0,100,100,1,1,-1,1,1,0,0) --0,100,100,
DisPatchPointer(10,10,1,100,100,1,1,-1,1,1,0,0)
UserWait(2000)
2)将文件保存为xx. script 文件
3)将文件发送到设备
发送电脑monkey.script文件到手机命令:
adb push xx.script /data/local/tmp/
4)查看文件是否在设备存在
adb shell
cd /data/local/tmp/
ls
依次执行上面命令,查看/data/local/tmp/文件列表中有没有xx.script文件
5)执行测试命令
adb shell -f /data/local/tmp/xx.script 10

MonkeyRunner(执行过程中实现截屏)
API
MonkeyRunner:完成设备或模拟器连接。
MonkeyDevice:提供安装卸载应用,发送模拟事件
MonkeyImage:完成图像保存及对比操作

MonkeyRunner压力测试结果
多设备控制
功能测试
回归测试

API
警告框:alert
void alert(String Message,String title,String okTitile)
等待设备连接
waitForConnection(float timeout,string deviceid)
拖动
drag(tuple start,stuple end,float duration,integer steps)
按键
press(string keycode,dictionary type)
启动
startActivity(package+/+activtyname);
点击
touch(integer x,integer y,integer type)
截屏
MonkeyImage takeSnapShot()
void WritetoFile(string path,string format);

1) 创建编写MonkeyRunner文件(上面4行动):
#!/usr/bin/python
#-*- UTF-8 -*-
from com.android.monkeyrunner import MonkeyRunner
MonkeyRunner.alert("Hello MonkeyRunner","title","ok")


补充:
android :exported= "true"//允许这个app的activity被外界的命令行调用,附则外界的命令行不能启动该App Activity
< activity android :name= "com.example.wzy.wzyapplication.MainActivity"
android:exported="true" >
< intent-filter >
< action android :name= "android.intent.action.MAIN" />
< category android :name= "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值