monkeyrunner只执行截图,未执行按键的问题

在网上查找monkeyrunner的用法,抄了一段代码如下:

复制代码
 
 
1 # Imports the monkeyrunner modules used by this program 2   3   from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner.waitForConnection() 8 9 # Takes a screenshot 10 11 result = device.takeSnapshot() 12 13 # Writes the screenshot to a file 14 15 result.writeToFile( ' ./shotbegin.png ' , ' png ' ) 16 17 # Presses the Down button 18 19 device.press( ' KEYCODE_DPAD_DOWN ' , ' DOWN_AND_UP ' ) 20 21 device.press( ' KEYCODE_DPAD_DOWN ' , ' DOWN_AND_UP ' ) 22 23 device.press( ' KEYCODE_DPAD_DOWN ' , ' DOWN_AND_UP ' ) 24 25 device.press( ' KEYCODE_DPAD_DOWN ' , ' DOWN_AND_UP ' ) 26 27 device.press( ' KEYCODE_DPAD_DOWN ' , ' DOWN_AND_UP ' ) 28 29 # Takes a screenshot 30 31 result = device.takeSnapshot() 32 33 # Writes the screenshot to a file 34 35 result.writeToFile( ' ./shotend.png ' , ' png ' )
原地址:http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980086.html

运行时未报错,开头和结尾的截图执行成功,但中间的按键未执行。请教了同事,原来是因为时间太短,第一次截图未完立即发送按键指令。加入等待后运行正常。完整代码如下:

#encoding utf-8

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
import time

device=MonkeyRunner.waitForConnection()
device.installPackage("../samples/android-14/ApiDemos/bin/ApiDemos.apk")
device.startActivity(component='com.example.android.apis/.ApiDemos')
result=device.takeSnapshot()
result.writeToFile('./snapshotbegin.png','png')

time.sleep(5)

device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
time.sleep(1)
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
time.sleep(1)
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
time.sleep(1)
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
time.sleep(1)
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
time.sleep(1)

result=device.takeSnapshot()
result.writeToFile('./snapshotend.png','png')

----------------------------------------------------------------------------------------------------------------------------------------

刚刚知道monkeyrunner自带sleep函数,所以上面程序不需要import time了,time.sleep()可以换成MonkeyRunner.sleep()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值