Android Automation Test-Monkeyrunner (i) Introduction

Source: Internet
Author: User

Monkeyrunner Introduction:

Monkeyrunner is a point-of- contact android black box automated test tool provided by Google.

The Monkeyrunner tool provides a set of APIs for users/testers to invoke, invoking these APIs to control an Android device or emulator without needing to know the corresponding source code.

With Monkeyrunner, we can write Python scripts to control the installation and uninstallation of the APK package, launch the app, send various action events to the app, intercept images, and save them.

The difference between Monkeyrunner and monkey:

Monkey is a series of random simulation events, mainly used for stress testing, through the ADB shell command.

Monkeyrunner can use the API to generate specific commands and events to control devices and emulators.

Features of the Monkeyrunner:

Multi-Device support : The 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.

Functional test: Monkeyrunner can run a functional test (top-down) automatically for an application. You provide input values for keystrokes or touch events, and then view the results by intercepting the screen.

regression test : Monkeyrunner can test the stability of an application by running an app and comparing its result screenshot to a given known good result screen.

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 standard Python OS and subprocess modules to invoke Android tools such as ADB. You can also add your own classes to the Monkeyrunner API.

Note the point:

The Monkeyrunner tool uses Jython (a Python implementation using the Java programming language, not Python), although the script needs to be saved as a. py file, but it cannot be executed using Python **.py ( The Monkeyrunner module cannot be found), you must use the Monkeyrunner **.py command to perform

Official An example of a simple monkeyrunner

1 #Python introduces Monkeyrunner module2  fromCom.android.monkeyrunnerImportMonkeyrunner, Monkeydevice3 #connect the device and get a Monkeydevice object4device =monkeyrunner.waitforconnection ()5 #install APK package. The return value is Boolean and can be used to determine if the installation was successful6Device.installpackage ('myproject/bin/myapplication.apk')7 #set a valid test package name8Package ='com.example.android.myapplication'9 #set an activity name under the test package nameTenActivity ='com.example.android.myapplication.MainActivity' One #Set Program Entry ARuncomponent = package +'/'+Activity - #Start the application -Device.startactivity (component=runcomponent) the #Press the phone's menu key -Device.press ('Keycode_menu', MONKEYDEVICE.DOWN_AND_UP) - #  -result =device.takesnapshot () + #Save -Result.writetofile ('Myproject/shot1.png','PNG')

From the example above we can see that the specific steps to test the application using Monkeyrunner are :

1. Import three APIs from From.android.monkeyrunner first

2. Connect the device, wait for the device to connect and return the connected device

3, install the test package (can write absolute path), after installation will return a Boolean variable, through this variable, write an if statement, determine whether the installation succeeded

4. Set the installer package name and activity name

5. Start the application with the package name/activity

6, the implementation of a series of touch, drag and other events

7. Save

8, compare SAMAs, judge the result


How to handle devices with different resolutions based on coordinate points

If 320*480, how to transplant to the top of 480*600?

Device.touch (340,580, ' down_and_up ')

320/x = 480/newx, x = 340

480/y = 600/newy, y = 580

This will be able to obtain a proportional relationship, using this proportional relationship to do a similar macro substitution, the code is basically not moving, or even write a method, obtained after the pixel, and the basis of the calculation, the direct replacement can be.

In addition to the above, you can also use Hierarchyviewer to get controls and find coordinates through controls. Disadvantage: To open root,4.1.2 below to open root. Note that more than 4.1 versions with Uiautomatorviewer do not require root and can be obtained.

Android Automation Test-Monkeyrunner (i) Introduction

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.