Monkeyrunner Environment Construction and example tutorial (3) _android

Source: Internet
Author: User
Tags python script

The Monkeyrunner tool provides an API for programs written using this API to control Android devices and emulators outside of the Android code.

Introduction of Monkeyrunner

1.MOnkeyrunner Relative Monkey Difference

1 Specific commands and event control devices or simulators (controllable) defined by the API by the Monkeyrunner tool on the workstation

2 precise control of events between events

3 can be carried out: touch screen, drag, long press, keyboard events

4) can be a smart screenshot comparison and judgment

5) Trace out the detailed specific bug path

2.Monkeyrunner Advantages and Disadvantages

1) can completely simulate all manual operation

2 has detailed API documentation reference

3 can write smart image contrast script

4 support Java and Python scripts in two languages

5 Poor script portability

3.Monkeyrunner Script Writing

1 Terminal USB tuning to developer mode

2 computer-mounted mobile phone driver

Second, Monkeyrunner environment to build

Monkeyrunner environment, you need to install the following tools: JDK, Android SDK, Python compiler.

1.JDK Installation and Configuration

1) JDK Download address

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

When the download is complete, the default installation can be done.

2 JDK Environment configuration

After the success of the JDK installation, the computer → properties → advanced system settings → advanced → environment variables, in the system variable, new java_home variable, variable value fill in the JDK installation directory.

In the system variable, edit the path variable and enter the%java_home%\bin;%java_home%\jre\bin at the end of the variable value (note that there is no at the end of the variable value of the path; if not, first enter the number and then enter the code above)

In the system variable, create a new classpath variable with the variable value filled in as:

.; %java_home%\lib;%java_home%\lib\tools.jar (Note that there is a point at the front)

The system variable is configured to complete.

3 JDK Environmental Inspection

Verify that the JDK environment is configured successfully, then run CMD, and in the cmd window, enter java-version (there are spaces between Java and-version). If you display version information as shown in the illustration, the installation and configuration are successful.

2.android SDK Installation and configuration

The Android SDK refers to Android's proprietary software development toolkit. The most common use of the Android SDK is the tools and tool in the Platform-tools folder.

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 the SDK download is complete, unzip to your own directory without the need for installation.

2 SDK Environment Configuration

After successful SDK installation, the computer → properties → advanced system settings → advanced → environment variables, in the system variable, new android_home variable, variable value fill in the SDK tools and Platform-tools installation directory.

In the system variable, edit the path variable and enter the%android_home% at the end of the variable value;

(Note that the variable value of the original path has no; number, if not, first enter the number and then enter the code above)

3) SDK Environment check

Verify that the SDK environment is configured successfully, then run cmd, and enter the ADB in the cmd window. As shown in the illustration, installation and configuration are successful.

3.Python Editor installation and configuration

Python is used to support Monkeyrunner running, and using Python scripts to write use cases greatly simplifies the writing of monkeyrunner use cases and helps to extend the automation capabilities of Monkeyrunner.

1) Python download address

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

After downloading, follow the prompts to install the next step.

2 Python environment configuration

After the success of Python installation, the computer → properties → advanced system settings → advanced → environment variables, in the system variable, edit the path variable, at the end of the value of the variable input python installation path;

(Note that the variable value of the original path has no; number, if not, first enter the number and then enter the code above)

3 Python environment check

Verify that the Python environment is configured successfully, run cmd, and enter Python in the cmd window. If you display version information as shown in the illustration, the installation and configuration are successful.

4.Monkeyrunner Environmental Inspection

If the above steps are completed, and the environment variables are also configured correctly, so far, Monkeyrunner environment has been completed. Check Monkeyrunner environment is built successfully, the same run cmd, in the cmd window, input monkeyrunner. As the following illustration shows, the Monkeyrunner environment is built successfully.

The following can be used to automate the test with the Monkeyrunner connection simulator.

Three, Monkeyrunner use method

Before use, the Moneyrunner must first turn on the simulator or connect to the mobile device. The following is an instance operation for Monkeyrunner.

1. Simulator Startup

Here we select the command to open the emulator. Run cmd, in the cmd window, enter the command: Emulator-avd avd_test, where Avd_test is the name of the emulator, fill in the name of the emulator you created.

After the simulator started successfully, we still operate in the CMD environment. Now enter the Monkeyrunner shell command interaction mode.

Enter command: Monkeyrunner

The first thing to do when you go into shell command interaction mode is to import the module you want to use for Monkeyrunner. Enter the command directly under the shell command:

Copy Code code as follows:
From Com.android.monkeyrunner import Monkeyrunner,monkeydevice

Return to the carriage, this step completes we can use Monkeyrunner to carry on the test work.

2. Simulator connection

Below we will monkeyrunner connected to the simulator, a series of operations. Enter command:

Device=monkeyrunner.waitforconnection ()

of which, device=monkeyrunner.waitforconnection (6, ' emulator-5554′)
Parameter 1: Timeout, unit seconds, floating-point number, default is to wait indefinitely.
Parameter 2: The specified device name device_id, defaults to the current device (cell phone priority, followed by the simulator)

When you enter a command, there is no error message returned on the page, that is, the device is successfully connected.

3.app Install and start

1) App Installation

After the simulator starts successfully, we install the apk we want, here we choose the QQ music installation.

Input command: device.installpackage (' f:\\qqyinle_439.apk '), where the parameter is the relative path of the APK.

Successful installation returns True, at which point we can see the installed APK icon on the idle interface.

2 app Startup

After the app is installed, the app is now launched, and the command is:

Device.startactivity (component= "package name/.activity")

First, we need to say how to get the package name and activity of an app. Here, we only describe a way to get.

Using AAPT, where aapt is a tool from the SDK, under the Sdk\builds-tools\ directory:

Take the QQ music stored in the F disk as an example, run cmd, switch to the Aapt.exe directory in the command line,

Method One:

Execute command: AAPT dump badging F:\QQyinle_439.apk, note that there must be no spaces in the APK path.

From the above figure: package Name:com.tencent.qqmusic

Activity:.activity.appstarteractivity

Method one because of the log more, looking up more laborious, so we draw out method two.

Method Two (recommended):

Store the log in a specific file, in the file by searching for keywords, get package name and activity name, here I output the results to the log.txt of the F disk:

AAPT dump badging F:\QQyinle_439.apk > F:\log.txt

In this, the package name and activity of the app have been acquired. Below, we really start the app. In the original cmd Run window, enter the command:

Copy Code code as follows:
Device.startactivity (component= "com.tencent.qqmusic/.activity"). Appstarteractivity ")

After the command is executed, the app on the emulator is started. This indicates that the command started the app successfully. The key here is that app's package name and activity map get the right thing, otherwise it won't start a particular app.

At this point can be sent to the simulator such as keystrokes, scrolling, screenshots, storage and other operations.

Iv. Monkeyrunner Run the python script

Again, Monkeyrunner can invoke the specified Python script directly, write the command into a python file, name it like ***.py, and then we run it directly from the command line through Monkeyrunner. For example, we still use the above example, the syntax is as follows: Monkeyrunner ***.py. Next Monkeyrunner will automatically invoke ***.py and execute the statements in it, which is quite handy.

Here we will put the example above, all the commands in the Python file, and name test.py, then store to the local F disk, the path is: F:\test.py.

#coding: Utf-8 from
com.android.monkeyrunner import monkeyrunner,monkeydevice 
device= Monkeyrunner.waitforconnection ()
device.installpackage (' f:\\qq_374.apk ') 
monkeyrunner.sleep (3.0)
Runcomponent = "com.tencent.qqmusic/.activity." Appstarteractivity "
device.startactivity (component=runcomponent)

Run Monkeyrunner F:\test.py in cmd, where the Python script path is a relative path. Result error: syntaxerror:mismatched input ' test ' expecting newline, as follows:

This is because the Python script should be executed in DOS mode and not into the Monkeyrunner shell command interaction mode. The correct way is as follows, enter command Monkeyrunner F:\test.py:

After successful operation, you can see the launch of the QQ Music app on the simulator.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.