Monkeyrunner是什么
monkeyrunner是一个Android自动化测试工具。它提供一个API,运用该API编写的程序可以不用通过Android代码来直接控制Android设备和模拟器,我们可以写一个Python程序对android应用程序或测试包进行安装,运行,发送模拟击键,对用户界面进行截图并将截图存储在workstation上等操作。monkeyrunner工具的主要设计目的是用于application /framework层上的应用程序和设备,我们当然也可以将其用于其它目的。
Monkeyrunner遇到forceclose就会停止运行,通过logcat可进一步确定forceclose的原因。
monkeyrunner的截图功能可以比较预定结果和运行结果是否一致,来测试程序的运行结果。
为什么需要monkeyrunner
monkey也能产生如触摸,击键,移动屏幕等事件,但这些事件都是随机的,不可控的。典型使用方式是用于软件压力测试。adb shell monkey -p com.jrdcom.JrdSetupWizard --throttle 500 -v500
Instrumentation具有强大的测试功能,通过它可以编写Junit类似的自动化测试代码,但编写比较费时费力,相对较复杂。
Monkeyrunner既能按照用户的需求来产生特定序列的特定事件,还能截屏保存和比较图片,编写简单。借助于python,也能实现强大的功能。
运行monkeyrunner
【在命令行里执行,windows command或者 Linux terminal】
方法一
monkeyrunner.bat #windows进入monkeyrunner模式
monkeyrunner #linux进入monkeyrunner模式
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
device = MonkeyRunner.waitForConnection()
device.touch(200,300,MonkeyDevice.DOWN_AND_UP)
也可如下写
from com.android.monkeyrunner importMonkeyRunner as mr
from com.android.monkeyrunner importMonkeyDevice as md
device = md.waitForConnection()
device.touch(200,300,md.DOWN_AND_UP)
方法二
monkeyrunner myrunner.py(在命令行执行,myrunner.py是用户写的自动化测试的脚本文件)
或者monkeyrunner.bat myrunner.py
monkeyrunner脚本
from com.android.monkeyrunner importMonkeyRunner,MonkeyDevice #引入monkeyrunner模块
device = MonkeyRunner.waitForConnection() #链接到设备
device.wake() #唤醒屏幕
#device.startActivity(component='com.jrdcom.JrdSetupWizard/.SelectLanguageActivityAlias') #启动Activity
#image = device.takeSnapshot() #获取屏幕截图
#image = image.getSubImage((100,100,200,200)) #截取图像的部分
#image.writeToFile('/data/qinyu/abc.png','png') #以png格式保存图像到/data/qinyu/abc.png
#device.press('KEYCODE_HOME') #按HOME键
interval_time = 1 #设定命令间隔时间
device.drag((100,500),(100,100),1.0,3) #滑动屏幕
MonkeyRunner.sleep(interval_time) #为命令执行添加间隔时间
device.touch(200,485,MonkeyDevice.DOWN_AND_UP) #点击屏幕(x:200,y:485)
MonkeyRunner.sleep(interval_time)
device.touch(344,766,MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(3)
……
recorder
Recoder是一个java开发的图形化的monkeyrunner脚本生成工具,
可极大程度提高monkeyrunner脚本的编写速度
启动方法一
from com.android.monkeyrunner importMonkeyRunner asmr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)
启动方法二
monkeyrunner monkey_recorder.py 通过脚本启动record工具,windowns上需要将monkey_recorder.py放在sdk\tools\目录下,否则报错
然后将动作信息保存成aciton.txt
monkeyrunner monkey_playback.py action.txt 执行recordr生成的monkeyrunner脚本
WAIT|{'seconds':10.0,}
TYPE|{'message':'可乐',} #中文输入有问题
DRAG|{'start':(288,1024),'end':(288,204),'duration':1.0,'steps':10,}
TOUCH|{'x':524,'y':218,'type':'downAndUp',}
PRESS|{'name':'BACK','type':'downAndUp',}
recorder界面
ThemonkeyrunnerAPI
monkeyrunnerAPI都包含在com.android.monkeyrunner包中 :
MonkeyRunner:monkeyrunner的工具类,用于连接monkeyrunner到设备,以及为调试信息输入创建界面等
MonkeyDevice:表示一个设备或模拟器。用于向目标设备发送模拟事件。
MonkeyImage:代表着一个屏幕捕获图像。这个类提供一些方法来捕捉屏幕截图、将位图转换成各种格式,比较两个MonkeyImage对象和写作一个图像文件。
MonkeyRunner
void sleep(float seconds)
Pausesthe current program for the specified number of seconds.
Monkey DevicewaitForConnection(float timeout, string deviceId)
Triesto make a connection between themonkeyrunner backend and the specified device or emulator.
MonkeyDevice
void drag(tuple start, tupleend, float duration, integer steps)
Simulates a drag gesture (touch, hold, and move) onthis device's screen.
void press(string name,dictionary type)
Sendsthe key event specified by type to the key specified bykeycode.
void touch(integer x,integer y, integer type)
Sends atouch event specified by type to the screen location specified by x and y.
void startActivity(string uri,string action, stringdata, string mimetype, iterablecategories dictionary extras, componentcomponent, flags)
Startsan Activity on this device by sending an Intent constructed from the suppliedarguments.
void type(string message)
Sendsthe characters contained in message to this device, as if they had been typedon the device's keyboard. This is equivalent to calling press() for eachkeycode in message using thekey event type DOWN_AND_UP.
MonkeyImage takeSnapshot()
Capturesthe entire screen buffer of this device, yielding aMonkeyImage object containing a screen capture of thecurrent display.
void wake ()
Wakesthe screen of this device.
MonkeyImage
MonkeyImage getSubImage(tuple rect)
Createsa new MonkeyImage object from arectangular selection of the current image.Eg:
Boolean sameAs(MonkeyImage other, float percent)
Comparesthis MonkeyImage object to anotherand returns the result of the comparison. The percent argument specifies thepercentage difference that is allowed for the two images to be"equal".
Void writeToFile(string path, string format)
Writesthe current image to the file specified by filename, in the format specified byformat.