Monkey principle and improved optimization--android automated test learning process

Source: Internet
Author: User

Chapter: Basic principles of automation--monkey and improved optimization (third speaking)

Main content and notes:

First, theoretical knowledge:

Read the documentation directly to understand the concepts, fundamentals, and how to use monkey.

First,what is Android Monkey?

The Monkey is a program this runs on your emulator or device and generates pseudo-random streams of user events such as CL Icks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to Stress-test applications that is developing, in a random yet repeatable manner.

Translation: Monkey is a program that can run on a simulator or device, which can generate a pseudo-random sequence of user time, such as a click, touch screen, or crawl, and also contains a series of system-level events. We can use monkey to stress test our own applications, using random and repeatable operations.

Note: What are pseudo-random events?

The so-called pseudo-random event, that is: seed is the same, the random sequence does not change, and for a certain period of time, the generated random events are not duplicated.

Second,what can Monkey do?

The Monkey is a command-line tool, that's, can run on any emulator instance or on a device. It sends a pseudo-random stream of the user events into the system, which acts as a stress test on the application software yo U are developing.

The Monkey includes a number of options, but they break to into four primary categories:

    • Basic configuration options, such as setting the number of events to attempt.
    • Operational constraints, such as restricting the test to a single package.
    • Event types and frequencies.
    • Debugging options.

When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially:

    • If you had constrained the Monkey to run in one or more specific packages, it watches-attempts to navigate-any oth Er packages, and blocks them.
    • If your application crashes or receives any sort of unhandled exception, the Monkey would stop and report the error.
    • If your application generates an application not responding error, the Monkey would stop and report the error.

Depending on the verbosity level you are selected, you'll also see reports on the progress of the Monkey and the events Being generated.

Translate here:

Monkey contains a variety of options, but they are divided into the following four basic sections:

    • Base setting options, such as setting the number of events to be done
    • Operational restrictions, such as restricting testing of a single package
    • Event Type and frequency
    • Debugging options

When monkey runs, it generates events and sends them to the system. It will also focus on the system under test, and note that the three cases will be treated in a special way:

    • If you limit the monkey to only one or more specific packages, it will focus on attempts to cross over to other packages and block them.
    • If your application collapses or accepts other types of uncontrolled anomalies, monkey will stop and report this error
    • If your application generates an app that doesn't send an error, Monkey will also block and report the error

Third,how to use Monkey?

The basic syntax is:

$ adb shell monkey [options] <event-count>

In general, we are relatively simple to use:

$ adb shell Monkey-p your.package.name-v 500

Next, focus on the meaning of the command line arguments:

Category Option Description
General --help Open Help to see parameter information instantly
-v Each-v command increases the result with different redundancy levels. Different level, from 0 to 2, can provide more and more detailed content hints
Events -s <seed> Randomly generated sequences of seeds, if the values of seed are the same, the resulting random sequence is determined
--throttle <milliseconds>

Insert a fixed delay between two events, if not inserted, the event will be generated quickly, generally should be generated by the user can click a relatively normal delay

--pct-touch <percent>

Adjust the percentage of touch touch events, which refers to a single click-raised event in a separate location on the screen

--pct-motion <percent>

Adjust the percentage of move events, which means to press somewhere on the screen, undergo a series of pseudo-random movements, and finally lift

--pct-trackball <percent> Adjust the percentage of trackball events (this is no way to do this through drag, because drag is straight)
--pct-nav <percent> Adjust the percentage of navigation events (as a percentage of the base navigation event, such as up or down keys)
--pct-majornav <percent> Adjusts the percentage of primary navigation events (such as Middle key, Cancel, OK, or action of the graphical interface that the menu raises)
--pct-syskeys <percent> Adjust System key events such as Home, back,Startcall,Endcall and volume control keys, etc.
--pct-appswitch <percent> Adjusts the percentage of startup activity, executing a startactivity () method call in a random interval, as a way to maximize the coverage of all activity of the installation package
--pct-anyevent <percent> Adjust the percentage of other types of events, such as keystrokes, or other less commonly used events
Constraints -p <allowed-package-name>

This parameter can specify one or more packages, and each additional package requires the use of the one-time-p

-c <main-category>

To identify one or more specific categories, monkey only allows the system to access the activity listed under those specific categories. If not specified, monkey will select the Activity:Intent.CATEGORY_LAUNCHER or in the categories listed below.

Intent.category_monkey.

The-C command is the same as-p, and specifying a specific class requires a-C

Debugging --dbg-no-events

This option is primarily used to monitor conversions between packages that your application calls. When this parameter is set, monkey executes the initial boot and enters a test activity. In order to achieve a best effect, the limit should be with-V, one or more package,

and a non-zero throttle to make sure monkey can run at least 30S.

--hprof

When this option is set, the report will be generated immediately before and after the monkey event sequence, which is approximately 5Mb in size and stored in Data/misc, so be careful to use

--ignore-crashes

Typically, monkey stops running when it encounters an application crash or any other type of non-controllable exception. If this is set, monkey will not stop, continue to run, continue to send events to the system until all of this count is complete

--ignore-timeouts

Typically, monkey listens to stop running when it encounters an error in an application that encounters any type of timeout, such as the "Application stops Responding" dialog box. If this is set, monkey will continue to send events to the system until the count is complete

--ignore-security-exceptions

Typically, monkey stops when it encounters an application encountering any form of permission error, such as it intends to start an activity and it requires a certain amount of permission. If the item is set, Monkey will continue to send the event to the system until the count is over.

--kill-process-after-error

Typically, the application will continue to run because of an error that monkey to stop running. When this is set, when this error occurs, it notifies the system to stop the process. Note that in a normal (successful) completion process, the program that has been started is not stopped and the device status is the one that stays after the last event.

--monitor-native-crashes

Monitor and report the crashes of the native code in the Android system. If the--kill-process-after-error is set, the system will terminate

--wait-dbg

Wait for the connection on the debugger

Second, the basic principle of monkey architecture

1, Monkey main class

find void Main ()-->run (//view note, run is by assigning the value following the command-line argument to the corresponding variable, running the monkey-p xxx-v xxx in the adb shell command)

Logic in the Run method:

(1) Setting the initialization value

(2) generating random value events

(3) Add Intent.category_launcher and Intent.category_monkey to our CATEGORY in order to complete our launch

(4) According to the results of the assignment of a bunch of output operation, the specific visibility of their own running monkey when the output

(5) Enter Main monkey loop

2, monkeyactivityevent--is responsible for entering the main screen apk

Private Intent getEvent () {        2         new Intent (intent.action_main);//define a Intent (intent), Identify activity as the start of a program intent.addcategory (intent.category_launcher);//Add the application to the program list intent.setcomponent ( MAPP); Set the part intent.addflags (intent.flag_activity_new_task);//Add flag, one of four startup modes return intent;  7}         

Note: About the four startup modes: (from http://www.cnblogs.com/fanchangfa/archive/2012/08/25/2657012.html)

Activity startup Mode setting:        <activity android:name= ". Mainactivity "android:launchmode=" standard "/>activity four types of start-up mode:    1.        Activity is created each time the activity is activated and placed in the task Stack    2. Singletop        If an instance of the activity is present at the top of the task's stack, the instance is reused, and the new instance is created and placed on top of the stack ( An instance is created even if the activity instance already exists in the stack, as long as it is not on top of the stack    . 3. Singletask        If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created in the Stack    4. SingleInstance        creates the activity instance in a new stack and lets multiple apps share that activity instance in the stack. Once an instance of the activity of the pattern exists on a stack, any application that activates the activity will reuse the instance in that stack, which would have the effect of sharing an app with multiple applications, regardless of who activates the activity into the same application

The following configuration information is available in the androidmanifest.xml of the Andorid program:

1<Activity2Android:name= "Com.example.com.android.hello.FullscreenActivity"3Android:configchanges= "Orientation|keyboardhidden|screensize"4Android:label= "@string/app_name"5Android:theme= "@style/fullscreentheme">6<Intent-filter> 7 <action android:name= "Android.intent.action.MAIN" Span style= "color: #0000ff;" >/> 9 <category android:name= " Android.intent.category.LAUNCHER "/>10 </intent-filter> 11 </activity>                 

One of the things that is configured for Android is the activity that starts first, Note: Android.intent.action.MAIN determines the activity that the application starts first; Android.intent.category.LAUNCHER determines whether the application appears in the program list

3. Monkeyevent class and a series of sub-classes

Monkeyevent: Defines a superclass, defines a variable, constructs a method monkeyevent (int type), and gets the type method public int Geteventtype ()

Monkeykeyevent, monkeymotionevent and so on are all inherited from this super class

Iii. Improved Monkey program 1 (click where you need to go): Do Java project, ADB push in

Iv. Optimizing Monkey Scheme 2 (out of ADB control): Make Android apk form, let system level process call it directly

NetEase Cloud Classroom:

http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=878100&courseId=712011

Golden Sun Test

Sina Weibo: Golden Sun Woody

Website address

1, Baidu Search: Golden Sunshine test

2. Official website: www.goldensunshine.cc

Public number

Monkey principle and improved optimization--android automated test learning process

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.