一.常见的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)