monkeyrunner 触发按键几种方式

上一篇 / 下一篇  2013-11-01 14:49:55 / 个人分类:android

转载自http://blog.csdn.net/tzh2009/article/details/8152459

一:monkeyunner控制多台设备

根据monkeyrunner提供的函数:

MonkeyDevicewaitForConnection(floattimeout,stringdeviceId)

Tries to make a connection between themonkeyrunnerbackend and the specified device or emulator.

Arguments
timeoutThe number of seconds to wait for a connection. The default is to wait forever.
deviceIdA regular expression that specifies the serial number of the device or emulator. See the topicAndroid Debug Bridge for a description of device and emulator serial numbers.
Returns
  • AMonkeyDeviceinstance for the device or emulator. Use this object to control and communicate with the device or emulator.
[python]
  1. :\>adb devices
  2. ist of devices attached
  3. 123456789ABCDEFdevice
  4. mulator-5554device

  5. :\>monkeyrunner
  6. ython2.5.0(Release_2_5_0:6476, Jun162009,13:33:26)
  7. JavaHotSpot(TM) Client VM (OracleCorporation)] on java1.7.0_05
  8. >>fromcom.android.monkeyrunnerimportMonkeyRunner, MonkeyDevice
  9. >> device1 = MonkeyRunner.waitForConnection(1,"emulator-5554")
  10. >> device2 = MonkeyRunner.waitForConnection(1,"0123456789ABCDEF")
  11. 就可以控制多台设备或者虚拟机。

通过id触摸屏幕

其中id可以通过和monkeyrunner同级目录即tools\hierarchyviewer.bat获得

[python]
  1. fromcom.android.monkeyrunnerimportMonkeyRunner,MonkeyDevice,MonkeyImage
  2. fromcom.android.monkeyrunner.easyimportEasyMonkeyDevice,By

  3. device = MonkeyRunner.waitForConnection(1.0,'0123456789ABCDEF')

  4. device.startActivity(component='com.android.browser/.BrowserActivity')
  5. MonkeyRunner.sleep(5)

  6. easy_device = EasyMonkeyDevice(device)
  7. easy_device.touch(By.id('id/one'),MonkeyDevice.DOWN_AND_UP)

三:通过id触摸屏幕,多个Id名字相同

用hierarchyviewer.bat去获取id,但是同目录很多id都相同,比如setting下所有TextView id都是id/title

解决方法一:

[python]
  1. device.press('KEYCODE_ENTER','DOWN_AND_UP')

  2. evice.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')

  3. evice.press('KEYCODE_DPAD_UP','DOWN_AND_UP')

  4. device.press('KEYCODE_DPAD_RIGHT','DOWN_AND_UP')

  5. device.press('KEYCODE_DPAD_LEFT','DOWN_AND_UP')

来控制上下左右移动

解决方法二:

当两个ID名称相同时,可以使用层级进行定位

easy_device.touch(By.id('id/parent_button'),MonkeyDevice.DOWN_AND_UP,By.id('id/current_button'),MonkeyDevice.DOWN_AND_UP)

但是这个方法自己试过没有成功,也许是父子id没有找对。

解决方法三:ViewClient

下载viewclient包:

http://sourceforge.net/projects/wrapeasymonkey/files/wrapEasyMonkey/1.0/

解压添加到环境变量,ANDROID_VIEW_CLIENT_HOME,具体操作参考文章

http://blog.csdn.net/tzh2009/article/details/8152382

可以用findViewWithAttribute获取text变量来控制。

PS:速度真的很慢,不是很喜欢。

[python]
  1. importre
  2. importsys
  3. importos
  4. importstring
  5. importlocale
  6. # this must be imported before MonkeyRunner and MonkeyDevice,
  7. # otherwise the import fails

  8. try:
  9. ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME']
  10. exceptKeyError:
  11. print>>sys.stderr,"%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment"% __file__
  12. sys.exit(1)
  13. sys.path.append(ANDROID_VIEW_CLIENT_HOME +'/src')
  14. sys.setdefaultencoding('utf-8')

  15. fromcom.dtmilano.android.viewclientimportViewClient
[python]
  1. device = MonkeyRunner.waitForConnection()
[python]
  1. vc = ViewClient(device)
[python]
  1. <p><spanclass="pln">view </span><spanclass="pun">=</span><spanclass="pln"> vc</span><spanclass="pun">.</span><spanclass="pln">findViewWithText</span><spanclass="pun">(</span><spanclass="str"><a href="mailto:'@@@@@'">'@@@@@'</a></span><spanclass="pun">)</span></p><p><spanclass="pun"></span><spanclass="pln">view</span><spanclass="pun">.</span><spanclass="pln">touch</span><spanclass="pun">()</span><spanclass="pln"></span></p>

四:利用monkeyrunner往文件写log

[python]
  1. #write file to pc
  2. log = open('d:\\MonkeyruunerTestlog\\tmp.txt','w')
  3. log.write("strings")
  4. log.close()

五:获取测试DUT的系统时间和PC时间

[python]
  1. #get device date&time
  2. date = device.shell('date +%Y%m%d%H%M%S')
[python]
  1. #get the PC time
[python]
  1. importtime
  2. date = time.strftime('%Y%m%d%H%M%S')

六:图片比较

用MonkeyRunner.loadImageFromFile()来添加电脑上已经存在的图片,与设备上的截图相比较;

[python]
  1. imageA = MonkeyRunner.loadImageFromFile('D:\\WORKSF\\SAQtools\\Monkeyrunner\\scripts\\test12.png','png')
  2. image=device.takeSnapshot()
  3. #image.writeToFile('D:\\WORKSF\\SAQtools\\Monkeyrunner\\scripts\\test12A.png','png')
  4. printimage.sameAs(imageA,0.95)
  5. ifimage.sameAs(imageA,0.95):
  6. log12.write("Download the doc file success,testis PASS \n")

  7. else:
  8. log12.write("Fail to download the doc file or the doc file not open successed,please check")

TAG:

nightxxxx的个人空间 引用 删除 nightxxxx   /   2013-11-07 17:24:51
感谢,比较常用但麻烦的东西都有了
 

评分:0

我来说两句

我的栏目

日历

« 2024-03-28  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 7125
  • 日志数: 3
  • 建立时间: 2013-05-24
  • 更新时间: 2013-11-01

RSS订阅

Open Toolbar