Monkeyrunner usage and examples of Android self-initiated test

Source: Internet
Author: User

Now the Android SDK comes with a ready-made test kit with monkey and Monkeyrunner two. People don't look alike, but in fact they are totally different two tools that are used in different test areas. In general, Monkey is mainly used in the pressure and reliability test, the command can randomly send a variety of simulation keyboard event flow to the target program, but also can customize the number of times sent, in order to observe the stability and reliability of the measured application, it is easier to apply, remember that several commands. And Monkeyrunner, in contrast, will be more powerful, it can be used mainly in functional testing, regression test, but also can be customized test expansion, flexibility, and test personnel can be completely controlled.

This is my study monkeyrunner when the notes, to view the network of information and I tested it out, because I just contact, so the content of this article is superficial, experts please walk around ~

Please build your own Android environment when you test the example below.

First, open the simulator

The corresponding emulator must be executed before executing the Monkeyrunner, otherwise monkeyrunner cannot connect the device.

Open the Android emulator with Elipse or open the emulator with the android command in CMD. Here's the point. Open simulator with android command in CMD

Command:EMULATOR-AVD test (Note: The name of the test for the virtual device--AVD is full: Android virtual device, which is an Android-executed VM, for example, as seen in:)

The test in the above command is the emulator name. You need to change the actual name when you use it.

Assuming normal, the simulator should be able to start up.

Assume that the result of the run has the following error content:

Panic:could not open:c:\documents and Settings\sadministrator\.android/avd/test.ini

For example, as seen in:

The reason is that your environment variable is missing a configuration. Please add "android_sdk_home" to "System variable" and set its value to "C:\Documents and Settings\Administrator" (Note: The value here cannot be C:\Documents and Settings\ Administrator\.android), for example, as seen in:

When you are sure, close the cmd form and open cmd again. Run the above command. The emulator will be enabled.

After the simulator starts successfully, we still operate in the CMD environment. Now enter Monkeyrunner's shell command interaction mode.

Command:monkeyrunner

After entering the shell command interaction mode, the first thing to do is to import the module to be used by Monkeyrunner. Enter directly under the shell command:

From com.android.monkeyrunner import monkeyrunner,monkeydevice Enter

OK, we'll be able to use Monkeyrunner to test the work after this step.

Here are two scenarios, one is to enter the following command directly under the shell command;

Command description

Device=monkeyrunner.waitforconnection () #连接手机设备

Device.installpackage (".. /samples/android-10/apidemos/bin/apidemos.apk ") #安装apk包到手机设备.

Start the random activity, just to pass in the package and activity name can be. Commands such as the following:
Device.startactivity (component= "Com.example.android.apis/com.example.android.apis.apidemos")
At this point the emulator will proactively open the home page of the Apidemos application itself.

Device.reboot () #手机设备重新启动

Device.touch (300,300, ' down_and_up ')

Monkeyrunner.alert ("Hello") #在emulator上会弹出消息提示

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

Device.type (' hello ') #向编辑区域输入文本 ' Hello '

The second is to write the following command into a python file, such as test.py, and then we can execute it directly from the command line through Monkeyrunner. For example, we still use the above example, syntax such as the following: Monkeyrunner test.py next Monkeyrunner will call test.py on its own initiative, and execute the statements in it, quite convenient.

Example: test.py

From Com.android.monkeyrunner import monkeyrunner,monkeydevicedevice=monkeyrunner.waitforconnection () Device.startactivity (component= "your.www.com/your.www.com.testactivity")

Run in cmd

Monkeyrunner test.py

There may be an error "can ' t open specified script file", for example, as seen in:

The reason is that the Python script file path is incorrect. You can have the following workaround:

1. Store the test.py file in the same folder as the Monkeyrunner file. Ability to run:monkeyrunner test.py Call

2. Specify the location of the Python file. Assuming that the test.py file is in the D-packing folder, it can run like this:monkeyrunner d:\test.py

Study Notes

Here are the notes in the study, a little messy. Just put it at the end of this article.

Environment variables

Variable name: android_sdk_home
Variable value: C:\Documents and Settings\Administrator

Variable name: Path
Variable value:%systemroot%\system32;%systemroot%; C:\Python27; C:\py;d:\android\android-sdk-windows\tools;d:\android\android-sdk-windows\platform-tools


Android self-motivated test frame: CTS, Monkey, Monkeyrunner, Benchmark

Monkeyrunner

The

monkeyrunner tool provides an API for programs written using the API to directly control Android devices and emulators without using Android code. We were able to write a Python program to install, execute, and send analog keystrokes to the Android app or test package, and the user interface would be stored in the workstation operation. The main purpose of the Monkeyrunner tool is to test applications and equipment on the application/framework layer, or to perform unit test kits, and for other purposes. The

Monkey Tool is a pseudo-random event stream that executes directly in the ADB shell of a device or emulator, generating a user or system. The

Monkeyrunner provides the following unique features for Android testing:
1, multi-device control: The Monkeyrunner API can implement test suites across multiple devices or simulators. Be able to pick up all the devices at the same time or start all the emulators at once, connect to each one in turn according to the program, and then perform a test or multiple tests. You can also use a program to start a configured emulator, perform one or more tests, and then close the emulator.
2, Function test: Monkeyrunner can be an application of their own initiative to carry out a function test. You provide input values for keystrokes or touch events, and then observe the screenshots of the output results.
4. Regression test: Monkeyrunner is able to perform an application and compare its result screenshot with a given known correct result screenshot to test the stability of the application.
4, extensible self-initiative: Because Monkeyrunner is an API toolkit, we are able to develop a complete set of systems based on Python modules and programs to control Android devices. In addition to using the Monkeyrunner API, we are able to use the standard Python OS and ubprocess modules to invoke Android Debug Bridge, an Android tool. such as the ADB, the Android tool, you can also add their own classes to the Monkeyrunner API.

Execute monkeyrunner

Ability to execute Monkeyrunner directly using a single code file, or enter the Monkeyrunner statement in an interactive conversation. Either way, you need to invoke the Monkeyrunner command under the Tools subfolder of the SDK folder. Assuming that a file name is provided as an execution parameter, Monkeyrunner will treat the file content as a Python program and execute it, otherwise it will provide an interactive dialog environment.

Monkeyrunner command syntax

Monkeyrunner-plugin <plugin_jar> <programe_filename> <programe_option>

Monkeyrunner API

Consists of three main modules
1. Monkeyrunner: This class provides a way to connect monkeyrunner and devices or emulators, and it provides a way to create a user interface display.
2, Monkeydevice: Represents a device or simulator. This class provides a way to install and unload packages, activate activity, send keystrokes and touch events, and perform test packs.
3, Monkeyimage: This class provides a way to capture the screen. This class provides a way to convert bitmaps into various formats, control two Monkeyimage objects, save an image to a file, and so on.

Note: The corresponding emulator must be executed before Monkeyrunner is executed, otherwise monkeyrunner cannot connect to the device
There are two ways to execute the emulator: 1, execute simulator 2 through Eclipse, invoke simulator through command in CMD

Here's how to run the emulator in cmd by command

Command: EMULATOR-AVD test
The test in the above command refers to the name of the simulator.

Import the required modules

Import Sys
From Com.android.monkeyrunner import Monkeyrunner as Mr
From Com.android.monkeyrunner import Monkeydevice as MD
From Com.android.monkeyrunner import monkeyimage as Mi
If you alias an imported module, you should use an alias instead of the original name, or an error will occur.
For example, to connect a device or emulator, the command should be as follows:
Device=mr.waitforconnection ()

can also be used in the following ways
From Com.android.monkeyrunner import Monkeyrunner,monkeydevice,monkeyimage

can also be used in such a way
Import Com.android.monkeyrunner
But when it comes to use, it's especially troublesome.
Device=com.android.monkeyrunner.monkeyrunner.waitforconnection ()
We can also give it an alias
Import Com.android.monkeyrunner as Cam
But when it comes to use, it's especially troublesome.
Device=cam. Monkeyrunner.waitforconnection ()

#等待连接到设备, connected to the emulator, returns the Monkeydevice object that represents the connected device. If no error is indicated, the connection is successful.
Number 1: Timeout, unit seconds, floating point. The default is to wait indefinitely.
Number 2: String DeviceID, the specified device name. The current device (phone priority, for example, the phone via a USB cable connected to the PC, followed by the simulator).
Default connection: Device=monkeyrunner.waitforconnection ()
Number of connections: device = mr.waitforconnection (1.0, ' emulator-5554 ')

Install a test apk to a device or simulator

Device.installpackage (' myproject/bin/myapplication.apk ') #參数是相对或绝对APK路径
Path level with "/", cannot use "\", for example d:\www\a.apk, but should be written d:/www/a.apk
The successful installation returns True, at which point we can see the installed APK icon on the idle interface.


Removes the specified package from the device, containing its associated data and adjusting the cache

Device.removepackage (' myproject/bin/myapplication.apk ')
Delete succeeded returns true.


#启动随意的Activity

Device.startactivity (component= "your.www.com/your.www.com.testactivity")
Or
Device.startactivity (component= "your.www.com/.testactivity")

At this point can be sent to the simulator such as keystrokes, scrolling, storage and other operations.


Run an adb shell command and return the results, assuming there are
Device.shell ("...")

Pauses the number of seconds specified by the currently executing program
Monkeyrunner.sleep (number of seconds, floating point)

Gets the screen buffer of the device, resulting in a masking capture of the entire display. ()
Result=device.takesnapshot ()
Returning a Monkeyimage object (bitmap wrapper), we can save the diagram to a file with the following command
Result.writetofile (' takesnapshot\\result1.png ', ' png ')

Write File Monkeyimage

Monkeyimage.writetofile (Parameter 1: Output file name, can also include path, number of parameters 2: Destination format)
Write success returns true, otherwise false


The type on the keyboard specifies the string, which is equivalent to requiring each character in a string to be pressed (key code, DOWN_AND_UP).
String sent to keyboard
Device.type (' string ')

Wake-up device screen (wakes up on the device screen)

Device.wake ()

Booting again to the specified boot program specified device

Device.reboot ()
=========================================================

Sends a touch event at a specified location (in pixels for x, y)

Device.touch (x,y,touchpresstype-touch Event type)

A key event that is sent to the specified key

Device.press (number 1: Key code, number of references 2: Touch event type)
Number 1: See Android.view.KeyEvent
In 2, there are three types of type-touch event types that are returned by Touchpresstype ().
1, down to send a down event. Specifies that the down event type is sent to the device, pressing a key or touching the screen.
2, up sends an up event. Specifies that the up event type is sent to the device, releasing a key or lifting from the screen accordingly.
3. Down_and_up sends a down event and then an up event. corresponding to the input key or tap the screen.
The above three events are either press () or touch (). The original English is as follows:
Use the-the type argument of press () or touch () to send a down event.


In order to simulate the input key, send down_and_up.


Part of the detail logic for the number 1:

Press the HOME button device.press (' Keycode_home ', monkeydevice.down_and_up)
Press the back key device.press (' Keycode_back ', monkeydevice.down_and_up)
Press the Down navigation key device.press (' Keycode_dpad_down ', monkeydevice.down_and_up)
Press the upper navigation key device.press (' keycode_dpad_up ', monkeydevice.down_and_up)
Press the OK key device.press (' Keycode_dpad_center ', monkeydevice.down_and_up)


Device.press (' Keycode_enter ', monkeydevice.down_and_up)#输入回车
Device.press (' Keycode_back ', monkeydevice.down_and_up)#点击返回

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
Down Volume key Keycode_volume_down
Power key Keycode_power
Camera key Keycode_camera
Menu key Keycode_menu


Many others: http://developer.android.com/reference/android/view/KeyEvent.html

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.