Monkeyrunner Tools
Objective:
Recently began to study the Android automated test method, some of the tools, methods and frameworks to do some simple collation, including Android test framework, CTS, Monkey, Monkeyrunner, benchmark, other test tool and so on. Because the contact time is very short, many places have the insufficiency, hoped can communicate with everybody.
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>
V. Examples
Take Apidemos in sample as an example, and first generate the apidemos.apk.
Prerequisite: Device Connection already available
1, put the apidemos.apk under the $android_root\tools.
2. Create a new monkeyrunnerprogram.py file under $android_root\tools with the following contents:
1 # Imports The Monkeyrunner modules used by this program 2 3 from Com.android.monkeyrunner import Monkeyrunner, Monkey Device, Monkeyimage 4 5 # connects to the current device, returning a Monkeydevice object 6 7 device = Monkeyrunner . Waitforconnection () 8 9 # Installs the Android package. Notice that this method returns a Boolean, so can test10 one-to-see if the installation worked.12 Lpackage ('./apidemos.apk ') * Runs the component17 device.startactivity (component= ' com.example.android.apis/ . Apidemos ') # presses the Menu button22 device.press (' Keycode_menu ', ' down_and_up ') # Takes a Screensh Ot27 result = Device.takesnapshot () writes the screenshot to a file32 result.writetofile ('./shot1.png ', ' PNG ')
Note: The examples on the SDK are some errors and cannot be copied directly, otherwise an error will occur when executing the command. Specifically, you can compare it with my above code.
3. Open the command line and go to the Android_root\tools directory to run the command:
Monkeyrunner monkeyrunnerprogram.py
110307 15:33:19.625:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:wake.
110307 15:33:20.625:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:wake.
110307 15:33:21.625:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:wake.
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] Error starting Command:monkey --port 12345
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] Com.android.ddmlib.ShellCommandUnresponsiveException
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Com.android.ddmlib.AdbHelper.executeRemoteCommand (adbhelper.java:408)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Com.android.ddmlib.Device.executeShellCommand (device.java:276)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Com.android.monkeyrunner.adb.adbmonkeydevice$1.run (adbmonkeydevice.java:89)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Java.util.concurrent.executors$runnableadapter.call (Unknownsource)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Java.util.concurrent.futuretask$sync.innerrun (Unknown Source)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Java.util.concurrent.FutureTask.run (Unknown Source)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Java.util.concurrent.threadpoolexecutor$worker.runtask (Unknown Source)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Java.util.concurrent.threadpoolexecutor$worker.run (Unknownsource)
110307 15:33:22.718:s [pool-1-thread-1] [Com.android.monkeyrunner.adb.AdbMonkeyDevice] at Java.lang.Thread.run ( Unknownsource)
110307 15:33:57.437:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:press keycode_menu.
110307 15:33:59.171:i [Main] [Com.android.monkeyrunner.MonkeyManager] Monkey command:quit.
Note: Inside the exception hint we can ignore, because we can see Monkey command:press Keycode_menu has executed successfully.
4. You can view the generated shot1.png under Android_root\tools.
Vi. instance Expansion
Because the menu key on the Apidemos home page does not appear, in order to visualize it, continue experimenting on the basis of example five:
1. Create a new monkeyrunnerprogram1.py file under $android_root\tools with the following contents:
1 # Imports The Monkeyrunner modules used by this program 2 3 from Com.android.monkeyrunner import Monkeyrunner, Monkey Device, Monkeyimage 4 5 # connects to the current device, returning a Monkeydevice object 6 7 device = Monkeyrunne R.waitforconnection () 8 9 # Takes a screenshot10 one result = Device.takesnapshot () # writes the screenshot to a F Ile14 result.writetofile ('./shotbegin.png ', ' PNG ') # presses the ' Down Button18 ' device.press (' Keycode_dpad_down ', ' down_and_up ') device.press (' Keycode_dpad_down ', ' down_and_up ') of device.press (' Keycode_dpad_down ', ' DOWN_ And_up ') device.press (' Keycode_dpad_down ', ' down_and_up ') device.press (' Keycode_dpad_down ', ' DOWN_AND_UP ') # Takes a Screenshot30 result = Device.takesnapshot () # writes the screenshot to a file34 result.writetof Ile ('./shotend.png ', ' png ')
2, the screen positioning in the Apidemos home, and positioning the cursor on the first item.
3. Run the command in the $android_root\tools directory:
Monkeyrunner monkeyrunnerprogram1.py
4, in the process of operation we can see the cursor is moving downward, and can be in the current directory we customize:
Before Operation: Shotbegin.png
After operation (five down-shift): Shotend.png
Resources:
Http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html
[Turn] Android Auto test Monkeyrunner tool (ii)