一个简单的monkeyrunner程序实例

上一篇 / 下一篇  2014-07-04 10:54:36 / 个人分类:Monkeyrunner

  

monkeyrunnerAPIcom.android.monkeyrunner包中包含三个模块:

  1、MonkeyRunner:这个类提供了用于连接monkeyrunner和设备或模拟器的方法,它还提供了用于创建用户界面显示提供了方法,,另外有个显示提示信息的alert方法比较常用。
  2、MonkeyDevice:代表一个设备或模拟器。这个类为安装和卸载包、开启Activity、发送按键和触摸事件、运行测试包等提供了方法。
  3、MonkeyImage:这个类提供了捕捉屏幕的方法。这个类为截图、将位图转换成各种格式、对比两个MonkeyImage对象、将image保存到文件等提供了方法。

python程序中,您将以Python模块的形式使用这些类。monkeyrunner工具不会自动导入这些模块。您必须使用类似如下的from语句:

  fromcom.android.monkeyrunner import 

其中,为您想要导入的类名。您可以在一个from语句中导入超过一个模块,其间以逗号分隔。

 

一个简单的monkeyrunner程序实例

#coding=utf-8   /加上这句可以支持中文注释

#导入此程序所需要的Monkeyrunner模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

#默认连接方式来连接当前设备,返回一个MoneyDevice对象。如果要连接多台设备,就需要写上设备名称

device = MonkeyRunner.waitForConnection()

##连接真机时需要加上deviceID(设备名),在cmd命令下,输入adb devices可以看到

#device1 = MonkeyRunner.waitForConnection(5,"1234567GHYFDB")

##连接模拟器

#device2 = MonkeyRunner.waitForConnection(5,"emulator-777")

#sets a variable:程序包名
package = 'com.founder.poetry'

#sets a variable:启动Activity
activity = 'com.founder.poetry.activities.SplashActivity'

#sets the name of the component to start
runComponent= package + '/' + activity

#Runs the component

device.startActivity(component=runComponent)

##也可以直接输入组件名称

#device.startActivity(component='com.founder.poetry/com.founder.poetry.activities.SplashActivity')

#pause 10 seconds,等待程序启动
MonkeyRunner.sleep(10.0)

#Presses the BACK button
device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)


TAG:

 

评分:0

我来说两句

Open Toolbar