# 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 mrfrom com. Android. monkeyrunner. Recorder import monkeyrecorder as recorderdevice = 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 sysfrom 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 cocould be passed to the underlying # command. # lookup table to map command strings to functions that implement that # command. export _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) failed T: Print 'unable to parse options' continue if cmd not in progress _map: Print 'unknown command: '+ cmd continue pai_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 ()