monkeyrunner on eclipse

monkeyrunner是Android提供的自动化测试工具,而且它还提供了Python接口,使我们能够使用程序控制应用的行为,例如:安装应用,根据包名启动应用,点击、滑动事件等。在eclipse中使用monkeyrunner需要一些配置:

1.下载并安装python;

2.eclipse中安装python插件;

3.下载并安装jython.jar;

4.配置eclipse;

由于前3步都比较简单,这里就不过多描述了,直接第4步。

a.配置python解释器,点击Window->Preferences->PyDev->Interpreters,如下图所示:

b.配置jython,点击Window->Preferences->PyDev->Interpreters,另外,还要把monkeyrunner.jar添加到Libraries中,如下图所示:


c.接下来就可以创建jython项目,

d.最后就是创建python代码,一段简单的代码,从Android官网上拷贝的,这段代码包括行为有:安装MyApplication.apk,打开MainActivity,点击Menu

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')
e.运行代码,实际上我执行这段代码,要靠Android提供的monkeyrunner.bat的,所以不能像平常运行程序一样,右键->Run->选择执行程序,这时候要用到eclipse的External Tools,点击Run->External Tools->External Tools Configurations,然后添加相关配置信息,如下图:


以后,每次执行的时候,就不用重新添加了。

参考资料:http://forum.xda-developers.com/showthread.php?t=2566234


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。