Android uses monkey for testing

Source: Internet
Author: User

First, let's take a look at the introduction and give a small instance.

Introduction

Monkey is a command line tool that can run in a simulator or a real device. It sends a pseudo-random user event stream to the system to test the stress of developing applications. Monkey includes many options, which are roughly divided into four categories:

· Basic configuration options, such as setting the number of attempted events.

· Run constraint options, such as setting to test only one package.

· Event type and frequency.

· Debugging options.

When monkey is running, it generates events and sends them to the system. At the same time, monkey also monitors the system in the test and handles the following three situations:

· If the monkey is limited to one or more specific packages, it will monitor and block operations that attempt to switch to other packages.

· If the application crashes or receives any exceptions that are out of control, the monkey stops and reports an error.

· If the application generates an application not responding error, the monkey will stop and report an error.

Based on the selected feedback information of different levels, you can also see the execution process report and generated events in the monkey.

Monkey usage

You can start monkey through the command line or script on the development machine. Since monkey runs in the simulator/device environment, you must use the shell in the environment to start it. You can add the ADB shell before each command to achieve the goal, or enter the monkey command directly after entering the shell. The basic syntax is as follows:

$ ADB shell monkey [Options]

If no options is specified, monkey starts in non-feedback mode and sends any event to all packages installed in the target environment. The following is a more typical command line example. It starts a specified application and sends 500 pseudo-random events to it:

$ ADB shell monkey-p your. Package. Name-V 500

Command Option reference

The following table lists all the options available for the monkey command line.

Category
Option
Description

General
-- Help
List simple usage.

-V
Each-V in the command line will increase the level of feedback information. Level 0 (default) provides less information except the startup prompt, test completion, and final result. Level 1 provides detailed test information, such as events sent to the activity one by one. Level 2 provides more detailed settings, such as the selected or unselected activity in the test.

Event
-S <seed>
The seed value of the pseudo-random number generator. If you run the monkey again with the same seed value, it generates the same event sequence.

-- Throttle <milliseconds>
Insert a fixed delay between events. This option can reduce the execution speed of monkey. If this option is not specified, the monkey will not be delayed and the event will be generated as quickly as possible.

-- PCT-touch <percent>
Adjust the percentage of touch events (a touch event is a down-up event that occurs at a single location on the screen ).

-- PCT-motion <percent>
Adjust the percentage of Action events (Action events are composed of a down event somewhere on the screen, a series of pseudo-random events, and an up event ).

-- PCT-trackball <percent>
Adjust the percentage of track events (track events consist of one or more random moves, sometimes with clicks ).

-- PCT-nav <percent>
Adjust the percentage of "Basic" navigation events (a navigation event consists of up/down/left/right from the input device ).

-- PCT-majornav <percent>
Adjust the percentage of "Main" navigation events (these navigation events usually lead to actions in the graphic interface, such as the intermediate buttons of the 5-way keyboard, the back button, and the menu button)

-- PCT-syskeys <percent>
Adjust the percentage of system key events (these keys are usually retained and used by the system, such as home, back, start call, end call, and volume control keys ).

-- PCT-Modify witch <percent>
Adjust the percentage of started activity. During the random interval, monkey executes a startactivity () call as a method to overwrite all the activities in the package to the maximum extent.

-- PCT-anyevent <percent>
Adjust the percentage of other types of events. It includes all other types of events, such as buttons, other infrequently used device buttons, and so on.

Constraints
-P <allowed-package-Name>
If you use this parameter to specify one or more packages, Monkey only allows the system to start the activity in these packages. If your application needs to access the activity in other packages (for example, select a contact), the packages also need to be specified here. If no package is specified, monkey allows the system to start the activity in all the packages. To specify multiple packages, you need to use multiple-P options. Each-P option can only be used for one package.

-C <main-Category>
If this parameter is used to specify one or more categories, monkey allows the system to start only the activities listed by a category in these categories. If no category is specified, monkey selects the activity listed in the following categories: intent. category_launcher or intent. category_monkey. To specify multiple categories, you need to use multiple-C options. Each-C option can only be used for one category.

Debugging
-- Dbg-no-Events
When this option is set, monkey will start initially, enter a test activity, and no further event will be generated. To get the best result, it is combined with-V, one or more packets, and a non-zero value that keeps the monkey running for 30 seconds or longer to provide an environment, monitors the conversion between packages called by applications.

-- Hprof
Set this option to generate a profiling report immediately before and after the monkey event sequence. This will generate a large file (~ 5 MB), so use it with caution.

-- Ignore-crashes
In general, monkey stops running when the application crashes or any runaway exception occurs. If this option is set, monkey continues to send events to the system until the count is complete.

-- Ignore-timeouts
Generally, when an application encounters any timeout error (such as the "Application not responding" dialog box), the monkey stops running. If this option is set, monkey continues to send events to the system until the count is complete.

-- Ignore-security-Exceptions
In general, when an application has a license error (such as starting an activity that requires certain licenses), the monkey stops running. If this option is set, monkey continues to send events to the system until the count is complete.

-- Kill-process-after-Error
Generally, when a monkey stops due to an error, the failed application continues to run. When this option is set, the system will be notified to stop the process with an error. Note that the process ends normally (successfully) and does not stop. The device simply remains in the final state after the event ends.

-- Monitor-native-crashes
Monitors and reports crash events of local code in the Android system. If -- kill-process-after-error is set, the system stops running.

-- Wait-dbg
Stop the running monkey until a debugger is connected to it.

Next, let's look at an instance.

Monkey usage is $ ADB shell monkey-p your. package. name-V 500. First, we need to find the package name corresponding to the application in emulator. At the beginning, I found directories one by one and completed the work in the first seven steps, finally, we found that all application packages are under data/data. You can CD Data/data in the shell, and then ls to view all application packages in the current emulator.

If we want to perform a stress test on apidemos In the SDK,

1. Create a project in eclipse

F: \ Android-SDK-Windows \ platforms \ Android-2.0 \ samples \ apidemos add to project and click Run. At this time, his APK should be loaded to emulator.

2. Enter ADB shell in the command line

3. Enter ls to view the directory in the current folder. The execution result is as follows:

C: \ Documents ents and Settings \ Administrator> ADB Shell
# Ls
Ls
Sqlite_stmt_journals
Config
Cache
Sdcard
D
Etc
System
Sys
Sbin
Proc
Init. RC
Init. Goldfish. RC
Init
Default. Prop
Data
Root
Dev

4. The application packages are all under data. Enter CD data to enter the data folder, as shown below:

# CD Data
CD Data

5. Enter ls to view the content in the folder, as shown below:

# Ls
Ls
Misc
Local
Data
App-Private
App
Property
ANR
Backup
Dontpanic
Dalvik-Cache
System
Lost + found

6. There is another data. All the applications are under this data. Enter this data and enter the LS as follows:

# CD Data
CD Data
# Ls
Ls
Com. Android. MMS
Com. Android. providers. Applications
Com. Android. globalsearch
Com. Android. calculator2
Com. Android. spare_parts
Com. Android. gesture. Builder
Com. Android. Music
Com. Android. sdksetup
Com. Android. packageinstaller
Com. Android. Fallback
Com. Android. providers. Settings
Com. Android. providers. DRM
Com. Android. Development
Com. Android. providers. Telephony
Com. Android. inputmethod. Pinyin
Com.android.html Viewer
Com. Android. Settings
Com. Android. netspeed
Com. Android. providers. userdictionary
Com. Android. Browser
Com. Android. Contacts
Com. Android. alarmclock
Com. Android. Camera
Com. Android. providers. Contacts
JP. co. omronsoft. openwnn
Com. Android. Launcher
Com. Android. Phone
Com. Android. soundrecorder
Com. Google. Android. providers. enhancedgooglesearch
Com. svox. Pico
Com. Android. customlocale
Com. Android. Term
Com. Android. providers. downloads
Com. Android. providers. Media
Com. Android. Email
Android. TTS
Com. Android. inputmethod. Latin
Com. Android. server. VPN
Com. example. Android. APIs

7. The last one is the application package of apidemo we just loaded.

8. you can directly enter monkey-p com. example. android. APIS-V 50. The result is as follows. You can also exit the shell and enter ADB shell monkey-p com. example. android. APIS-V 500. During the running process, the applications in the emulator are constantly switching the screen. You can take a look, haha :)

# Monkey-P com. example. Android. APIs-V 50
Monkey-P com. example. Android. APIs-V 50
: Monkey: seed = 0 count = 50
: Allowpackage: COM. example. Android. APIs
: Includecategory: Android. Intent. Category. Launcher
: Includecategory: Android. Intent. Category. Monkey
// Event percentages:
// 0: 15.0%
// 1: 10.0%
// 2: 15.0%
// 3: 25.0%
// 4: 15.0%
// 5: 2.0%
// 6: 2.0%
// 7: 1.0%
/// 8: 15.0%
: Switch: # intent; Action = Android. Intent. Action. Main; Category = Android. Intent. Categ
Ory. launcher; launchflags = 0x10000000; Component = com. example. Android. APIs/. apidemos
; End
// Allowing start of intent {act = Android. Intent. Action. Main cat = [Android. In
Tent. Category. Launcher] CMP = com. example. Android. APIs/. apidemos} in package COM.
Example. Android. APIs
: Sending pointer action_move x =-4.0 y = 2.0
: Sending pointer action_up x = 0.0 y = 0.0
// Allowing start of intent {CMP = com. example. Android. APIs/. apidemos} in PA
Ckage com. example. Android. APIs
: Sending pointer action_down x = 207.0 y = 282.0
: Sending pointer action_up x = 189.0 y = 289.0
// Allowing start of intent {CMP = com. example. Android. APIs/. App. intents} in
Package com. example. Android. APIs
: Sending pointer action_down x = 95.0 y = 259.0
: Sending pointer action_up x = 95.0 y = 259.0
: Sending pointer action_down x = 295.0 y = 223.0
: Sending pointer action_up x = 290.0 y = 213.0
: Sending pointer action_move x =-5.0 y = 3.0
: Dropped: Keys = 0 pointers = 0 trackbils = 0 flips = 0
# Network stats: elapsed time = 3799 ms (3799 Ms mobile, 0 Ms WiFi, 0 Ms not connecte
D)
// Monkey finished

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.