Monnkey使用的两种方式

Posted on 2014-05-21 22:42  willhar  阅读(195)  评论(0)    收藏  举报
monkeyrunner的实现有两种方式:
  一、通过cmd命令
  1、打开eclipse中android模拟器或在cmd命令窗口打开模拟器器。
  2、模拟器运行正常之后,在cmd窗口输入"monkeyrunner",进入shell命令模式。
  3、导入monkeyrunner所使用的模块,直接在命令行输入:from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice回车
  4、与模拟器‘对勾’的命令:device=MonkeyRunner.waitForConnection()
  5、连接成功之后可以安装apk应用程序了,安装成功之后会返回true,否则对传入的路径做检查是否有误。输入命令:device.installPackage(".../test.apk")
  6、启动任意的Activity,只要传入相应的package和activity即可,命令:device.startActivity(component="包名/activity名")
  此时模拟器可以打开相应的Activity,我们也可以向模拟器发送更多的操作命令,比如按键时间或滚动等等。
  二、使用python文件
  定义一个apptest.py文件,在cmd命令输入:monnkeyrunner apptest.py(绝对路径),可查看程序运行结果的截图。
  apptest.py内容如下:

#import 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 installtion work
device.installPackage("D:/com.tencent.qqmusic.apk")
device.startActivity(component="com.tencent.qqmusic/.activity.NewGuideActivity")
#press the menu button
device.press('KEYCODE_DPAP_DWON','DOWN_AND_UP')
#takes a screenshot
result=device.takeSnapshot()
#write screenshot to a file
result.writeToFile('D:/shot1.png','png') 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3