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


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值