您的位置:首页 > 移动开发 > Android开发

Android自动测试工具MonkeyRunner之一:基础知识

2012-10-18 22:55 399 查看
一、简介

monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。

二、monkeyrunner工具同Monkey工具的差别

Monkey:

Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。

monkeyrunner:

monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。

三、monkeyrunner的测试类型

1、多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。

2、 功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。

3、 回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。

4、 可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subprocess模块来调用Android Debug Bridge这样的Android工具。

四、运行monkeyrunner

您可以直接使用一个代码文件运行monkeyrunner,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。

monkeyrunner的命令语法为:

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

五、主要涉及类及API

MonkeyRunner ,MonkeyDevice,MonkeyImage

· MonkeyRunner:一个为monkeyrunner程序提供工具方法的类。这个类提供了用于连接monkeyrunner至设备或模拟器的方法。它还提供了用于创建一个monkeyrunner程序的用户界面以及显示内置帮助的方法。

· MonkeyDevice:表示一个设备或模拟器。这个类提供了安装和卸载程序包、启动一个活动以及发送键盘或触摸事件到应用程序的方法。您也可以用这个类来运行测试包。

· MonkeyImage:表示一个截图对象。这个类提供了截图、将位图转换成各种格式、比较两个MonkeyImage对象以及写图像到文件的方法。

主要API(不想自己做,直接贴官方的)

MonkeyRunner:

void
alert (string message, string title, string okTitle) (主要的,弹出alert)
Displays an alert dialog to the process running the current program.
integer
choice (string message, iterable choices, string title)
Displays a dialog with a list of choices to the process running the current program.
void
help (string format) (显示帮助信息)
Displays the monkeyrunner API reference in a style similar to that of Python's pydoc tool, using the specified format.
string
input (string message, string initialValue, string title, string okTitle, string cancelTitle)
Displays a dialog that accepts input.
void
sleep (float seconds) (睡眠,参数为秒)
Pauses the current program for the specified number of seconds.
MonkeyDevice
waitForConnection (float timeout, string deviceId) (等待连接device)
Tries to make a connection between the monkeyrunner backend and the specified device or emulator.
MonkeyDevice:

Constants
string
DOWN
Use this with the type argument of press() or touch() to send a DOWN event. (按键事件,按下不弹上,适用于长按)
string
UP
Use this with the type argument of press() or touch() to send an UP event.
(按键事件,弹上来)
string
DOWN_AND_UP
Use this with the type argument of press() or touch() to send a DOWN event immediately followed by an UP event. (先按下,再弹上)
Methods
void
broadcastIntent (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, iterable flags)
Broadcasts an Intent to this device, as if the Intent were coming from an application.
void
drag (tuple start, tuple end, float duration, integer steps) (拖动屏幕)
Simulates a drag gesture (touch, hold, and move) on this device's screen. (参数为起始点,终止点,及duration事件段的步骤数steps)
object
getProperty (string key) (获取设备参数)
Given the name of a system environment variable, returns its value for this device. The available variable names are listed in the detailed description of this method.
object
getSystemProperty (string key) (获取系统参数)
. The API equivalent of adb shell getprop <key>. This is provided for use by platform developers.
void
installPackage (string path) (安装包,参数为包路径)
Installs the Android application or test package contained in packageFile onto this device. If the application or test package is already installed, it is replaced.
dictionary
instrument (string className, dictionary args)
Runs the specified component under Android instrumentation, and returns the results in a dictionary whose exact format is dictated by the component being run. The component must already be present on this device.
void
press (string name, dictionary type) (按下键值为那么的键,type为按键动作类型)
Sends the key event specified by type to the key specified by keycode.
void
reboot (string into)
Reboots this device into the bootloader specified by bootloadType.
void
removePackage (string package) (卸载)
Deletes the specified package from this device, including its data and cache.
object
shell (string cmd) (发送adb shell 命令)
Executes an adb shell command and returns the result, if any.
void
startActivity (string uri, string action, string data, string mimetype, iterable (开启activity)categories dictionary extras, component component, flags)
Starts an Activity on this device by sending an Intent constructed from the supplied arguments.
MonkeyImage
takeSnapshot() (截图)
Captures the entire screen buffer of this device, yielding a MonkeyImage object containing a screen capture of the current display.
void
touch (integer x, integer y, integer type) (触摸坐标为x,y的地方,type为动作类型)
Sends a touch event specified by type to the screen location specified by x and y.
void
type (string message) (在焦点处输入信息)
Sends the characters contained in message to this device, as if they had been typed on the device's keyboard. This is equivalent to calling press() for each keycode in message using the key event type DOWN_AND_UP.
void
wake () (唤醒屏幕)
Wakes the screen of this device.
MonkeyImage:

Methods
string
convertToBytes (string format)
Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes.
tuple
getRawPixel (integer x, integer y) (获得x,y位置的ARGB矩阵值)
Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b).
integer
getRawPixelInt (integer x, integer y)
Returns the single pixel at the image location (x,y), as a 32-bit integer.
MonkeyImage
getSubImage (tuple rect) (截图,参数为一个矩形)
Creates a new MonkeyImage object from a rectangular selection of the current image.
boolean
sameAs (MonkeyImage other, float percent) (图片比对,percent为容忍出错率)
Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal".
void
writeToFile (string path, string format) (图片存为文件)
Writes the current image to the file specified by filename, in the format specified by format
六、简单例子

MonkeyRunner脚本是用python写的,所以要学习python。其实不难,自己理解理解,python简约而不简单的。

例子摘自网上,很小不过是可行的:

——————————————————————————
  以sample中的ApiDemos为例,先将其生成ApiDemos.apk。
  前提:已有device连接
  1、 将ApiDemos.apk放在$Android_Root\tools下。
  2、 在$Android_Root\tools下新建一个monkeyrunnerprogram.py文件,里面内容为:
  # Imports the monkeyrunner modules used by this program
  from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
  # 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('./ApiDemos.apk')
  # Runs the component
  device.startActivity(component='com.example.android.apis/.ApiDemos')
  # Presses the Menu button
  device.press('KEYCODE_MENU','DOWN_AND_UP')
  # Takes a screenshot
  result = device.takeSnapshot()
  # Writes the screenshot to a file
  result.writeToFile('./shot1.png','png')
  3、 打开命令行转到Android_Root\tools目录下运行一下命令:
  monkeyrunner monkeyrunnerprogram.py
110307 15:33:19.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:20.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:21.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] Error starting command: monkey --port 12345
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice]com.android.ddmlib.ShellCommandUnresponsiveException
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.Device.executeShellCommand(Device.java:276)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.monkeyrunner.adb.AdbMonkeyDevice$1.run(AdbMonkeyDevice.java:89)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.Executors$RunnableAdapter.call(UnknownSource)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask.run(Unknown Source)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.lang.Thread.run(Unknown Source)
110307 15:33:57.437:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: press KEYCODE_MENU.
110307 15:33:59.171:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: quit.
  注:里面exception的提示我们可以忽略。
  4、 可以Android_Root\tools下查看生成的shot1.png的截图。

实例扩展
  在实例五的基础上继续试验:
  1、 在$Android_Root\tools下新建一个monkeyrunnerprogram1.py文件,里面内容为:
  # Imports the monkeyrunner modules used by this program
  from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
  # Connects to the current device, returning a MonkeyDevice object
  device = MonkeyRunner.waitForConnection()
  # Takes a screenshot
  result = device.takeSnapshot()
  # Writes the screenshot to a file
  result.writeToFile('./shotbegin.png','png')
  # Presses the Down button
  device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
  device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
  device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
  device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
  device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
  # Takes a screenshot
  result = device.takeSnapshot()
  # Writes the screenshot to a file
  result.writeToFile('./shotend.png','png')
  2、 将画面定位在Apidemos的首页,并将光标定位在第一项上。
  3、 在$Android_Root\tools目录下运行一下命令:
  monkeyrunner monkeyrunnerprogram1.py

(该例子摘自:http://www.189works.com/article-13451-1.html

全文出自本人博客:http://iwangyue.cn(不二生的老窝)


菊子曰:专业的
博客管理软件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: