Monkeyrunner recording and playback scripts for Android automated testing

Source: Internet
Author: User

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
    1. #usage:monkeyrunner recorder.py
    2. #recorder.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_recorder.py
    3. Com.android.monkeyrunner Import Monkeyrunner as Mr
    4. Com.android.monkeyrunner.recorder Import Monkeyrecorder as Recorder
    5. device = mr.waitforconnection ()
    6. Recorder.start (device)
    7. #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
  1. #usage:monkeyrunner playback.py "MyScript"
  2. #playback.py http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_playback.py
  3. Import Sys
  4. Com.android.monkeyrunner Import Monkeyrunner
  5. # The format of the file we are parsing is very carfeully constructed.
  6. # each line corresponds to a single command. The line was split into 2
  7. # Parts with a |  Character. Text to the left of the pipe denotes
  8. # which command to run. The text to the right of the pipe is a python
  9. # dictionary (it can be evaled to existence) that specifies the
  10. # Arguments for the command. In most cases, this directly maps to the
  11. # keyword argument dictionary that could being passed to the underlying
  12. # command.
  13. # Lookup table to map command strings to functions this implement that
  14. # command.
  15. Cmd_map = {
  16. "TOUCH": Lambda Dev, Arg:dev.touch (**arg),
  17. "DRAG" ": Lambda Dev, Arg:dev.drag (**arg),
  18. "Press": Lambda Dev, arg:dev.press (**arg),
  19. "TYPE": Lambda Dev, Arg:dev.type (**arg),
  20. "WAIT" ": Lambda Dev, arg:MonkeyRunner.sleep (**arg)
  21. }
  22. # Process A single file for the specified device.
  23. def process_file (FP, device):
  24. For line in FP:
  25. (cmd, rest) = Line.split ("" | "" )
  26. Try
  27. # Parse The Pydict
  28. rest = eval (rest)
  29. Except
  30. Print "Unable to parse options"
  31. Continue
  32. If cmd not in Cmd_map:
  33. print "" Unknown Command: "" + cmd
  34. Continue
  35. Cmd_map[cmd] (device, rest)
  36. def main ():
  37. file = sys.argv[1]
  38. fp = open (file, "R")
  39. device = monkeyrunner.waitforconnection ()
  40. Process_file (FP, device)
  41. Fp.close ();
  42. If __name__ = = "" __main__ "":
  43. Main ()

=================================================

Usage:monkeyrunner playback.py "MyScript"

Monkeyrunner recording and playback scripts for Android automated testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.