如何phpstorm生成api文档 MonkeyRunner API Reference文档

下次自动登录
现在的位置:
& 综合 & 正文
MonkeyRunner_android 自动化脚本轻松编写, monkeyrunner easy api高级用法及封装库
python library, it's used for android ui automation testing. but it's can be exported to java language.
python 语言库, 用于andorid ui自动化. 此库依赖于monkeyrunner的执行环境.
wrapEasyMonkey库 ( monkeyrunner与view交互的库封装easy api)
Howto和使用文档 请访问
-------------------------------------------------------------------wrapEasyMonkey 1.1 版本
-------------------------------------------------------------------
请访问sourceforge进行下载:
教程及文档:
-------------------------------------------------------------------wrapEasyMonkey
-------------------------------------------------------------------
源码下载地址: 请见附件.
游客无法查看附件,请登录后查看。
build# release note:
Copyright (C)
Created on July 2 ,2012
import sys
DEBUG=False
repeatTimesOnError=15
ANDROID_HOME=os.environ['ANDROID_HOME'] if os.environ.has_key('ANDROID_HOME') else 'E:\\android-sdks\\android-sdks\\'
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.device import ViewNode
from com.android.chimpchat.core import TouchPressType
class wrapEasyMonkey:
Wrap easy monkey class
Copyright (C)
Created on july 2 ,2012
import sys
DEBUG=False
repeatTimesOnError=15
ANDROID_HOME=os.environ['ANDROID_HOME'] if os.environ.has_key('ANDROID_HOME') else 'E:\\android-sdks\\android-sdks\\'
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.device import ViewNode
from com.android.chimpchat.core import TouchPressType
class wrapEasyMonkey:
Wrap easy monkey class
def __init__(self,easyDevice,device):
print ("__int__: created the wrap easy monkey object")
self.easyDevice = easyDevice
self.device = device
self.DOWN = TouchPressType.DOWN.getIdentifier()
self.UP = TouchPressType.UP.getIdentifier()
self.DOWN_AND_UP = TouchPressType.DOWN_AND_UP.getIdentifier()
wrap easyMonkeyDevice by function
will return the view object if found. not found will return None.
def getView(self,id):
print 'getview object functions'
for tmp in range(repeatTimesOnError):
return By.id(id)
print ' %d time getView error , not found the view , will retry ' % tmp
MonkeyRunner.sleep(1)
print 'sorry , still can\'t get the view. please check the view is exist or not?'
return None
wrap get text function.
return text string content of the view object. If can not found the view object , will return None.
def getText(self,view):
print 'getview text functions'
for tmp in range(repeatTimesOnError):
return self.easyDevice.getText(view).encode('utf-8')
print ' %d time getText error , will retry ' % tmp
MonkeyRunner.sleep(1)
print 'sorry , still can\'t get the text. please check the view is exist or not , or does the view have text property?'
return None
wrap easyMonkeyDevice touch view function
return true or false (if cannot locate the view ,will return false)
def touchView(self,view,type):
print 'wrap touch view function'
for tmp in range(repeatTimesOnError):
self.easyDevice.touch(view,type)
return True
print ' %d time touch error , not found the view , will retry ' % tmp
if (tmp &1 & DEBUG):
print 'Please wait to touch the view'
MonkeyRunner.sleep(1)
print 'sorry , still can\'t touch view. please check the view is exist or not , or increase the repeat times variable?'
return False
def touchView(self,view,type):
print 'wrap touch view function'
for tmp in range(repeatTimesOnError):
self.easyDevice.touch(view,type)
return True
print ' %d time touch error , not found the view , will retry ' % tmp
if (tmp &1 & DEBUG):
print 'Please wait to touch the view'
MonkeyRunner.sleep(1)
print 'sorry , still can\'t touch view. please check the view is exist or not , or increase the repeat times variable?'
return False
wrap touch point function , touch screen position
return true or false
always return true actually
def touchPoint(self,x,y,type):
print 'wrap touch point function'
for tmp in range(repeatTimesOnError):
self.device.touch(x,y,type)
return True
print ' %d time touch point error ,
, will retry ' % tmp
MonkeyRunner.sleep(1)
print 'sorry , still can\'t touch point. please check the view is exist or not , or increase the repeat times variable?'
return False
def hasFocused(self,id):
print 'check the view is focused or not'
#hierarchyViewer = device.getHierarchyViewer()
#print hierarchyViewer.findViewById(id).hasFocus
for tmp in range(repeatTimesOnError):
hierarchyViewer = self.device.getHierarchyViewer()
return hierarchyViewer.findViewById(id).hasFocus
print ' %d time check focus error ,
, will retry ' % tmp
MonkeyRunner.sleep(1)
return False
def getPosition(self,id):
print 'check the view is focused or not'
for tmp in range(repeatTimesOnError):
hierarchyViewer = self.device.getHierarchyViewer()
print hierarchyViewer.findViewById(id).left
print hierarchyViewer.findViewById(id).top
print hierarchyViewer.findViewById(id).width
print hierarchyViewer.findViewById(id).height
return hierarchyViewer.findViewById(id).left
MonkeyRunner.sleep(1)
return None
def touchDialog(self,parentIdPosition,id,type):
print 'touch the dialog button , here need the parent id'
hierarchyViewer = self.device.getHierarchyViewer()
#print hierarchyViewer.findViewById(parentId).left
#print hierarchyViewer.findViewById(parentId).top
x = hierarchyViewer.findViewById(parentId).left + (hierarchyViewer.findViewById(parentId).width - hierarchyViewer.findViewById(id).width)/2
y = hierarchyViewer.findViewById(parentId).top + (hierarchyViewer.findViewById(parentId).height - hierarchyViewer.findViewById(id).height)/2
self.touchPoint(x,y,type)
def touchDialogButton(self,type):
print 'touch the dialog button , thru controling the direction key'
if type==1:
MonkeyRunner.sleep(1)
self.device.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
self.device.press('KEYCODE_ENTER',MonkeyDevice.DOWN_AND_UP)
if type==2:
self.device.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
self.device.press('KEYCODE_DPAD_RIGHT',MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
self.device.press('KEYCODE_ENTER',MonkeyDevice.DOWN_AND_UP)
if type==0:
MonkeyRunner.sleep(1)
self.device.press('KEYCODE_ENTER',MonkeyDevice.DOWN_AND_UP)
def type(self,content):
print 'device type the %s' % content
self.device.type(content)
def press(self,keycode,type):
print 'device press the %s ' % keycode
self.device.press(keycode,type)
'''def __init__(self,easyDevice,device):if DEBUG:print ("__int__: created the wrap easy monkey object")self.easyDevice = easyDeviceself.device = deviceself.DOWN = TouchPressType.DOWN.getIdentifier()self.UP = TouchPressType.UP.getIdentifier()self.DOWN_AND_UP = TouchPressType.DOWN_AND_UP.getIdentifier()'''wrap easyMonkeyDevice by functionwill return the view object if found. not found will return None.'''def getView(self,id):if DEBUG:print 'getview object functions'for tmp in range(repeatTimesOnError):try:return By.id(id)except:if DEBUG:print ' %d time getView error , not found the view , will retry ' % tmpMonkeyRunner.sleep(1)continueprint 'sorry , still can\'t get the view. please check the view is exist or not?'return None '''wrap get text function.return text string content of the view object. If can not found the view object , will return None.'''def getText(self,view):if DEBUG:print 'getview text functions'for tmp in range(repeatTimesOnError):try:return self.easyDevice.getText(view).encode('utf-8')except:if DEBUG:print ' %d time getText error , will retry ' % tmpMonkeyRunner.sleep(1)continueprint 'sorry , still can\'t get the text. please check the view is exist or not , or does the view have text property?'return None
'''wrap easyMonkeyDevice touch view functionreturn true or false (if cannot locate the view ,will return false)'''def touchView(self,view,type):if DEBUG:print 'wrap touch view function'for tmp in range(repeatTimesOnError):try:self.easyDevice.touch(view,type)return Trueexcept:if DEBUG:print ' %d time touch error , not found the view , will retry ' % tmpif (tmp &1 & DEBUG):print 'Please wait to touch the view'MonkeyRunner.sleep(1)continueprint 'sorry , still can\'t touch view. please check the view is exist or not , or increase the repeat times variable?'return False
def touchView(self,view,type):if DEBUG:print 'wrap touch view function'for tmp in range(repeatTimesOnError):try:self.easyDevice.touch(view,type)return Trueexcept:if DEBUG:print ' %d time touch error , not found the view , will retry ' % tmpif (tmp &1 & DEBUG):print 'Please wait to touch the view'MonkeyRunner.sleep(1)continueprint 'sorry , still can\'t touch view. please check the view is exist or not , or increase the repeat times variable?'return False
'''wrap touch point function , touch screen positionreturn true or falsealways return true actually'''def touchPoint(self,x,y,type):if DEBUG:print 'wrap touch point function'for tmp in range(repeatTimesOnError):try:self.device.touch(x,y,type)return Trueexcept:if DEBUG:print ' %d time touch point error ,
, will retry ' % tmpMonkeyRunner.sleep(1)continueprint 'sorry , still can\'t touch point. please check the view is exist or not , or increase the repeat times variable?'return False '''has focus''' def hasFocused(self,id):if DEBUG:print 'check the view is focused or not'#hierarchyViewer = device.getHierarchyViewer()
#print hierarchyViewer.findViewById(id).hasFocusfor tmp in range(repeatTimesOnError):try:hierarchyViewer = self.device.getHierarchyViewer()return hierarchyViewer.findViewById(id).hasFocusexcept:if DEBUG:print ' %d time check focus error ,
, will retry ' % tmpMonkeyRunner.sleep(1)continuereturn False
def getPosition(self,id):if DEBUG:print 'check the view is focused or not'for tmp in range(repeatTimesOnError):try:hierarchyViewer = self.device.getHierarchyViewer()print hierarchyViewer.findViewById(id).leftprint hierarchyViewer.findViewById(id).topprint hierarchyViewer.findViewById(id).widthprint hierarchyViewer.findViewById(id).heightreturn hierarchyViewer.findViewById(id).leftexcept:MonkeyRunner.sleep(1)continuereturn None
def touchDialog(self,parentIdPosition,id,type):if DEBUG:print 'touch the dialog button , here need the parent id'hierarchyViewer = self.device.getHierarchyViewer()#print hierarchyViewer.findViewById(parentId).left
#print hierarchyViewer.findViewById(parentId).top
x = hierarchyViewer.findViewById(parentId).left + (hierarchyViewer.findViewById(parentId).width - hierarchyViewer.findViewById(id).width)/2y = hierarchyViewer.findViewById(parentId).top + (hierarchyViewer.findViewById(parentId).height - hierarchyViewer.findViewById(id).height)/2print xprint yself.touchPoint(x,y,type)
def touchDialogButton(self,type):if DEBUG:print 'touch the dialog button , thru controling the direction key'if type==1:MonkeyRunner.sleep(1)self.device.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP)MonkeyRunner.sleep(1)self.device.press('KEYCODE_ENTER',MonkeyDevice.DOWN_AND_UP)if type==2:self.device.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP)MonkeyRunner.sleep(1)self.device.press('KEYCODE_DPAD_RIGHT',MonkeyDevice.DOWN_AND_UP)MonkeyRunner.sleep(1)self.device.press('KEYCODE_ENTER',MonkeyDevice.DOWN_AND_UP)if type==0:MonkeyRunner.sleep(1)self.device.press('KEYCODE_ENTER',MonkeyDevice.DOWN_AND_UP) def type(self,content):if DEBUG:print 'device type the %s' % contentself.device.type(content) def press(self,keycode,type): if DEBUG: print 'device press the %s ' % keycodeself.device.press(keycode,type)
1. 增加函数touchViewById 根据view id值获取view,并模拟触屏电击
2. hasFocused 函数判断该id的view是否当前被选中
3. 增加封装函数 type和press, sleep
4. 修改传入参数, 给wrapEasyMonkey库增加 成员变量, 减少以后每次调用函数时传入的参数.
-------------------------------------------------------------------wrapEasyMonkey
-------------------------------------------------------------------
build# 源码
Copyright (C)
Created on july 2 ,2012
import sys
DEBUG=False
repeatTimesOnError=15
ANDROID_HOME=os.environ['ANDROID_HOME'] if os.environ.has_key('ANDROID_HOME') else 'E:\\android-sdks\\android-sdks\\'
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.device import ViewNode
from com.android.chimpchat.core import TouchPressType
class wrapEasyMonkey:
Wrap easy monkey class
def __init__(self,easyDevice,device):
print ("__int__: created the wrap easy monkey object")
self.easyDevice = easyDevice
self.device = device
self.DOWN = TouchPressType.DOWN.getIdentifier()
self.UP = TouchPressType.UP.getIdentifier()
self.DOWN_AND_UP = TouchPressType.DOWN_AND_UP.getIdentifier()
wrap easyMonkeyDevice by function
will return the view object if found. not found will return None.
def getView(self,id):
print 'getview object functions'
for tmp in range(repeatTimesOnError):
return By.id(id)
print ' %d time getView error , not found the view , will retry ' % tmp
MonkeyRunner.sleep(1)
print 'sorry , still can\'t get the view. please check the view is exist or not?'
return None
wrap get text function.
return text string content of the view object. If can not found the view object , will return None.
def getText(self,view):
print 'getview text functions'
for tmp in range(repeatTimesOnError):
return self.easyDevice.getText(view).encode('utf-8')
<span style="color:#b1
&&&&推荐文章:
【上篇】【下篇】3233人阅读
学习(18)
/?uid-22381-action-viewspace-itemid-242682
1.其实Monkeyrunner自身是带有帮助手册的,里面有所有API的说明,可以输出为HTML或者TXT&#26684;式的文档。这对于我们来说,已经足够了。可是,这个帮助文档在哪呢?根据&developer网站上的说明,我们可以用这个命令来生成API
reference手册:
monkeyrunner&format&help.py&outfile&
其中,format可以是HTML和TXT。outfile就是咱们输出文档的路径。OK,说做咱就做,比如我们运行 monkeyrunner html help.py &mrapi.html&,回车。咦,报错了?Can't open sepcified script. 不对啊,这照着做也出错,这不坑die吗?是的,这次google&#20284;乎是估计留了个陷阱在这。在monkeyrunner的路径里面确实找不到help.py这个文件,不报错那就是SDK有错了。那该肿么办呢?我们可以看看monkeyrunner的源码,在Monkeyrunner.java中,可以看到静态的help方法可以生成所有帮助的字符串,那么可不可以利用这个方法来生成API帮助文档呢?实践证明是可以的。代码很简单,如下:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
content = MonkeyRunner.help(&text&);
f = open('help.txt', 'w');
f.write(content);
f.close();如果要生成html的话,替换第二行的参数为html,并将第三行的help.txt改为html即可。完毕后,我们可以保存为help.py文件(为方便起见我们最好将该文件放到monkeyrunner.bat同一目录)。然后我们直接运行 monkeyrunner help.py,等待2,3秒钟,OK,运行完毕,help.txt是不是已经在你&#30524;前了?呵呵,好好看看吧,所有API函数都在里面了。其实大家如果不想生成文档,想看在线的,也是可以的,android developer上都有,个人所爱吧。
2.另外还有一个问题是,有童鞋使用MonkeyImage的sameAs方法老是出错,我自己试了试,确实要出错,错误提示是:不能将IChimpImage对象转换为IMonkeyImage对象时要出错。再次查看源码,MonkeyImage.java中的sameAs函数有这么一个注释:// TODO: check if thisconversion works
IChimpImage ther = (IChimpImage) otherObject.__tojava__(IChimpImage.class);
看来明显google的工程师还没有验证过这个转换的正确性,并且很杯具地失败了。哎,真是天意弄人。幸好,有万能的python在,我们还是有办法滴。建议大家使用python的PIL库进行图片比较,一样可以达到非常好的效果并且要稳定很多。
3.还有一个常见的问题是模拟按键时的press方法,比如我们要模拟按一下向下键,本来可以这样写:device.press('KEYCODE_DPAD_DOWN', 'DOWN_AND_UP')但大家会发现要报错,并且是很奇怪的错: TypeError: press: The 3rd argument is required. 咋会这样呢?再看看MonkeyDevice的源码,原来DOWN_AND_UP是MonkeyDevice的静态字符串成员,应该是可以直接调用的。我们再换成
device.press('KEYCODE_DPAD_DOWN', MonkeyDevice.DOWN_AND_UP)
这下终于OK了,呵呵。
4还有一个常见的问题是,能否将monkeyrunner集成到eclipse中?答案是看你使用什么平台。如果是linux环境下,那没问题,完全可以,具体操作可以看看国外有个高手写的博客,需翻墙:/2011/03/using-android-monkeyrunner-from-eclipse.html&但如果你是用的window环境,那情况&#20284;乎就没有那么乐观了。因为google没有提供在windows环境下用于eclipse的monkeyrunner解释器,所以...基本上是8可能配置的,除非哪个高手将这个解释器做出来,呵呵。还是辛苦点,手写吧,也有利于真正锻炼大家的编程能力。
Monkeyrunner是基于python的脚本工具,虽然有一定的局限性,不过使用起来确实入门门槛较低,大家看看帮助,自己利用ApiDemos的example做例子多练练,应该还是可以很快入门的。
今天就先写到这里,大家在实际使用过程中有问题可以留言一起讨论讨论,共同进步!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:145560次
积分:1847
积分:1847
排名:第17184名
原创:45篇
评论:37条
(4)(1)(1)(3)(5)(1)(4)(1)(4)(1)(1)(8)(6)(1)(5)(6)monkeyrunner 简介说明文档的中英文对照_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
monkeyrunner 简介说明文档的中英文对照
上传于||文档简介
&&A&#8203;n&#8203;d&#8203;r&#8203;o&#8203;i&#8203;d&#8203;自&#8203;动&#8203;化&#8203;测&#8203;试&#8203;经&#8203;常&#8203;会&#8203;用&#8203;到&#8203;m&#8203;o&#8203;n&#8203;k&#8203;e&#8203;y&#8203;r&#8203;u&#8203;n&#8203;n&#8203;e&#8203;r&#8203;,&#8203;但&#8203;是&#8203;文&#8203;档&#8203;本&#8203;来&#8203;都&#8203;是&#8203;英&#8203;文&#8203;文&#8203;档&#8203;,&#8203;不&#8203;利&#8203;于&#8203;阅&#8203;读&#8203;。&#8203;这&#8203;个&#8203;是&#8203;一&#8203;个&#8203;翻&#8203;译&#8203;版&#8203;本&#8203;,&#8203;可&#8203;供&#8203;参&#8203;考&#8203;。
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩5页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢

我要回帖

 

随机推荐