1 Introduction
MonkeyScript is a scripting language for monkeys. It is a set of commands that can be recognized by Monkey. It can help us complete a series of fixed repetitive operations.
Script advantages: simple, fast, no tools required, just run a script file
Script shortcomings: only basic operations such as coordinates and buttons can be implemented simply (operate according to pixels)
2. Script format
#Header file, parameters that control the monkey to send messages
type=raw events
count=10
speed=1.0
#The following is the monkey command
start data >>
LaunchActivity(pkg_name,activity)
DispatchPress(KEYCODE_ENTER)
DispatchPress(KEYCODE_HOME)
----> The first three lines are script headers. count is the number of instructions contained in the script. speed is the execution speed.
----> start data >> followed by the executed command.
3. Common methods
LaunchActivity (pkg_name, cl_name): Launch the activity of the application. Parameters: package name and activated Activity.
Click to operate
Tap (x, y, tapDuration): Simulate a finger click event. Parameters: x, y are the coordinates of the control, tapDuration is the duration of the click, this parameter can be omitted.
DispatchPress (keyCode): Press the key. Note: The keyCode value can be obtained from the Baidu android keycode list
LongPress (): Long press for 2 seconds.
PressAndHold (x, y, pressDuration): Simulate long press events.
DispatchPointer (downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, device, edgeFlags): The DispatchPointer command is used to send a click operation to a specified coordinate position.
Mainly focus on the three parameters of action, x and y. action: Whether the event is pressed or raised, 0 means pressed, 1 means raised. x, y: indicates the X-axis and Y-axis coordinates triggered by the current event. Two DispatchPointer commands are added together, one is pressed and the other is lifted, which means one-click operation, and the remaining parameters are set to 0 by default. downTime, eventTime represents the duration, expressed in milliseconds.
Drag and drop
Drag (xStart, yStart, xEnd, yEnd, stepCount): Used to simulate a drag operation.
PinchZoom (x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End, stepCount): Simulate zoom gesture.
Wait for operation
UserWait (sleepTime): Waiting for a period of time, the waiting time indicated by sleepTime, in milliseconds.
DeviceWakeUp (): Wake up the screen.
ProfileWait: Wait 5 seconds.
Other operations
DispatchString (input): Input string.
RotateScreen (rotationDegree, persist): Rotate the screen. Parameters: rotationDegree is the rotation angle, eg 1 represents 90 degrees; persist means whether it is fixed after rotation, 0 means recovery after rotation, and non-zero means fixed.
DispatchFlip (true / false): Open or close the soft keyboard.
4. Examples
-
<Script example>
Requirements: Open the browser, enter www.ningmengban.com, and exit the browser. This step is repeated stress test.
-
----> Script file browser.txt is written as follows:
#Header file, parameters that control the monkey to send messages
type=raw events
count=10
speed=1.0
#The following is the script body
start data >>
# 1. Open the browser
LaunchActivity(com.android.browser,com.android.browser.BrowserActivity)
ProfileWait()
# 2. Clear URL
Tap(223,146)
ProfileWait()
DispatchPress(112)
ProfileWait()
# 3. Enter URL
DispatchString(www.ningmengban.com)
ProfileWait()
# 4. Confirm and load the URL
DispatchPress(KEYCODE_ENTER)
ProfileWait()
# 5. Finish exiting the browser
DispatchPress(KEYCODE_HOME)
ProfileWait()
----> Push browser.txt file to the phone
adb push D:\browser.txt data/local/tmp
D: \ browser.txt is the path and name of the local script file, and data / local / tmp is the path of the mobile phone
----> Run script
adb shell monkey -f data/local/tmp/browser.txt -v -v 200