App压力测试——MonkeyRunner

一.常见的MonkeyRunner API

1.alert(警告框)

void alert(string message,string title,string okTitle)

如这样一个python脚本

#coding:utf-8
from com.android monkeyrunner import MonkeyRunner
MonkeyRunner.alert("Hello World","This is title","OK")

执行脚本命令
使用monkeyrunner就要用monkeyrunner命令来执行这个脚本,而不是我们以前的pyhton filename.py 命令

monkeyrunner filename.py

2.waitForConnection(等待设备连接)

注意:有多个device id时,需要指明设备名

waitForConnection(float timeout,string deviceid)
#float timeout超时时间
#string deviceid设备id

3.drag(拖动)

drag(tuple start,tuple end,float duration,integer steps)
#tuple start拖动的起点
#tuple end拖动的终点
#float duration手势持续的时间
#integer steps拖动的过程分几步来实现(插值点的步数,默认为10)

4.press(按键)

press(string keycode,dictionary type)
#string keycode按键值,如回车键为66
#dictionary type按键类型,如UP,Down,DOWN_AND_UP

5.startActivity(启动应用)

startActivity(package+'/'+activity)

6.touch(点击)

touch(integer x,integer y,integer type)
#integer x,integer y 指x和y的坐标
#integer type触摸类型,如UP,Down,DOWN_AND_UP

7.type(输入)

type(string message)

8.take Snapshot(截屏)

MonkeyImage takeSnapshot()

9.sameAs(图像对比)

boolean sameAs(MonkyeyImage other,float percent)
#MonkyeyImage other需要对比的图像名称
#float percent对比相似度的百分比

10.writetoFile(保存图像文件)

void writetoFile(string path,string format)
#string path指定图像存储的路径
#string format指定图像的类型,如jpg,png

脚本示例:

#-*- coding:utf-8 -*-
from com.android.monkeyrunner import MonkeyRunner,MonekyDevice,MonkeyImage
#连接设备,超时时间设置3秒
device = MonkeyRunner.waitForConnection(3,"emulator-5554")
#启动APP
device.startActivity("com.example.zhangjian.minibrowser2/com.example.zhangjian.minibrowser2.myapplication.MiniActivity")
MonkeyRunner.sleep(2)
#点击搜索框,输入框的位置用uiautomatorviewer工具可查看
device.touch(100,100,"DOWN_AND_UP")
MonkeyRunner.sleep(1)
#输入查询词
device.type("text")
MonkeyRunner.sleep(1)
#点击回车键
device.press("KEYCODE_ENTER","DOWN_AND_UP")
MonkeyRunner.sleep(1)
#点击搜索按钮
device.touch(400,100,"DOWN_AND_UP")
MonkeyRunner.sleep(6)
#截图
image = device.takeSnapshot()
image.writeToFile("./test.png",'png')
#点击清除按钮
device.touch(300,100,"DOWN_AND_UP")
MonkeyRunner.sleep(3) 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值