This article is the learning note, video address of "Android app stress test" of Mu-class network
http://www.imooc.com/video/13007
Monkey is a tool for sending pseudo-random user events.
Monkeyscript Implementation of the automated test script, is a set of monkey can be recognized by the collection of commands, you can complete the repeated fixed operation. Screenshots are not supported.
Monkeyrunner provides a range of API operations.
The difference between monkey and Monkeyrunner:
- Monkey: Production of pseudo-random events for users or systems in adb shell
- Monkeyrunner: Defining specific commands and event control devices through the API
Monkeyrunner APIs
Monkeyrunner: Used to connect a device or simulator
Monkeydevice: Provides installation, uninstall app, send simulation event
Monkeyimage: Complete image preservation, and contrast operation
monkeyrunner Test Types : Multi-device control, functional testing, regression testing
Practice:(need to configure Android SDK and Python environment)
The first step: USB cable connected to the phone and computer
Step two: Cmd window input adb devices, if shown similar then successful connection
Step three: Install the test app
Fourth step: Send the pressure command, adb shell Monkey 1000 (randomly complete 1000 instructions)
Shown below:
Fifth step: Get the Test app package name
ADB Logcat | grep START (Gets the log information executed by the app as input to the output of the START tab in the next crawl log, grep is a command under Linux, and Windows can use FINDSTR instead of grep.) or execute the ADB shell into the phone, under the phone system, execute Logcat | grep START. Android system is Linux, so grep is supported)
real-machine test diagram, not the same as in the video, so it is recommended to use a virtual machine
The red notation represents the action of an implicit intent, whose naming specification is the package name +intent.action+ custom.
virtual machine test diagram, output will be updated with Operation
Sixth step: Put pressure on the specified package
ADB shell Monkey-p Package name 1000
Monkey Application of advanced parameters (parameters are after the package name, can be used in combination)
Throttle parameter, specifying the command interval
adb Shell Monkey--throttle < milliseconds >
such as c:\users\administrator>adb Shell monkey-p Com.android.calculator2–throttle 1000 10
Seed parameter, which specifies the seed value of the randomly generated number, which allows the same command to be generated
adb shell monkey-s< seed >< event-count >
Example: c:\users\administrator>adb shell monkey-p com.android.calculator2-s 100 50
PS: The video is executed several times, the effect is basically the same, and the execution time is only a few MS, but I test the effect is roughly the same, time-consuming some difference of 300ms, should be the problem of computer performance
Events can be combined, and percentages of <=100 can be
Click events to set the percentage of click events
adb shell monkey--pct-touch< percent >
If percent is set to 100, indicates that monkey only performs click events
C:\USERS\ADMINISTRATOR>ADB Shell monkey-v (can print out the specific actions performed)-P Com.android.calculator2–pct-touch 100 100
Action event, set action event ratio
adb shell monkey--pct-motion< percent >
Basic navigation events, set basic navigation events percentage, input device top, bottom, left, right
adb shell monkey--pct-nav< percent >
Main navigation event, set main navigation event percentage, compatible Middle key, return key, menu button
adb shell monkey--pct-majornav< percent >
System navigation event, set system navigation event percentage, home,back, dial key, Volume key
ADB shell monkey–pct-syskeys< percent >
Start activity events, set the percentage of activity events to start
ADB shell monkey–pct-appswitch< percent >
Infrequently used events, set the percentage of infrequently used events
ADB shell monkey–pct-anyevent< percent >
Crash events, ignore crashes and exceptions, or stop testing if encountered
adb shell monkey–ignore-crashes< event-count >
Timeout event, ignore ANR, or stop test if encountered
adb shell monkey–ignore-timeouts< event-count >
Crash result extraction
Starting with crash, the exact content is the same as in Android studio
ANR Results Extraction
Start with the ANR, or adb shell,cd/data/anr/, LS can see the traces.txt file, more traces.txt can see the details
Monkeyscript Common Command Introduction
Commands to execute monkey scripts
adb shell monkey-f< scriptfile >< event-count>
Script command:
- Dispatchtrackball Trackball event, paired with
Dispatchtrackball (Long Downtime,long eventide,int action,float x, float y, float pressure,float size,int Metastate,float xprecision,float yprecision,int divice,int edgeflags)
Action 0 represents press, 1 for bounce, x, y for coordinate point
Dispatchpointer Click events, pairing with
Dispatchtrackball (Long Downtime,long eventide,int action,float x, float y, float pressure,float size,int Metastate,float xprecision,float yprecision,int divice,int edgeflags)
Action 0 represents press, 1 for bounce, x, y for coordinate point
dispatchstring Command Input string event
Dispatchstring (String text)
launchactivity command to start the app
Launchactivity (package,activity)
Userwait Wait Event
Userwait (Millseconds)
Dispatchpress Press the key value
dispatchpress (int keycode) such as keycode 66 = Enter
Example:
1. Using the Android Sdk/tools/uiautomatorviewer.bat tool to measure the application interface to be tested, get the control in the interface of the upper-left point coordinates, lower-right point coordinates
2. Scripting (Save as script file)
The second parameter in launchactivity is to write the action name of the activity in full, i.e. com.xxx.xxx.xxx.MainActivity
3. Because the script is in the computer, monkey on the phone, so you have to copy the script to the phone
ADB push mooc.script/data/local/tmp/
Can enter the directory, ls view directory, ll view the file creation time
3. Test (at this time on the phone, do not add the adb shell)
Monkey-f Mooc.script 2
PS: To be able to start the corresponding application, its activity in the Androidmanifest file exported property to be set to true, in order to be opened externally!!!
Monkeyrunner Common API (can screen)
1.alert Warning Box
void alert (String message,string title,string oktitle)
#!/user/bin/python#-*- UTF-8 -*-from com.android.monkeyrunner import MonkeyRunnerMonkeyRunner.alert("Hello","title","OK")
Cmd into the Android sdk/tools directory, input Monkeyrunner demo.py, (at this time already put demo.py in this directory, otherwise need to add a path, such as Monkeyrunner D:\xx\demo.py) get
2.waitForConnection wait for the device to connect with multiple device IDs to indicate which devices are specific
3.drag drag (tuple-like point)
Drag (tuple start,tuple end,float Duration,integer steps)
4.press button
Press (String keycode,dictionary type)
5.startActivity Launch Application
StartActivity (package+ '/' +activity)
6.touch views
Touch (integer x,integer y,integer type)
7.type input
Type (String message)
8.takeSnapShot Screen Cutting
monkeyimage API
1.sameAs Image Comparison
Boolean Sameas (monkeyimage other,float percent)
2.writetoFile Image Preservation
void WriteToFile (String path,string format)
Practice
#!/usr/bin/python#-*-UTF-8-*-from com.android.monkeyrunner import Monkeyrunner,monkeydevice, Monkeyimage #连接设备, the device name ADB devices can get Device=monkeyrunner.waitforconnection (3, "192.168.56.101:5555") #启动App, Sleep Unit is Seconds device.startactivity ( "com.jikexueyuan.animationartdemo/ Com.jikexueyuan.animationartdemo.MainActivity ") Monkeyrunner.sleep (2) #点击device. Touch (100,100, "down_and_up") Monkeyrunner.sleep (1) .... #截屏image =device.takesnapshot () image.writetofile (
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
To be able to loop a test, you need a Python script that does not ...
Monkey of the Android app stress test