The Android SDK is now equipped with ready-made test tools monkey and Monkeyrunner two. Maybe we don't look at the same brother name. But the fact is that completely ran two completely different tools in the different areas of the test application.
Overall, the monkey is used primarily for stress and reliability testing, for the simulation of keyboard event streams, and for the ability to customize the number of sends to observe the stability and reliability of the tested application. It's easier to apply and remember the commands. And Monkeyrunner, in contrast, will be more powerful, it can be applied to functional testing, regression test. and the ability to customize the test expansion, flexibility is strong. And the test staff can be completely controlled.
This is my study Monkeyrunner notes, check the network of information and I tested out. Because I just contact, so the content of this article is superficial, experts please walk around ~
When I test the example below. Please build your own Android environment.
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 variables". 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. 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.
There are two options here. 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, let's use the example above. 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 from the study. It's 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. can also be used for other purposes.
Monkey Tools. is to execute the pseudo-random event stream of the user or system directly in the ADB shell of the device or simulator.
Monkeyrunner offers 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 connect all the devices at the same time or start all the emulators at once, connecting to each in sequence according to the program. Then perform one or more 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 execute an application and compare its result screenshot with the 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 the Android device.
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.
Executive 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. 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 using the original. Otherwise, an error will occur.
For example, connect the device or emulator, after the above alias. The command should be for example the following:
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: Time-out, in seconds. Floating-point number. 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路径
The path level is "/" and cannot be used with "\". 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. Contains its related data and adjusts 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 returns 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
Android's own automated test monkeyrunner and usage examples