Android automated Test Monkeyrunner recording and playback scripts (11)Category: Automated test Android Automation 2013-02-22 10:57 7346 people read reviews (2) favorite reports Androidandroidandroidmonkeyrecordermonkeyrunnermonkeyrunnermonkeyrunner
For Monkeyrunner, some people might think that since it is an Android automated test that cannot be separated from a test script, can we record a test script and the answer is yes.
Let's start by looking at the following monkeyrecoder.py script:
[HTML]View Plaincopy
- #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
First, connect the Android device or emulator that you have turned on in debug mode, and then run the script above, such as executing the command in the cmd window: Monkeyrunner monkeyrecoder.py
After you execute the following code, the program that will run the recording script:
#press exportaction to save recorded scrip to a file
#example of Result:
#press| {"Name" ":" "Menu" "," "Type" ":" "Downandup" ",}
#touch| {"" X "": 175, "" Y "": "," "" "" ":" "Downandup" ",}
#type| {"Message" ":" "",}
=================================================
This script requires another Monkeyrunner script to interpret the execution. monkeyplayback.py
[HTML]View Plaincopy
- #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 was 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 to existence) that specifies the
- # Arguments for the command. In most cases, this directly maps to the
- # keyword argument dictionary that could being passed to the underlying
- # command.
- # Lookup table to map command strings to functions this 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"
Monkeyrunner recording and playback scripts for Android automated testing