Monkeyrunner environment construction and example tutorial (3)

The monkeyrunner tool provides an API. Programs written using this API can control Android devices and simulators outside of Android code.

1、 Introduction to monkeyrunner

1. The difference between monkeyrunner and monkey

1) The monkeyrunner tool controls the device or simulator (controllable) through specific commands and events defined by the API on the workstation

2) Precise control of events between events

3) It can carry out: touch screen, drag, long press and keyboard events

4) Intelligent screenshot comparison and judgment

5) Trace back the detailed bug path

2. Advantages and disadvantages of monkeyrunner

1) It can completely simulate all manual operations

2) There are detailed API documentation references

3) You can write intelligent image comparison scripts

4) Support Java and python scripts

5) Poor script portability

3. Monkeyrunner scripting

1) Set USB terminal to developer mode

2) Computer installation mobile phone driver

2、 Construction of monkeyrunner environment

To build the monkeyrunner environment, you need to install the following tools: JDK, Android SDK and python compiler.

1. JDK installation and configuration

1) JDK download address

http://www.oracle.com/technetwork/java/javase/downloads/index.html

After downloading, you can install it by default.

2) JDK environment configuration

After the JDK is successfully installed, computer → properties → advanced system settings → advanced → environment variable, in the system variable, create a new Java_ Home variable. Fill in the JDK installation directory with the variable value.

In the system variable, edit the path variable and enter% Java at the end of the variable value_ HOME%\bin;% JAVA_ HOME%\jre\bin; (note whether there is a; sign at the end of the original path variable value. If not, enter the; sign first and then the above code)

In the system variable, create a new classpath variable, and fill in the variable value as:

.;% JAVA_ HOME%\lib;% JAVA_ Home% \ lib \ tools.jar (note the first point)

This completes the configuration of system variables.

3) JDK environment check

Verify whether the JDK environment is successfully configured, run CMD, and enter Java - Version (there is a space between Java and - Version) in the CMD window. If the version information is displayed as shown in the figure, the installation and configuration are successful.

2. Android SDK installation and configuration

Android SDK refers to the exclusive software development kit for Android. Tools and platform tools folders are the most commonly used tools in Android SDK.

1) SDK download address

Download address 1: http://developer.android.com/sdk/index.html

Download address 2: http://rj.baidu.com/soft/detail/23485.html?ald

After downloading SDK, unzip it to your own directory without installation.

2) SDK environment configuration

After the SDK is successfully installed, computer → properties → advanced system settings → advanced → environment variable. In the system variable, create a new Android_ Home variable. Fill in the installation directory of tools and platform tools in the SDK with the variable value.

In the system variable, edit the path variable and enter% Android at the end of the variable value_ HOME%;

(note whether there is a; sign at the end of the original path variable value. If not, enter the; sign first and then the above code)

3) SDK environment check

If the SDK environment is successfully configured, run CMD and enter ADB in the CMD window. If it is shown in the figure, the installation and configuration are successful.

3. Python editor installation and configuration

Python is used to support monkeyrunner. Using Python scripting examples will greatly simplify the writing of monkeyrunner use cases and help expand the automation function of monkeyrunner.

1) Python download address

Download address: http://www.python.org/download/

After downloading, follow the prompts and install in the next step.

2) Python environment configuration

After Python is successfully installed, computer → properties → advanced system settings → advanced → environment variable, edit the path variable in the system variable, and finally enter the python installation path in the variable value;

(note whether there is a; sign at the end of the original path variable value. If not, enter the; sign first and then the above code)

3) Python environment check

If the python environment is successfully configured, run CMD and enter Python in the CMD window. If the version information is displayed as shown in the figure, the installation and configuration are successful.

4. Monkeyrunner environment check

If the above steps are completed and the environment variables are configured correctly, the monkeyrunner environment has been built. To verify whether the monkeyrunner environment is successfully built, run CMD and enter monkeyrunner in the CMD window. As shown in the following figure, the monkeyrunner environment is successfully built.

Next, you can use monkeyrunner to connect the simulator for automated testing.

3、 Monkeyrunner usage

Before using moneyrunner, you must turn on the simulator or connect the mobile device. The following is the instance operation of monkeyrunner.

1. Simulator startup

Here we choose the command to open the simulator. Run CMD. In the CMD window, enter the command: emulator - AVD AVD_ Test, where AVD_ Test is the name of the simulator. Fill in the name of the simulator you created.

After the simulator starts successfully, we still operate in CMD environment. Now enter the shell command interaction mode of monkeyrunner.

Enter the command: monkeyrunner

After entering the shell command interaction mode, the first thing is to import the module to be used by monkeyrunner. Enter the command directly under the shell command:

Press enter again. After this step is completed, we can use monkeyrunner to test.

2. Simulator connection

Next, we will connect monkeyrunner to the simulator and perform a series of operations. Enter command:

device=MonkeyRunner.waitForConnection()

Where, device = monkeyrunner. Waitforconnection (6, 'emulator-5554') parameter 1: timeout, unit: second, floating point number. The default is to wait indefinitely. Parameter 2: specified device name device_ ID, the default is the current device (mobile phone first, followed by simulator)

After entering the command, no error message is returned on the page, that is, the device is successfully connected.

3. App installation and startup

1) App installation

After the simulator starts successfully, we install the APK we want. Here we choose QQ music installation.

Enter the command: device. Installpackage ('f: \ \ qqyinle_439. APK '), where the parameter is the relative path of APK.

True is returned after successful installation. At this time, we can see the icon of the installed APK on the idle interface.

2) App startup

After the app is successfully installed, start the app now. The command is:

Device. Startactivity (component = "package name /. Activity")

First, we need to talk about how to get the package name and activity of an app. Here, we only describe one acquisition method.

Use AAPT, which is a self-contained tool in SDK. In the SDK \ builds tools \ Directory:

Take the QQ music stored in disk F as an example, run CMD and switch to the aapt.exe directory on the command line,

Method 1:

Execute the command: AAPT dump bagging F: \ qqyinle_ 439. APK, note that there must be no spaces in the APK path.

As can be seen from the above figure, package name: com.tencent.qqmusic

activity:.activity.AppStarterActivity

Method 1: because there are many logs, it is difficult to find, so we lead to method 2.

Method 2 (recommended):

Store the log in a specific file. Search keywords in the file to get the package name and activity name. Here, I output the results to log.txt on disk F:

aapt dump badging F:\QQyinle_ 439.apk > F:\log.txt

At this point, you have obtained the package name and activity of the app. Next, let's really start the app. In the original CMD run window, enter the command:

After the command is executed, the app on the simulator is started. This indicates that the command started the app successfully. The key here is to obtain the correct correspondence between the package name and activity of the app, otherwise the specific app cannot be started.

At this time, you can send operations such as key pressing, scrolling, screenshot, storage, etc. to the simulator.

4、 Monkeyrunner runs Python scripts

Similarly, monkeyrunner can directly call the specified Python script, write the command to the python file, such as * * *. Py, and then run it directly through monkeyrunner from the command line. For example, we still use the above example. The syntax is as follows: monkeyrunner * * *. Py. Next, monkeyrunner will automatically call * * *. Py and execute the statements therein, which is quite convenient.

Here, we put all the commands of the above example in the python file, name it test.py, and then store it in the local F disk, that is, the path is f: \ test.py.

Run monkeyrunner F: \ test.py in CMD. The python script path here is a relative path. Result error: syntax error: mismatched input 'test' expecting newline, as follows:

This is because the python script should be executed in DOS mode instead of entering the shell command interaction mode of monkeyrunner. The correct method is as follows: enter the command monkeyrunner F: \ test.py:

After running successfully, you can see the launched QQ music app on the simulator.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>