MonkeyRunner 录制和播放脚本,及碰到的一些问题整理
自己的SDK位置:比如d:\android_sdk_windows,下文都当做$SDK
1.$SDK\tools下面如果没有monkeyrunner可能是因为SDK版本过低。最好升级到2.3
2.$SDK\tools\lib一定要存在。
3.系统的环境变量里面一定要指向$SDK\tools
4.建议下载eclipse ,在eclipse里面点击DDMS监控monkeyrunner的操作
5.建议下载最新版本adb.exe,解决有的模拟器或者真机连接电脑,无法使用adb,一输入adb.exe回车就提示error ,killing
下载连接:http://android.googlecode.com/issues/attachment?aid=8293722374312378755&name=adb.exe&token=b5fbe373d5474fb0ee49239fa8accd03
6
在使用monkeyrunner之前,请大家到这里 http://www.skycn.com/search.php?ss_name=activepython&sf=index 下载activepython 并安装好它。
7.$SDK\tools\monkeyrunner.bat 运行之后,如果提示如下:
Exception in thread "main" java.lang.NoClassDefFoundError: com/android/chimpchat/ChimpChat
at com.android.monkeyrunner.MonkeyRunnerStarter.<init>(MonkeyRunnerStarter.java:60)
at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:188)
Caused by: java.lang.ClassNotFoundException: com.android.chimpchat.ChimpChat
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
说明SDK中缺少了 ChimpChat.jar,可以去 http://code.google.com/p/aster/downloads/list 下载一个最新的aster 的压缩包。这个aster 的意思是 Android System Testing Environment and Runtime. 在这个压缩包内有 aster\dist\jar\chimpchat.jar 请将这个jar包复制到你的SDK\tools\lib 目录。
#Usage: monkeyrunner recorder.py
#recorder.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_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()