Android自动化测试之MonkeyRunner录制和回放脚本(十一)
2015-06-08 13:05
323 查看
对于MonkeyRunner,有些人可能会想,既然是Android自动化测试,离不开测试脚本,那么,我们可不可以录制测试脚本呢,答案是可以的。
我们先看看以下monkeyrecoder.py脚本:
[html] view plaincopyprint?- #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
#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
首先,连接你已经打开调试模式的ANDROID设备或模拟器,然后运行上面的脚本,例如在cmd窗口中执行命令: monkeyrunner monkeyrecoder.py
执行下面的代码后,将运行录制脚本的程序:
#Press ExportAction to save recorded scrip to a file
#Example of result:
#PRESS|{""name"":""MENU"",""type"":""downAndUp"",}
#TOUCH|{""x"":180,""y"":175,""type"":""downAndUp"",}
#TYPE|{""message"":"""",}
=================================================
这种脚本需要另外一个monkeyrunner的脚本来解释执行。monkeyplayback.py
[html] view plaincopyprint?- #Usage: monkeyrunner playback.py "myscript"
- #playback.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_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():
- file = sys.argv[1]
- fp = open(file, ""r"")
- device = MonkeyRunner.waitForConnection()
- process_file(fp, device)
- fp.close();
- if __name__ == ""__main__"":
- main()
#Usage: monkeyrunner playback.py "myscript" #playback.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_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(): file = sys.argv[1] fp = open(file, ""r"") device = MonkeyRunner.waitForConnection() process_file(fp, device) fp.close(); if __name__ == ""__main__"": main()
=================================================
Usage:monkeyrunner playback.py "myscript"
阅读更多相关文章推荐
- Android自动化测试之MonkeyRunner录制和回放脚本(十一)
- Android自动化测试之MonkeyRunner录制和回放脚本
- Android自动化测试之MonkeyRunner录制和回放脚本
- Android自动化测试之MonkeyRunner录制和回放脚本(四)
- 【转】Android自动化测试之MonkeyRunner录制和回放脚本(四)
- android自动化测试之--MonkeyRunner录制和回放脚本(四)
- Android自动化测试之MonkeyRunner录制和回放脚本(四)
- [转] Android自动化测试之MonkeyRunner录制和回放脚本(四)
- Android自动化测试之MonkeyRunner录制和回放脚本
- Android自动化工具Monkeyrunner使用(七) —— 录制、回放脚本
- python使用monkeyrunner对录制脚本进行回放脚本
- monkeyrunner脚本的录制和回放
- 【monkeyrunner】monkeyrunner脚本录制和回放
- monkeyrunner之monkeyrecorder录制回放脚本
- MonkeyRunner之MonkeyRecorder录制回放脚本
- 【Android测试】【第十节】MonkeyRunner—— 录制回放
- Android自动化测试之Monkeyrunner 脚本编写
- Monkeyrunner 录制脚本&回放
- 【Android测试】MonkeyRunner之录制回放
- Monkeyrunner脚本的录制与回放