Android Monkeyrunner Test

Source: Internet
Author: User

For Android automated testing, we have studied Monkey and Robotium. Here we will look at Monkeyrunner.
 
Here is a simple record of the practice process. Monkeyrunner needs to be written in Python. It doesn't matter if you have never learned Python, because Python is a good scripting language. I have never learned Python, but I still can understand Python well if I have other programming basics such as PHP, Java, and Peal.
 
1. Introduction to monkeyrunner
Monkeyrunner provides an API to control Android devices and Simulators in addition to Android code.
 
Ii. monkeyrunner test Type
Multi-Device Control, function testing, and regression testing
 
Three steps (test myandroid.apk)
1. Create a monkeyrunnerTest. py
# Import the monkey runner modules used by this program
From com. android. monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
 
# Connects to current device, returning a MonkeyDevice object
Device = MonkeyRunner. waitForConnection ()
 
# INSTALLThe Android package
Device. installPackage ("./MyAndroid.apk ")
 
# Runs the component
Device. startActivity (component = 'com. luwenjie. android/. myandroidactive ')
 
# Presses the Menu button
Device. press ('keycode _ MENU ', 'down _ AND_UP ')
 
# Takes a screenshot
Result = device. takeSnapshot ()
 
# Writes the screenshot to a file
Result. writeToFile ('./shot1.png', 'png ')
2. Run the command in the % Android_HOME % \ tools directory.
Monkeyrunner monkeyrunnerTest. py

 
Iv. API reference
MonkeyRunner: http://developer.android.com/tools/help/MonkeyRunner.html
MonkeyDevice: http://developer.android.com/tools/help/MonkeyDevice.html
MonkeyImage: http://developer.android.com/tools/help/MonkeyImage.html
 
V. recording mode
# Usage: monkeyrunner recorder. py

# Recorder. py
From com. android. monkeyrunner import MonkeyRunner as mr
From com. android. monkeyrunner. recorder import MonkeyRecorder as recorder

Device = 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 sys
From 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
# Arguments for the command. In most cases, this directly maps to
# Keyword argument dictionary that cocould be passed to the underlying
# Command.

# Lookup table to map command strings to functions that implement that
# Command.
Pai_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 t:
Print 'unable to parse options'
Continue

If cmd not in CMD_MAP:
Print 'unknown command: '+ cmd
Continue

Performance_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 ()
Author: luckyapplelwj

Related Article

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.