Android Monkeyrunner Test Tool

Source: Internet
Author: User

The current Android SDK comes with a ready-made test tool with Monkey and Monkeyrunner two. People don't look alike to each other, but in fact they are totally different two tools that are used in different areas of testing. In general, Monkey is mainly used in stress and reliability testing, running the command can randomly send a variety of simulation keyboard event flow to the target program, and can define the number of times sent, in order to observe the application of the stability and reliability of the test, the application is relatively simple, remember that a few commands on the line. And Monkeyrunner, in contrast, will be a bit more powerful, it can be applied to functional testing, regression testing, and can customize the test extension, flexibility, and testers can fully control.

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

Please build your own Android environment when testing the following examples.

First, open the simulator

You must run the appropriate emulator before running Monkeyrunner, or Monkeyrunner cannot connect to 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-run VM, as shown in:)

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

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

If the result of the execution appears the following error content:

[Plain]View Plaincopy
    1. Panic:could not open:c:\documents and Settings\sadministrator\.android/avd/test.ini

As shown in the following:

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

When you are sure, close the cmd window and reopen cmd. Execute 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, so we can use Monkeyrunner to do the testing work.

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

Command description

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

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

Start any activity in it, just pass in the package and activity name. The command is as follows:
Device.startactivity (component= "Com.example.android.apis/com.example.android.apis.apidemos")
The emulator will automatically open the home page of the Apidemos application.

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 run it directly from the command line through Monkeyrunner. For example, we still use the above example, the syntax is as follows: Monkeyrunner test.py next Monkeyrunner will automatically call test.py, and execute the statements in it, quite convenient.

Example: test.py

[Python]View Plaincopy
    1. From Com.android.monkeyrunner import Monkeyrunner,monkeydevice
    2. Device=monkeyrunner.waitforconnection ()
    3. Device.startactivity (component="your.www.com/your.www.com.testactivity")

Execute in CMD

Monkeyrunner test.py

There may be an error "can ' t open specified script file" as shown in:

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

1. Store the test.py file in the same directory as the Monkeyrunner file. Can do: Monkeyrunner test.py call

2. Specify the location of the Python file. If the test.py file is in the D packing directory, this can be done: 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 Automated test framework: 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, and we can write a Python program to install, run, Send analog keystrokes to the user interface and will be stored in workstation for the fine operation. The Monkeyrunner tool is designed primarily to test applications and devices on the application/framework layer, or to run unit test suites, or for other purposes. The

Monkey tool, which runs directly in the ADB shell of a device or emulator, generates a pseudo-random stream of events for 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 emulators. You can pick up all the devices at the same time or start all the emulators at once, 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 testing: 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.
4. Regression test: Monkeyrunner can run an app and compare its result screenshot to a given known good result screen to test the stability of the application.
4, extensible Automation: Since Monkeyrunner is an API toolkit, we can develop a complete set of systems based on Python modules and programs to control Android devices. In addition to using the Monkeyrunner API, we can also use the standard Python OS and ubprocess modules to invoke Android tools such as Android Debug Bridge. For Android tools such as ADB, you can also add your own classes to the Monkeyrunner API.

Run Monkeyrunner

You can run Monkeyrunner directly using one of the code files, 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.

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, turn on activity, send keystrokes and touch events, run test packages, and more.
3, Monkeyimage: This class provides a way to capture the screen. This class provides methods for converting bitmaps into various formats, comparing two Monkeyimage objects, saving an image to a file, and so on.

Note: You must run the appropriate emulator before running Monkeyrunner, otherwise monkeyrunner cannot connect to the device
There are two ways to run the emulator: 1, execute simulator 2 through Eclipse, invoke simulator through command in CMD

Here is an introduction to the method of executing 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, after the above alias, the command should be as follows:
Device=mr.waitforconnection ()  

can also be used in the following way
from Com.android.monkeyrunner Import Monkeyrunner,monkeydevice,monkeyimage

can also be used in this way
Import Com.android.monkeyrunner
But when used, it becomes particularly troublesome
Device=com.android.monkeyrunner.monkeyrunner.waitforconnection ()  
We can also give it an alias
import Com.android.monkeyrunner as Cam
But when used, it becomes particularly troublesome
Device=cam. Monkeyrunner.waitforconnection ()

#等待连接到设备, connected to the emulator, returns a Monkeydevice object that represents the connected device. If no error is indicated, the connection is successful.
Parameter 1: Time-out, unit seconds, floating-point number. The default is to wait indefinitely.
Parameter 2: string DeviceID, specifying the device name. It is assumed that the current device (mobile phone priority, such as the mobile phone via USB cable connected to the PC, followed by the simulator).
Default connection: Device=monkeyrunner.waitforconnection ()
parameter connection: device = mr.waitforconnection (1.0, ' emulator-5554 ')

Install the apk you want to test to the device or emulator

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


Removes the specified package from the device, including its associated data and tuning 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, you can send to the simulator, such as keystrokes, scrolling, storage and other operations.


Executes an adb shell command and returns the result, if any
Device.shell ("...")

Pauses the number of seconds specified by a program that is currently running
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 ()
Return a Monkeyimage object (bitmap wrapper), we can save the diagram to a file using the following command
Result.writetofile (' takesnapshot\\result1.png ', ' png ')

Write File Monkeyimage

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


The type on the keyboard specifies the string, which is equivalent to requiring the characters in each 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 ()

Reboots the specified device to the specified boot program

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 (Parameter 1: Key code, Parameter 2: Touch event type)
Parameter 1: see android.view.KeyEvent
Parameter 2, if there are Touchpresstype () return type-touch event type, there are three kinds.
1, down sends a down event. Specifies that the down event type is sent to the device, corresponding to pressing a key or touching the screen.
2, up sends an up event. Specifies that the up event type is sent to the device, corresponding to releasing a key or lifting from the screen.
3, Down_and_up sends a down event, and then an up event. Corresponds to the input key or tap the screen.
The above three events are the press () or touch () parameters. The original English is as follows:
Use this with the type argument of press () or touch () to send a down event.


to simulate the input key, send down_and_up. Some specific content logic for


Parameter 1:

Press the HOME key 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 OK 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 
Upper navigation key keycode_dpad_up 
Down navigation key Keycode_ dpad_down 
left navigation keycode_dpad_left 
Right navigation key keycode_dpad_right  
OK keycode_dpad_center 
Volume key keycode_volume_up  
Volume key keycode_volume_down 
POWER key keycode_power 
Camera key keycode_camera 
MENU key keycode_menu 


MORE: http://developer.android.com/reference/android/view/KeyEvent.html

The Monkeyrunner tool for Android automated testing

  What is the Monkeyrunnermonkeyrunner 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. Second, the difference between the Monkeyrunner tool and the Monkey Tool Monkey:monkey tool runs directly in the ADB shell of the device or simulator, generating a pseudo-random event stream for the user or system. The Monkeyrunner:monkeyrunner tool is a specific command and event control device or emulator that is defined on the workstation through the API. Iii. Monkeyrunner Test Type 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 emulators at once, 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 standard Python os and subprocess modules to invoke Android tools such as Android debug Bridge. Iv. running Monkeyrunner You can run Monkeyrunner directly with a code file, or enter Monkeyrunner statements 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 contents of the file as a Python program and run it;Otherwise, it will provide an interactive dialog environment. Monkeyrunner's command syntax is: Monkeyrunner-plugin <plugin_jar> <program_filename> <program_options>[ Part of the above content from the Internet, for reference to the previous summary] Five, examples I here in Sample Apidemos as an example, we learn that any thing seems to have a self-contained sample start, can be in the development environment first to generate apidemos.apk. Prerequisite: The device is connected (this can be a real device, or simulator) 1, put apidemos.apk under $android_root\tools. 2, create a new monkeyrunnerprogram.py file under $android_root\tools, according to the official Help document, the file contains:   1 # Imports the Monkeyrunner Modules used by this program 2   3  from com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage 4  5  # connects to the current device, returning a monkeydevice object 6  &nbs P;7 device = monkeyrunner.waitforconnection ()  8  9  # installs the Android package. Notice that this method returns a Boolean, so can test10  11  # to see if the installation worked.12  1 3 Device.installpackage ('./apidemos.apk ') 14 15 16  # Runs the component17  18 Device.startactivity (CompOnent= ' com.example.android.apis/. Apidemos ') 19 20 21  # presses the Menu button22  23 device.press (' Keycode_menu ', ' down_and_up ') 24& Nbsp;25 26  # Takes a screenshot27  28 result = Device.takesnapshot () 29 30 31  # writes the Screenshot to a file32  33 result.writetofile ('./shot1.png ', ' png ')  3, open command line go to android_root\ Tools directory Run the command: Monkeyrunner monkeyrunnerprogram.py130215 23:35:19.711:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:wake.130215 23:35:20.711:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:wake.130215 23:35:21.711:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:wake.130215 23:35:22.835:s [Pool-1-thread-1] [ Com.android.monkeyrunner.adb.AdbMonkeyDevice] Error starting command:monkey--port 12345130215 23:35:22.835:s [ POOL-1-THREAD-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] com.android.ddmlib.ShellCommandUnresponsiveException130215 23:35:22.835:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Com.android.ddmlib.AdbHelper.executeRemoteCommand ( adbhelper.java:408) 130215 23:35:22.835:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Com.android.ddmlib.Device.executeShellCommand (device.java:276) 130215 23:35:22.835:s [Pool-1-thread-1] [ Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Com.android.monkeyrunner.adb.adbmonkeydevice$1.run ( adbmonkeydevice.java:89) 130215 23:35:22.835:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Java.util.concurrent.executors$runnableadapter.call (Unknownsource) 130215 23:35:22.835:S [pool-1-thread-1 ] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Java.util.concurrent.futuretask$sync.innerrun (Unknown Source) 130215 23:35:22.835:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Java.util.concurrent.FutureTask.run (Unknown Source) 130215 23:35:22.835:s [Pool-1-thread-1] [ Com.android.monkeyrunner.adb.AdbMonkEydevice]  at java.util.concurrent.threadpoolexecutor$worker.runtask (Unknown Source) 130215 23:35:22.835:S [ POOL-1-THREAD-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at java.util.concurrent.threadpoolexecutor$ Worker.run (Unknownsource) 130215 23:35:22.835:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice]  at Java.lang.Thread.run (unknownsource) 130215 23:35:55.632:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:press keycode_menu.130215 23:35:58.311:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:quit.4, you can view the generated shot1.png under Android_root\tools. Vi. Example improvement because the menu key is not present on the Apidemos home page, in order to visualize, continue to experiment on the basis of the previous experiment: 1, in the $android_root\ Tools to create a new monkeyrunnerprogram1.py file with the content:   1 # Imports The Monkeyrunner modules used by this program 2 & Nbsp; 3  from com.android.monkeyrunner Import Monkeyrunner, Monkeydevice, Monkeyimage 4  5 # Connects to the current device, returning a monkeydevice object 6  7 device = Monkeyrunner.waitforconnection ()  8  9 # Takes a screenshot10 11 result = Device.takesnapshot () 12 13 # writes the screenshot to a file14 15 result.writetofile ('./shotbegin.png ', ' PNG ' ) 16&NBSP;17 # Presses the down button18 #这里是我们加入的主要内容   19 device.press (' Keycode_dpad_down ', ' down_and_up ') 20  21 device.press (' Keycode_dpad_down ', ' down_and_up ') 22 23 device.press (' Keycode_dpad_down ', ' DOWN_AND_UP ') 24 25 device.press (' Keycode_dpad_down ', ' down_and_up ') 26 27 device.press (' Keycode_dpad_down ', ' DOWN_ And_up ') 28 29 # Takes a screenshot30 31 result = Device.takesnapshot () 32 33 # writes the screenshot to a fi Le34 35 result.writetofile ('./shotend.png ', ' png ') www.2cto.com2,   position the screen on the first page of the Apidemos, where the cursor is positioned in line one. 3.   Run the command in the $android_root\tools directory: Monkeyrunner Monkeyrunnerprogram1.py4, we can see the cursor moving downward during operation, And can be in the current directory we customized: Before: Shotbegin.png Run, Shotend.png (5 times in the Code Keycode_dpad_down operation)

Android Monkeyrunner Test Tool

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.