首页 > 代码库 > MonkeyRunner (二)

MonkeyRunner (二)

A Simple monkeyrunner Program


Here is a simple monkeyrunner program that connects to a device, creating a MonkeyDevice object. Using the MonkeyDeviceobject, the program installs an Android application package, runs one of its activities, and sends key events to the activity. The program then takes a screenshot of the result, creating a MonkeyImage object. From this object, the program writes out a .png file containing the screenshot.

这有一个monkeyrunner 代码的例子, 连接设备,创建MonkeyDevice对象,通过使用MonkeyDevice对象,程序安装了安卓应用包,运行了一个它的activity,并发送一个KEY事件给activity.之后这段代码创建了一个MonkeyImage对象,截图作为结果. 通过这个对象,这段代码输出了一个包含截屏的.PNG图片.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
 
# Connects to the current device, returning a MonkeyDevice object
device =MonkeyRunner.waitForConnection()
 
# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage(‘myproject/bin/MyApplication.apk‘)
 
# sets a variable with the package‘s internal name
package=‘com.example.android.myapplication‘
 
# sets a variable with the name of an Activity in the package
activity =‘com.example.android.myapplication.MainActivity‘
 
# sets the name of the component to start
runComponent =package+‘/‘+ activity
 
# Runs the component
device.startActivity(component=runComponent)
 
# Presses the Menu button
device.press(‘KEYCODE_MENU‘,MonkeyDevice.DOWN_AND_UP)
 
# Takes a screenshot
result = device.takeSnapshot()
 
# Writes the screenshot to a file
result.writeToFile(‘myproject/shot1.png‘,‘png‘)

 

The monkeyrunner API


The monkeyrunner API is contained in three modules in the package com.android.monkeyrunner:

monkeyrunner API 在com.android.monkeyrunner中包含3个模块:

  • MonkeyRunner: A class of utility methods for monkeyrunner programs. This class provides a method for connecting monkeyrunner to a device or emulator. It also provides methods for creating UIs for a monkeyrunner program and for displaying the built-in help.

    monkeyrunner: 一个为了monkeyrunner编程提供的工具方法的类.这个类提供了连接monkeyrunner与设备或者模拟器的方法.也为monkeyrunner程序创建UI和现实内     建help提供了方法.

  • MonkeyDevice: Represents a device or emulator. This class provides methods for installing and uninstalling packages, starting an Activity, and sending keyboard or touch events to an application. You also use this class to run test packages.

    MonkeyDevice: 代表了设备和模拟器,这个类提供了安装或卸载包,开始activity,并且对应用发送keyboard事件或者touch事件.你也能用这个类去运行测试包.

  • MonkeyImage: Represents a screen capture image. This class provides methods for capturing screens, converting bitmap images to various formats, comparing two MonkeyImage objects, and writing an image to a file.

    MonkeyImage: 代表了一个屏幕截图. 这个类为抓取截屏,bitmap 图片转换到其他格式,比较2个MonkeyImage对象和把一个图片写入文件提供了方法.

In a Python program, you access each class as a Python module. The monkeyrunner tool does not import these modules automatically. To import a module, use the Python from statement:

在Python 程序中,你能连接每个类(作为python的模块). Monkeyrunner 工具不会自动import这些模块.为了import这些模块,用python 的from:

?
1
from com.android.monkeyrunner import<module>

where <module> is the class name you want to import. You can import more than one module in the same from statement by separating the module names with commas.

在<module>那是你想要import的类名. 你能import多个用逗号分开的模块在一个from语句中.