APP稳定性测试神器之Monkey-自定义脚本测试

APP稳定性测试神器之Monkey-自定义脚本测试

常规Monkey测试执行的是随机的事件流,但如果只是想让Monkey测试某个特定场景这时候就需要用到自定义脚本了,Monkey支持执行用户自定义脚本的测试,用户只需要按照Monkey脚本的规范编写好脚本,存放到手机上,启动Monkey通过-f 参数调用脚本即可。

一、Monkey脚本API方法

LaunchActivity(pkg_name, cl_name):启动应用,参数:包名和Activity

Tap(x, y, tapDuration): 模拟一次手指单击事件,参数:x,y为控件坐标,tapDuration为点击的持续时间,此参数可省略

DispatchPress(keyName): 按键。参数: keycode。 详细的Android Keycode列表: http://blog.csdn.net/feizhixuan46789/article/details/16801429

RotateScreen(rotationDegree, persist): 旋转屏幕,参数:rotationDegree为旋转角度,1代表90度;persist表示旋转之后是否固定,0表示旋转后恢复,非0则表示固定不变。

DispatchFlip(true/false): 打开或者关闭软键盘

LongPress(2000): 长按2秒

PressAndHold(x, y, pressDuration): 模拟长按事件

DispatchString(input): 输入字符串

Drag(xStart, yStart, xEnd, yEnd, stepCount): 用于模拟一个拖拽操作

PinchZoom(x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End, stepCount): 模拟缩放手势

UserWait(sleepTime): 休眠一段时间

DeviceWakeUp(): 唤醒屏幕

RunCmd(cmd): 运行shell命令

DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFlags): 向指定位置,发送单个手势

DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFilags): 发送按键消息

LaunchInstrumentation(test_name,runner_name): 运行instrumentation测试用例。

DispatchTrackball: 模拟发送轨迹球事件

ProfileWait(5000): 等待5秒

StartCaptureFramerate(): 获取帧率

EndCaptureFramerate(input): 结束获取帧率

二、monkey自定义脚本实战(免费领取Python自动化学习资料  工具,面试宝典面试技巧,加QQ群,785128166,群内还会大佬技术交流)

1、测试案例

启动QQ,然后点击登录按钮进入登录页面,在登录界面输入用户名、密码,点击登录。

2、搭建模拟器环境

在itools或者夜神模拟器中,安装QQ最新版本,检查是否连接ADB,并获取App包名。

C:\Users\chang>adb devices

List of devices attached

127.0.0.1:56001 device

#获取App的包名,先启动qq并获取焦点

adb shell dumpsys window | findstr mCurrentFocus

3、脚本开发

Monkey脚本只能通过坐标的方式来定位点击和移动事件的屏幕位置,这里就需要提前获取坐标信息。获取坐标信息的方法很多,最简单的方法就是打开手机中的开发人员选项,打开“显示指针位置”。随后,在屏幕上的每次操作,在导航栏上都会显示坐标信息

Monkey脚本主要包含两部分,一部分是头文件信息,一部分是具体的monkey命令。

将下面脚本复制到e:\qq.txt文件中。

#头文件信息

type = raw events

count = 1

speed = 1.0

#启动测试

start data >>

#启动应用的Activity

LaunchActivity(com.tencent.mobileqq,com.tencent.mobileqq.activity.LoginActivity)

UserWait(2000)

Tap(379,822,200) #点击登录按钮

UserWait(2000)

Tap(249,278,1000) #点击用户名文本框

DispatchString(2878228288)#输入qq号码

UserWait(2000)

Tap(228,381,1000) #点击用户名输入框

DispatchString(zxw1234)

UserWait(2000)

Tap(248,539,1000) #点击登录按钮

UserWait(2000)

4、执行脚本

先将脚本文件发送到移动设备上,然后调用adb shell monkey指令执行脚本

#将编写好的脚本push到模拟器的/data目录下

adb push e:\qq.txt /data

#调用monkey指令执行脚本

adb -s 127.0.0.1:56001 shell monkey -f /data/qq.txt -v 1

5、查看执行结果

C:\Users\chang>adb -s 127.0.0.1:56001 shell monkey -f /data/qq.txt -v 1

:Monkey: seed=1588719579198 count=1

:IncludeCategory: android.intent.category.LAUNCHER

:IncludeCategory: android.intent.category.MONKEY

Replaying 0 events with speed 1.0

:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.tencent.mobileqq/.activity.LoginActivity;end

    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.tencent.mobileqq/.activity.LoginActivity } in package com.tencent.mobileqq

:Sending Touch (ACTION_DOWN): 0:(379.0,822.0)

:Sending Touch (ACTION_UP): 0:(379.0,822.0)

:Sending Touch (ACTION_DOWN): 0:(249.0,278.0)

:Sending Touch (ACTION_UP): 0:(249.0,278.0)

// Shell command input text zxw1234 status was 0

:Sending Touch (ACTION_DOWN): 0:(228.0,381.0)

:Sending Touch (ACTION_UP): 0:(228.0,381.0)

// Shell command input text zxw1234 status was 0

:Sending Touch (ACTION_DOWN): 0:(248.0,539.0)

:Sending Touch (ACTION_UP): 0:(248.0,539.0)

Events injected: 20

:Sending rotation degree=0, persist=false

:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0

## Network stats: elapsed time=13890ms (0ms mobile, 0ms wifi, 13890ms not connected)

// Monkey finished

欢迎关注作者,如果觉得写的还不错,就给点个赞、收藏(后续持续更新)。

猜你喜欢

转载自blog.csdn.net/PythonCS001/article/details/108321711