MonkeyRunner初识(个人翻译,请多指教,未完成)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cadi2011/article/details/51841316

1、MonkeyRunner,google提供的利器。

2、google大神说了,虽然我叫MonkeyRunner,可跟Monkey没有半点关系!!!

3、官方文档原文 (个人翻译)

monkeyrunner 猴子奔跑

In this document 在以下文档中

  1. A Simple monkeyrunner Program 1.一个简单的monkeyRunner程序
  2. The monkeyrunner API 2.monkeyRunner的API
  3. Running monkeyrunner 3.运行monkeyRunner
  4. monkeyrunner Built-in Help 4.monkeyRunner运行帮助
  5. Extending monkeyrunner with Plugins 5.扩展monkeyrunner的插件

See Also 你也可以看

  1. Testing Fundamentals 1.测试原理(基础)

The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it, sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation. The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites, but you are free to use it for other purposes.

monkeyrunner工具通过的API用于输出控制Android设备或者模拟器,是的从Android源码外部就行。关于monkeyrunner,你可以写Python程序实现安装Android application 或者 test package,运行后,可以发送按钮事件,可以截取屏幕,还可以在工作站中存储截图。monkeyrunner工具主要设计用于测试application,还有设备在函数框架级别下,用于进行单元测试,不过你可以自由使用用于其它目的。

The monkeyrunner tool is not related to the UI/Application Exerciser Monkey, also known as the monkey tool. The monkey tool runs in an adb shell directly on the device or emulator and generates pseudo-random streams of user and system events. In comparison, the monkeyrunner tool controls devices and emulators from a workstation by sending specific commands and events from an API.

这个monkeyrunner工具与Ui/Application Exerciser Monkey一点关系都没有,尽管也叫monkey工具。其实monkey工具是通过真机或者模拟器上的adb shell执行的,生成随机的用户流和系统事件,相比较,monkeyrunner工具从你的代码层API发送制定的命令和事件,然后操作真机或者模拟器

The monkeyrunner tool provides these unique features for Android testing:

这个monkeyrunner工具提供这些特点用于Android测试

  • Multiple device control: The monkeyrunner API can apply one or more test suites across multiple devices or emulators. You can physically attach all the devices or start up all the emulators (or both) at once, connect to each one in turn programmatically, and then run one or more tests. You can also start up an emulator configuration programmatically, run one or more tests, and then shut down the emulator.
  • 多设备控制:monkeyrunner 应用程序接口(Application Program Interface),应用一个或者多个测试用例运行在多个真机或者模拟器上。你能一次就控制所有真机或者运行的所有模拟器(或者全部),程序会按照顺序一个一个的连接,然后运行一个或者多个测试用例。你也可以启动一个模拟器,配置程序,运行一个或者多个测试用例,然后关掉模拟器
  • Functional testing: monkeyrunner can run an automated start-to-finish test of an Android application. You provide input values with keystrokes or touch events, and view the results as screenshots.
  • 功能测试:monkeyrunner可以用在Android application上,运行自动开始-结束的用例。你用键盘事件或者触摸事件提供输入操作,然后查看结果作为截图
  • Regression testing - monkeyrunner can test application stability by running an application and comparing its output screenshots to a set of screenshots that are known to be correct.
  • 回归测试- monkeyrunner可以测试应用的稳定性,它输出的截屏对比还可以知道正确性
  • Extensible automation - Since monkeyrunner is an API toolkit, you can develop an entire system of Python-based modules and programs for controlling Android devices. Besides using the monkeyrunner API itself, you can use the standard Python os and subprocess modules to call Android tools such as Android Debug Bridge.

    You can also add your own classes to the monkeyrunner API. This is described in more detail in the section Extending monkeyrunner with plugins.

  • 可扩展的自动化 - 你可以开发,利用python基本模块然后操控Android设备,此外也可以使用monkeyrunner自己的API,你可以使用标准的Python模块,如os、和subprocess模块,去调用Android工具,比如Android Debug Bridge,即adb命令。   你也可以加入自定义类到monkeyrunner的API里,更多详细的描述,在这部分文章里 链接

The monkeyrunner tool uses Jython, a implementation of Python that uses the Java programming language. Jython allows the monkeyrunner API to interact easily with the Android framework. With Jython you can use Python syntax to access the constants, classes, and methods of the API.

monkeyrunnergon工具使用Jython,一个使用Java高级语言实现的Python.Jython允许这个monkeyrunner的API很容易与Android的框架层进行交互? 真的吗?草,我咋不知道?通过Jpython你可以使用Python语法有权去操作API中的常量、类、还有方法

A Simple monkeyrunner Program 一个简单的monkeyrunner程序


Here is a simple monkeyrunner program that connects to a device, creating a MonkeyDevice object. Using the MonkeyDevice object, 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对象,这个程序安装一个Android应用,运行它其中的一个Activity,然后发送一个实体按键到Activity。这个程序还要在结果里截图,会创建一个MonkeyImage对象。通过这个对象,程序会输出一个截图的.png文件

# 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')
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  介绍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去连接设备或模拟器。它也提供创建UI,还有显示构建帮助的方法
  • 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.
  • 代表一个设备或模拟器。这个类提供安装与卸载包的方法,启动一个Activity、发送键盘或者触摸事件到应用程序中。你也可以使用这个类去运行测试包
  • 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对象,写入图片到文件

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工具不会自动导入这些模块,想要导入模块,在python文件中声明即可:

from com.android.monkeyrunner import <module>   #从哪个包中导入模块 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>就是你想要倒入的模块名 。 你可以从声明处导入多个模块,用逗号隔开就好

Running monkeyrunner 运行monkeyrunner


You can either run monkeyrunner programs from a file, or enter monkeyrunner statements in an interactive session. You do both by invoking the monkeyrunner command which is found in the tools/ subdirectory of your SDK directory. If you provide a filename as an argument, the monkeyrunnercommand runs the file's contents as a Python program; otherwise, it starts an interactive session.

你也可以从一个文件中运行monkeyrunner程序,或者在交互情况的输入monkeyrunner声明。你得从工具目录或者SDK父级目录输入monkeyrunner命令。如果你提供一个

The syntax of the monkeyrunner command is 关于monkeyrunner的命令行语法

monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>-plugin <plugin_jar> <program_filename> <program_options>

Table 1 explains the flags and arguments. 表格1  一些标志位和参数的讲解

Table 1. monkeyrunner flags and arguments. 表格1 一些标志位和参数的讲解

Argument 参数

Description

描述

-plugin <plugin_jar> (Optional) Specifies a .jar file containing a plugin for monkeyrunner. To learn more about monkeyrunner plugins, seeExtending monkeyrunner with plugins. To specify more than one file, include the argument multiple times.
<program_filename> If you provide this argument, the monkeyrunner command runs the contents of the file as a Python program. If the argument is not provided, the command starts an interactive session.
<program_options> (Optional) Flags and arguments for the program in <program_file>.

monkeyrunner Built-in Help  关于monkeyrunner构建帮助


You can generate an API reference for monkeyrunner by running: 你可以构造API引用,像这样运行

monkeyrunner help.py <format> <outfile>.py <format> <outfile>

The arguments are:

  • <format> is either text for plain text output or html for HTML output.
  • <outfile> is a path-qualified name for the output file.

Extending monkeyrunner with Plugins


You can extend the monkeyrunner API with classes you write in the Java programming language and build into one or more .jar files. You can use this feature to extend the monkeyrunner API with your own classes or to extend the existing classes. You can also use this feature to initialize the monkeyrunner environment.

To provide a plugin to monkeyrunner, invoke the monkeyrunner command with the -plugin <plugin_jar> argument described in table 1.

In your plugin code, you can import and extend the the main monkeyrunner classes MonkeyDeviceMonkeyImage, and MonkeyRunner incom.android.monkeyrunner (see The monkeyrunner API).

Note that plugins do not give you access to the Android SDK. You can't import packages such as com.android.app. This is because monkeyrunner interacts with the device or emulator below the level of the framework APIs.

The plugin startup class

The .jar file for a plugin can specify a class that is instantiated before script processing starts. To specify this class, add the keyMonkeyRunnerStartupRunner to the .jar file's manifest. The value should be the name of the class to run at startup. The following snippet shows how you would do this within an ant build script:

<jar jarfile="myplugin" basedir="${build.dir}">
<manifest>
<attribute name="MonkeyRunnerStartupRunner" value="com.myapp.myplugin"/>
</manifest>
</jar>

 jarfile="myplugin" basedir="${build.dir}">
<manifest>
<attribute name="MonkeyRunnerStartupRunner" value="com.myapp.myplugin"/>
</manifest>
</jar>

To get access to monkeyrunner's runtime environment, the startup class can implement com.google.common.base.Predicate<PythonInterpreter>. For example, this class sets up some variables in the default namespace:

package com.android.example;

import com.google.common.base.Predicate;
import org.python.util.PythonInterpreter;

public class Main implements Predicate<PythonInterpreter> {
    @Override
    public boolean apply(PythonInterpreter anInterpreter) {

        /*
        * Examples of creating and initializing variables in the monkeyrunner environment's
        * namespace. During execution, the monkeyrunner program can refer to the variables "newtest"
        * and "use_emulator"
        *
        */
        anInterpreter.set("newtest", "enabled");
        anInterpreter.set("use_emulator", 1);

        return true;
    }
} com.android.example;

import com.google.common.base.Predicate;
import org.python.util.PythonInterpreter;

public class Main implements Predicate<PythonInterpreter> {
    @Override
    public boolean apply(PythonInterpreter anInterpreter) {

        /*
        * Examples of creating and initializing variables in the monkeyrunner environment's
        * namespace. During execution, the monkeyrunner program can refer to the variables "newtest"
        * and "use_emulator"
        *
        */
        anInterpreter.set("newtest", "enabled");
        anInterpreter.set("use_emulator", 1);

        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/cadi2011/article/details/51841316