Android Test Monkeyrunner

Source: Internet
Author: User


First, what is Monkeyrunner


The Monkeyrunner tool provides an API for programs written out using this API to control Android devices and emulators outside of Android code. With Monkeyrunner, you can write a Python program to install an Android application or test package, run it, send it an analog keystroke, intercept its user interface picture, and store it on your workstation. The main purpose of the Monkeyrunner tool is to test applications and devices at the functional/frame level, or to run unit test suites, but you can of course use them for other purposes.


Ii. differences between Monkeyrunner tools and monkey Tools


Monkey:



The Monkey tool runs directly in the ADB shell of the device or emulator, generating a pseudo-random stream of events for the user or system.



Monkeyrunner:



The Monkeyrunner tool is a specific command and event control device or emulator that is defined on the workstation through the API.


Iii. test type of Monkeyrunner


1. Multi-Device control: The Monkeyrunner API can implement test suites across multiple devices or emulators. You can pick up all the devices at the same time or start all the simulators at once (or all together), connect to each one by program, and then run one or more tests. You can also use a program to start a configured emulator, run one or more tests, and then close the emulator.



2. Functional test: Monkeyrunner can automatically implement a functional test for an application. You provide input values for keystrokes or touch events, and then observe the screenshots of the output results.



3. Regression test: Monkeyrunner can run an app and compare its result screenshot to a given known good result screenshot to test the application's stability.



4. Scalable Automation: Because Monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs to control your Android device. In addition to using the Monkeyrunner API, you can use the standard Python OS and subprocess modules to invoke Android tools such as Android Debug Bridge.


Four, running Monkeyrunner


You can run Monkeyrunner directly with a code file, or enter the Monkeyrunner statement in an interactive conversation. Either way, you need to invoke the Monkeyrunner command under the tools subdirectory of the SDK directory. If you provide a file name as a running parameter, Monkeyrunner will view the file content as a Python program and run it, otherwise it will provide an interactive dialog environment.



The command syntax for Monkeyrunner is:



Monkeyrunner-plugin <plugin_jar> <program_filename> <program_options>


1. cmd dialog box run mode


Such as:



, we simulate one (160,450) of the coordinate click and one return key click, and respectively:



As you can see, when you click Back, the emulator UI returns to the main interface.


2. How the code files run


(1) Create a new monkeyrunnerprogram.py file under $android_root\tools with the following contents:



The code is as follows:



# Imports the Monkeyrunner modules used by this program



From Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage



# connects to the current device, returning a Monkeydevice object



device = Monkeyrunner.waitforconnection ()



Device.touch (160,450, "down_and_up")



Print "Touch"



Monkeyrunner.sleep (1)



Print "Shoting ... shot.png"



# Takes A screenshot



result = Device.takesnapshot ()



filename = './shot.png '



# writes the screenshot to a file



Result.writetofile (filename, ' png ')



Print filename



Monkeyrunner.sleep (1)



# do some tests



# Presses the Menu button



Device.press (' Keycode_back ', ' down_and_up ')



Print "Press Back"



Print "Shoting ... shot.png"



# Takes A screenshot



result = Device.takesnapshot ()



filename = './shot1.png '



# writes the screenshot to a file



Result.writetofile (filename, ' png ')



Print filename



Monkeyrunner.sleep (1)



(2) Open the command line, go to the Android_root\tools directory, and run the following command:



Monkeyrunner monkeyrunnerprogram.py



can achieve the same test results as just now


V. Detailed usage of Monkeyrunner


1. #Import module;





From Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage





2. #Connect the current device, and return a Monkeydevice object;





device = Monkeyrunner.waitforconnection ()





If not device:





Print "Please connect a device to start!"





Else





Print "Start"





3. #Install Android package, note that the return value returned by this method is Boolean, which can be used to determine if the installation process is normal;





Device.installpackage ('myproject / bin / myapplication.apk')





Device.removepackage ('Com.example.android.notepad')





Print ('Unload succeeded')





Device.installpackage ('apidemos.apk')





Print ('Installation successful')





4. #Start an Activity;





Device.startactivity (component = 'com.android.htccontacts / com.android.htccontacts.contactstabactivity')





5. #;





result = Device.takesnapshot ()





Result.writetofile ('c: \\ users \\ martin \\ desktop \\ test.png', 'png')





6. #Time delay (seconds);





Monkeyrunner.sleep (3)





7. #Slide the screen;





For I in Range (1,70):





Device.drag (250,850), (250,110), 0.1,10)





Start, end, duration, step





For I in Range (1,70):





Device.drag (250,110), (250,850), 0.1,10)





Monkeyrunner.sleep (1)





8. #Touch the screen;





Device.touch (507,72, "down_and_up")





9. #Execute adb shell command;





Device.shell ("Input text Goup01")





Press the Home button





Device.press ('Keycode_home', 'down_and_up')





Press the back key





Device.press ('Keycode_back', 'down_and_up')





Press the down navigation key





Device.press ('Keycode_dpad_down', 'down_and_up')





Press the UP navigation key





Device.press ('keycode_dpad_up', 'down_and_up')





Press the OK button





Device.press ('Keycode_dpad_center', 'down_and_up')





The corresponding keys correspond to the following names:





HOME Key: Keycode_home





Back key: Keycode_back





Send key: Keycode_call





End key: Keycode_endcall





Up navigation key: Keycode_dpad_up





Down Navigation key: Keycode_dpad_down





Left navigation: Keycode_dpad_left





Right navigation key: Keycode_dpad_right





OK key: Keycode_dpad_center





Volume up key: Keycode_volume_up





Lower Volume key: Keycode_volume_down





POWER Key: Keycode_power





CAMERA Key: Keycode_camera





MENU Key: Keycode_menu





Android Test Monkeyrunner


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.