首页 > 系统 > Android > 正文

Android自动化测试

2019-11-09 15:53:44
字体:
来源:转载
供稿:网友
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 RespondingMonkey测试流程1)连接手机和电脑,测试连接成功命令:adb devices2)安装APP:命令adb install myApp.apk3)发送压力测试指令:adb shell monkey 10001000指随机的点击操作。Event injected 1000,出现表示完成了1000个事件操作。小于1000说明测试有问题4)获取包名: adb logcat | grep START最后一条 cmp=com.wzy.myapplication/.MainActivity包名:com.wzy.myapplication5)给指定包打压力:adb shell monkey -pcom.wzy.myapplication 1000高级参数throttle :指定事件间隔adb shell monkey--throttle 1000 1000 执行1000个事件,每个事件间隔1sseed:指定随机生成数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 1000ignore-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=usercount=10speed=1.0start 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文件到手机命令:adbpush 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 10MonkeyRunner(执行过程中实现截屏)APIMonkeyRunner:完成设备或模拟器连接。MonkeyDevice:提供安装卸载应用,发送模拟事件MonkeyImage:完成图像保存及对比操作MonkeyRunner压力测试结果多设备控制功能测试回归测试API警告框:alertvoid 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 MonkeyRunnerMonkeyRunner.alert("Hello MonkeyRunner","title","ok")补充:android:exported="true"//允许这个app的activity被外界的命令行调用,附则外界的命令行不能启动该App Activity<activityandroid:name="com.example.wzy.wzyapplication.MainActivity"android:exported="true"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表