Android Auto Test Monkeyrunner tool

Source: Internet
Author: User

  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, Monkeydevice, 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 test
10
One-to-see if the installation worked.
12
Device.installpackage ('./apidemos.apk ')
14
15
# Runs the component
17
Device.startactivity (component= ' com.example.android.apis/. Apidemos ')
19
20
# Presses the Menu button
22
Device.press (' Keycode_menu ', ' down_and_up ')
24
25
# Takes A screenshot
27
result = Device.takesnapshot ()
29
30
# writes the screenshot to a file
32
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, Monkeydevice, Monkeyimage
4
5 # Connects to the current device, returning a Monkeydevice object
6
7 device = Monkeyrunner.waitforconnection ()
8
9 # Takes A screenshot
10
One result = Device.takesnapshot ()
12
# writes the screenshot to a file
14
Result.writetofile ('./shotbegin.png ', ' png ')
16
# Presses the down button
18
Device.press (' Keycode_dpad_down ', ' down_and_up ')
20
Device.press (' Keycode_dpad_down ', ' down_and_up ')
22
Device.press (' Keycode_dpad_down ', ' down_and_up ')
24
Device.press (' Keycode_dpad_down ', ' down_and_up ')
26
Device.press (' Keycode_dpad_down ', ' down_and_up ')
28
# Takes A screenshot
30
* result = Device.takesnapshot ()
32
# writes the screenshot to a file
34
Result.writetofile ('./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

Android Auto Test Monkeyrunner 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.