码迷,mamicode.com
首页 > 系统相关 > 详细

monkeyrunner on eclipse

时间:2014-11-19 22:28:32      阅读:545      评论:0      收藏:0      [点我收藏+]

标签:android测试   jython   monkeyrunner   

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

1.下载并安装python;

2.eclipse中安装python插件;

3.下载并安装jython.jar;

4.配置eclipse;

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

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

bubuko.com,布布扣

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

bubuko.com,布布扣

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

bubuko.com,布布扣

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,然后添加相关配置信息,如下图:

bubuko.com,布布扣

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

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


monkeyrunner on eclipse

标签:android测试   jython   monkeyrunner   

原文地址:http://blog.csdn.net/hello0370/article/details/41285041

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!