Android Test App tool (i)

Source: Internet
Author: User

When interviewing the app developer recently, I met with the technical Director to ask the conceptual questions of the app test, then feel the basic project flow, Project logic, screen adaptation,

Testing is completely no problem, but for the application of performance testing, stress testing and other high-end testing, there is a flaw in it, after all, professional development of the app

Personnel. However, the process of pursuing app development is never-ending, so make up your mind to learn the test of digging the app, hungry to fill in the app's high-end testing process and often

With tools. Recently began to study the Android automated test method, some of the tools, methods and framework to do some simple collation, including Android test box

, CTS, Monkey, Monkeyrunner, benchmark, other test tool, etc. Because of the short contact time, many places have shortcomings, I hope to be able to pay more with you

Flow. Let's look at a common test tool:

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:

# Imports the Monkeyrunner modules used by this program
From Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage
# connects to the current device, returning a Monkeydevice object
device = Monkeyrunner.waitforconnection ()
# Installs the Android package. Notice that this method returns a Boolean, so can test
# to see if the installation worked.
Device.installpackage ('./apidemos.apk ')
# Runs the component
Device.startactivity (component= ' com.example.android.apis/. Apidemos ')
# Presses the Menu button
Device.press (' Keycode_menu ', ' down_and_up ')
# Takes A screenshot
result = Device.takesnapshot ()
# writes the screenshot to a file
Result.writetofile ('./shot1.png ', ' png ')

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:2)

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:

# Imports the Monkeyrunner modules used by this program
From Com.android.monkeyrunner import Monkeyrunner, Monkeydevice, Monkeyimage
# connects to the current device, returning a Monkeydevice object
device = Monkeyrunner.waitforconnection ()
# Takes A screenshot
result = Device.takesnapshot ()
# writes the screenshot to a file
Result.writetofile ('./shotbegin.png ', ' png ')
# Presses the down button
Device.press (' Keycode_dpad_down ', ' down_and_up ')
Device.press (' Keycode_dpad_down ', ' down_and_up ')
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 screenshot
result = Device.takesnapshot ()
# writes the screenshot to a file
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

Android Test App tool (i)

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.