monkey自动化测试

.关于自动化测试
对于自动化测试我也是一知半解,在此只是抛砖引玉.所以难免有错误的地方,请大家发现后提出来,以免误导了大家.
据了解android上的自动化测试有monkey,monkeyrunner等.
1)其中monkey它会随机的模拟各种用户操作,并提供了丰富的选项参数,如各种事件的百分比.因此用来测试程序的稳定性,健壮程度很好用.
最简单的用法是:
adb shell monkey -p your.package.name 500
其中-p 是用于指定包名, 500是随机事件的次数
(详细用法可以参考 http://huchenqiang90.blog.163.com/blog/static/112500800201131945412107/)
2)而monkeyrunner则可以通过强大的脚本自定义模拟操作,所以应用范围会更广泛一些,比如某些功能压力测试
但是好像monkeyrunner需要pc端的支持,所以测试许多机器时不方便.

这里主要介绍monkey


2.monkey脚本
其实monkey也可指定脚本,实现制定操作,但是使用的人比较少,资料不多.
使用的方法是 adb shell monkey -f scriptfile 1 (-f是指定脚本 1是循环次数)
这里是源码里的一段demo
/**
* monkey event queue. It takes a script to produce events

* sample script format:
*      type= raw events
*      count= 10
*      speed= 1.0
*      start data >>
*      captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314,
*          0.06666667,0,0.0,0.0,65539,0)
*      captureDispatchKey(5113146,5113146,0,20,0,0,0,0)
*      captureDispatchFlip(true)
*      ...
*/
 
-----------------------------------------------------------------------------------


根据demo我们可以写出自己需要的脚本,其中模拟按键,模拟触摸是我们比较常用的函数.
DispatchPress(int keyCode) //模拟按键 键值见最后的附表
LaunchActivity(String pkg_name, String cl_name) //启动apk
UserWait(long sleeptime) //休眠,单位毫秒

DispatchPointer(long downTime, long eventTime, int action,
float x, float y, float pressure, float size, int metaState,  
float xPrecision, float yPrecision, int device, int edgeFlags) //模拟触摸
触摸函数的参数较多,这里是摘录的网上的注释
@action The kind of action being performed -- one of either{@link #ACTION_DOWN=0}, {@link #ACTION_MOVE=1}, {@link #ACTION_UP=2}, or{@link #ACTION_CANCEL=3}.
@param x The X coordinate of this event.
@param y The Y coordinate of this event.
@param pressure The current pressure of this event.  The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure), however values higher than 1 may be generated depending on the calibration of the input device.
@param size: A scaled value of the approximate size of the area being pressed touched with the finger. The actual value in pixels corresponding to the finger touch is normalized with a device specific range of values and scaled to a value between 0 and 1.
@param metaState The state of any meta / modifier keys that were in effect when the event was generated.
@param xPrecision The precision of the X coordinate being reported.
@param yPrecision The precision of the Y coordinate being reported.
@param deviceId The id for the device that this event came from.  An id of zero indicates that the event didn't come from a physical device; other numbers are arbitrary and you shouldn't depend on the values.
@param edgeFlags A bitfield indicating which edges, if any, where touched by this MotionEvent


这一个触摸的列子
DispatchPointer(0, 0, 0, 210, 300, 0,0,0,0,0,0,0)    -> 按下某点
DispatchPointer(0, 0, 1, 210, 300, 0,0,0,0,0,0,0)    -> 弹起




3.实例
  这个是实现自动拍照的一个脚本,供参考
# Start of Script
type= user
count= 8
speed= 1.0
start data >>
LaunchActivity(com.android.camera, com.android.camera.Camera)
UserWait(5000)
#
DispatchPress(27)
UserWait(300)
DispatchPress(27)
UserWait(300)
有了脚本后,我们可以配合preinstall.sh,实现开机自动运行monkey进行测试,只需要修改如下地方:
①修改第一句,把shell从busybox该成sh
#!/system/bin/sh
②增加执行monkey的语句
/system/bin/sh /system/bin/monkey -f /system/test.script 1
sleep 5
③手动在设置->安全->解锁里边把解锁方式改为"无",保证能看到效果







----------------------------------------------------------------------------------------------
附录1键值
KEYCODE_UNKNOWN=0;
KEYCODE_SOFT_LEFT=1;
KEYCODE_SOFT_RIGHT=2;
KEYCODE_HOME=3;
KEYCODE_BACK=4;
KEYCODE_CALL=5;
KEYCODE_ENDCALL=6;
KEYCODE_0=7;
KEYCODE_1=8;
KEYCODE_2=9;
KEYCODE_3=10;
KEYCODE_4=11;
KEYCODE_5=12;
KEYCODE_6=13;
KEYCODE_7=14;
KEYCODE_8=15;
KEYCODE_9=16;
KEYCODE_STAR=17;
KEYCODE_POUND=18;
KEYCODE_DPAD_UP=19;
KEYCODE_DPAD_DOWN=20;
KEYCODE_DPAD_LEFT=21;
KEYCODE_DPAD_RIGHT=22;
KEYCODE_DPAD_CENTER=23;
KEYCODE_VOLUME_UP=24;
KEYCODE_VOLUME_DOWN=25;
KEYCODE_POWER=26;
KEYCODE_CAMERA=27;
KEYCODE_CLEAR=28;
KEYCODE_A=29;
KEYCODE_B=30;
KEYCODE_C=31;
KEYCODE_D=32;
KEYCODE_E=33;
KEYCODE_F=34;
KEYCODE_G=35;
KEYCODE_H=36;
KEYCODE_I=37;
KEYCODE_J=38;
KEYCODE_K=39;
KEYCODE_L=40;
KEYCODE_M=41;
KEYCODE_N=42;
KEYCODE_O=43;
KEYCODE_P=44;
KEYCODE_Q=45;
KEYCODE_R=46;
KEYCODE_S=47;
KEYCODE_T=48;
KEYCODE_U=49;
KEYCODE_V=50;
KEYCODE_W=51;
KEYCODE_X=52;
KEYCODE_Y=53;
KEYCODE_Z=54;
KEYCODE_COMMA=55;
KEYCODE_PERIOD=56;
KEYCODE_ALT_LEFT=57;
KEYCODE_ALT_RIGHT=58;
KEYCODE_SHIFT_LEFT=59;
KEYCODE_SHIFT_RIGHT=60;
KEYCODE_TAB=61;
KEYCODE_SPACE=62;
KEYCODE_SYM=63;
KEYCODE_EXPLORER=64;
KEYCODE_ENVELOPE=65;
KEYCODE_ENTER=66;
KEYCODE_DEL=67;
KEYCODE_GRAVE=68;
KEYCODE_MINUS=69;
KEYCODE_EQUALS=70;
KEYCODE_LEFT_BRACKET=71;
KEYCODE_RIGHT_BRACKET=72;
KEYCODE_BACKSLASH=73;
KEYCODE_SEMICOLON=74;
KEYCODE_APOSTROPHE=75;
KEYCODE_SLASH=76;
KEYCODE_AT=77;
KEYCODE_NUM=78;
KEYCODE_HEADSETHOOK=79;
KEYCODE_FOCUS=80;//*Camera*focus
KEYCODE_PLUS=81;
KEYCODE_MENU=82;
KEYCODE_NOTIFICATION=83;
KEYCODE_SEARCH=84;
KEYCODE_MEDIA_PLAY_PAUSE=85;
KEYCODE_MEDIA_STOP=86;
KEYCODE_MEDIA_NEXT=87;
KEYCODE_MEDIA_PREVIOUS=88;
KEYCODE_MEDIA_REWIND=89;
KEYCODE_MEDIA_FAST_FORWARD=90;
KEYCODE_MUTE=91;




附录2函数列表
DispatchPointer(long downTime,  long eventTime, int action,  
  
    float x, float y, float pressure, float size, int metaState,  
  
    float xPrecision, float yPrecision, int device, int edgeFlags)  
  
DispatchTrackball(long downTime,  long eventTime, int action,  
  
    float x, float y, float pressure, float size, int metaState,  
  
    float xPrecision, float yPrecision, int device, int edgeFlags)  
  
DispatchKey(long downTime, long eventTime, int action, int code,  
  
    int repeat, int metaState, int device, int scancode)  
  
DispatchFlip(boolean keyboardOpen)  
  
DispatchPress(int keyCode)  
  
LaunchActivity(String pkg_name, String cl_name)  
  
UserWait(long sleeptime)  
  
LongPress()
http://www.linuxidc.com/Linux/2011-10/45152.htm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值