adb进阶三 —— Monkey (2)

adb进阶三 —— Monkey (2)

前言

因为原生的monkey,是一系列的随机事件,很多时候事满足不了我们自定义场景需求的,比如只想验证APP的登录的稳定性,使用原生的adb shell monkey貌似做不到,这边就需要自定义脚本来实现

Monkey API

API 描述
LaunchActivity(pkg_name, cl_name) 启动应用的Activity。参数:包名和启动的Activity
Tap(x, y, tapDuration) 模拟一次手指单击事件。参数:x,y为控件坐标,tapDuration为点击的持续时间,此参数可省略
DispatchPress(keycode) 按键。参数: keycode
RotateScreen(rotationDegree, persist) 旋转屏幕。 参数:rotationDegree为旋转角度, e.g. 1代表90度;persist表示旋转之后是否固定,0表示旋转后恢复,非0则表示固定不变
DispatchFlip(true/false) 打开或者关闭软键盘
LongPress() 长按
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() 唤醒屏幕
PowerLog(power_log_type, test_case_status) 模拟电池电量信息
WriteLog() 将电池信息写入sd卡
RunCmd(cmd) 运行shell命令
DispatchPointer(long downTime, long eventTime, int action, loat 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) 发送键值
LaunchInstrumentation(test_name,runner_name) 运行一个instrumentation测试用例
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) 模拟发送轨迹球事件
ProfileWait 等待5秒
StartCaptureFramerate() 获取帧率
EndCaptureFramerate(input) 结束获取帧率

点击事件为例:

1
DispatchPointer(long downTime,  long eventTime, int action, loat x, float y, float pressure, float size, int metaState,  float xPrecision, float yPrecision, int device, int edgeFlags)

参数很多,只需要关注actionxy,对于参数action值为0代表按下(KeyDown),1代表弹起(KeyUp)。如果使用这个方法实现点击事件,这个方法就应该成对出现,先0再1。

  • long downTime:键最初被按下时间
  • long eventTime:事件发生时间
  • int action:动作ACTION_DOWN=0,ACTION_UP=1,ACTION_MULTIPLE=2
  • float x:x坐标
  • float y:y坐标
  • float pressure:当前事件的压力,值为0~1
  • float size:触摸的近似值,范围为0~1
  • int metaState:当前按下的meta键的标识
  • float xPrecision:x坐标精确值
  • float yPrecision:y坐标精确值
  • int device:事件来源,范围0~x,0表示不来自物理设备
  • int edgeFlags:坐标是否超出了屏幕范围

Android键盘事件,keycode

Monkey脚本

和自动化测试时候差不多实现,打开首页-点击用户名-输入用户名-点击密码-输入密码-点击登录,相应的,也就是只需要获取用户名和密码以及确定按键的坐标,传到DispatchPointer即可,获取坐标可以使用uiautomatorviewer.bat挺简单的,直接上脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 头部
type= user
count= 1
speed= 1.0
UserWait(200)
start data >>

# 打开app登录页
LaunchActivity(com.android.testapp,com.android.testapp.MainActivity)
# 休眠
UserWait(10000)
# 点击用户名文本框(460,751)为坐标
DispatchPointer(10,10,0,460,751,1,1,-1,1,1,0,0)
DispatchPointer(10,10,1,460,751,1,1,-1,1,1,0,0)
UserWait(5000)
# 输入用户名
DispatchString(monkeytest)
DispatchFlip(false)
UserWait(2000)
# 点击密码文本框(490,867)为坐标
DispatchPointer(10,10,0,490,867,1,1,-1,1,1,0,0)
DispatchPointer(10,10,1,490,867,1,1,-1,1,1,0,0)
UserWait(5000)
# 输入密码
DispatchString(123456)
DispatchFlip(false)
UserWait(2000)
# 点击确定
Tap(541,1096)

保存为monkey_test.txt文件,上传到android设备:adb push monkey_test.txt /mnt/sdcard/
执行脚本:adb shell monkey -f /mnt/sdcard/monkey_test.txt -v 1

完~

文章目录
  1. 前言
  2. Monkey API
  3. Monkey脚本
|