- Android Monkeyrunner Test
-
关于Android自动化测试,研究了Monkey,Robotium 这次来看下 Monkeyrunner.
这里简单记录下实践过程,Monkeyrunner 需要用Python来编写,对于曾未学过Python的童鞋来说也没关系,因为Python属于比较好学的一门脚本语言.笔者也未曾学过Python,但有其他编程基础如:PHP,Java,Peal,还是能够很好理解Python的。
一、monkeyrunner 介绍
monkeyrunner 提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.
二、monkeyrunner 测试类型
多设备控制、功能测试、回归测试
三、实例(测试MyAndroid.apk)
1. 新建一个 monkeyrunnerTest.py
# Import the monkey runner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Installs the Android package
device.installPackage("./MyAndroid.apk")
# Runs the component
device.startActivity(component = 'com.luwenjie.android/.MyAndroidActivity')
#Presses the Menu button
device.press('KEYCODE_MENU','DOWN_AND_UP')
#Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shot1.png','png')
2. 运行在 %Android_HOME%\tools 目录下运行一下命令
monkeyrunner monkeyrunnerTest.py
四、API参考
MonkeyRunner:http://developer.android.com/tools/help/MonkeyRunner.html
MonkeyDevice:http://developer.android.com/tools/help/MonkeyDevice.html
MonkeyImage:http://developer.android.com/tools/help/MonkeyImage.html
五、录制模式
#Usage: monkeyrunner recorder.py
#recorder.py
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)
#END recorder.py
#Press ExportAction to save recorded scrip to a file
#Example of result:
#PRESS|{'name':'MENU','type':'downAndUp',}
#TOUCH|{'x':190,'y':195,'type':'downAndUp',}
#TYPE|{'message':'',}
#Usage: monkeyrunner playback.py "myscript"
#playback.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_playback.py
import sys
from com.android.monkeyrunner import MonkeyRunner
# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command. The line is split into 2
# parts with a | character. Text to the left of the pipe denotes
# which command to run. The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command. In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command.
# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
'TOUCH': lambda dev, arg: dev.touch(**arg),
'DRAG': lambda dev, arg: dev.drag(**arg),
'PRESS': lambda dev, arg: dev.press(**arg),
'TYPE': lambda dev, arg: dev.type(**arg),
'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
}
# Process a single file for the specified device.
def process_file(fp, device):
for line in fp:
(cmd, rest) = line.split('|')
try:
# Parse the pydict
rest = eval(rest)
except:
print 'unable to parse options'
continue
if cmd not in CMD_MAP:
print 'unknown command: ' + cmd
continue
CMD_MAP[cmd](device, rest)
def main():
file = sys.argv[1]
fp = open(file, 'r')
device = MonkeyRunner.waitForConnection()
process_file(fp, device)
fp.close();
if __name__ == '__main__':
main()
作者:luckyapplelwj
-
id="iframeu2368767_0" src="http://pos.baidu.com/tcbm?rdid=2368767&dc=2&di=u2368767&dri=0&dis=0&dai=6&ps=3656x195&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1460012459077&ti=Android%20Monkeyrunner%20Test%20-%20Android%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0_%E6%89%8B%E6%9C%BA%E5%BC%80%E5%8F%91%20-%20%E7%BA%A2%E9%BB%91%E8%81%94%E7%9B%9F&ari=1&dbv=2&drs=1&pcs=1349x645&pss=1349x3757&cfv=0&cpl=4&chi=1&cce=true&cec=GBK&tlm=1441611104<u=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201207%2F140284.html&ecd=1&psr=1366x768&par=1366x706&pis=-1x-1&ccd=24&cja=false&cmi=6&col=zh-CN&cdo=-1&tcn=1460012460&qn=ec22ecaaeabc1d47&tt=1460012459035.1007.1068.1069" width="650" height="100" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">