monkeyrunner录制、回放

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:33   1601   0

注意:所有的脚本假若包含中文,要保存为utf-8。最好就不要包含中文。


录制的脚本,recoder.py脚本:

#Usage: monkeyrunner recorder.py
#recorder.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_recorder.py
com.android.monkeyrunner import MonkeyRunner as mr
com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)
#END recorder.py

cmd窗口中执行命令: monkeyrunner recoder.py,但要先启动模拟器。录制好的脚本,保存时要加 .mr 的后缀。

回放的脚本,playback.py脚本:
import sys
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(): www.2cto.com
file = sys.argv[1]
fp = open(file, ""r"")
device = MonkeyRunner.waitForConnection()
process_file(fp, device)
fp.close();
if __name__ == ""__main__"":
main()

启动的命令: monkeyrunner recorder.py “录制的脚本”
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP