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和使用文档 请访问 blog文章
-------------------------------------------------------------------
wrapEasyMonkey 1.1 版本
-------------------------------------------------------------------
请访问sourceforge进行下载: sourceforge.net/projects/wrapeasymonkey/files/
教程及文档: sourceforge.net/p/wrapeasymonkey/wiki/Home/

-------------------------------------------------------------------
wrapEasyMonkey 2012 7.10 版本
-------------------------------------------------------------------
源码下载地址: 请见附件.
游客无法查看附件,请登录后查看。

build#20120710 release note:
 
 
'''
Copyright (C) 2012 whoistester.com
Created on July 2 ,2012
@auther: admin#whoistester.com
 
'''
 
import sys
import os
 
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) 2012 whoistester.com
Created on july 2 ,2012
@auther: admin#whoistester.com (#->@)
 
'''
 
import sys
import os
 
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):
		if DEBUG:
			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):
		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 ' % tmp
				MonkeyRunner.sleep(1)
				continue
		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):
		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 ' % tmp
				MonkeyRunner.sleep(1)
				continue
		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):
		if DEBUG:
			print 'wrap touch view function'
		for tmp in range(repeatTimesOnError):
			try:
				self.easyDevice.touch(view,type)
				return True
			except:
				if DEBUG:
					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)
				continue
			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):
		if DEBUG:
			print 'wrap touch view function'
		for tmp in range(repeatTimesOnError):
			try:
				self.easyDevice.touch(view,type)
				return True
			except:
				if DEBUG:
					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)
				continue
			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):
		if DEBUG:
			print 'wrap touch point function'
		for tmp in range(repeatTimesOnError):
			try:
				self.device.touch(x,y,type)
				return True
			except:
				if DEBUG:
					print ' %d time touch point error ,  , will retry ' % tmp
				MonkeyRunner.sleep(1)
				continue
			print '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).hasFocus
	
		for tmp in range(repeatTimesOnError):
			try:
				hierarchyViewer = self.device.getHierarchyViewer()
				return hierarchyViewer.findViewById(id).hasFocus
			except:
				if DEBUG:
					print ' %d time check focus error ,  , will retry ' % tmp
				MonkeyRunner.sleep(1)
				continue
		return 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).left
				print hierarchyViewer.findViewById(id).top
				print hierarchyViewer.findViewById(id).width
				print hierarchyViewer.findViewById(id).height
				return hierarchyViewer.findViewById(id).left
			except:
				MonkeyRunner.sleep(1)
				continue
		return 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)/2
		y = hierarchyViewer.findViewById(parentId).top + (hierarchyViewer.findViewById(parentId).height - hierarchyViewer.findViewById(id).height)/2
		print x
		print y
		self.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' % content
		self.device.type(content)
 
	def press(self,keycode,type):
 
		if DEBUG: 
			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 2012 7.2 版本
-------------------------------------------------------------------

build#20100702 源码

 
 
'''
Copyright (C) 2012 whoistester.com
Created on july 2 ,2012
@auther: admin#whoistester.com (#->@)
 
'''
 
import sys
import os
 
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):
		if DEBUG:
			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):
		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 ' % tmp
				MonkeyRunner.sleep(1)
				continue
		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):
		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 ' % tmp
				MonkeyRunner.sleep(1)
				continue
		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):
		if DEBUG:
			print 'wrap touch view function'
		for tmp in range(repeatTimesOnError):
			try:
				self.easyDevice.touch(view,type)
				return True
			except:
				if DEBUG:
					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)
				continue
			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):
		if DEBUG:
			print 'wrap touch view function'
		for tmp in range(repeatTimesOnError):
			try:
				self.easyDevice.touch(view,type)
				return True
			except:
				if DEBUG:
					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)
				continue
			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):
		if DEBUG:
			print 'wrap touch point function'
		for tmp in range(repeatTimesOnError):
			try:
				self.device.touch(x,y,type)
				return True
			except:
				if DEBUG:
					print ' %d time touch point error ,  , will retry ' % tmp
				MonkeyRunner.sleep(1)
				continue
			print '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).hasFocus
	
		for tmp in range(repeatTimesOnError):
			try:
				hierarchyViewer = self.device.getHierarchyViewer()
				return hierarchyViewer.findViewById(id).hasFocus
			except:
				if DEBUG:
					print ' %d time check focus error ,  , will retry ' % tmp
				MonkeyRunner.sleep(1)
				continue
		return 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).left
				print hierarchyViewer.findViewById(id).top
				print hierarchyViewer.findViewById(id).width
				print hierarchyViewer.findViewById(id).height
				return hierarchyViewer.findViewById(id).left
			except:
				MonkeyRunner.sleep(1)
				continue
		return 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)/2
		y = hierarchyViewer.findViewById(parentId).top + (hierarchyViewer.findViewById(parentId).height - hierarchyViewer.findViewById(id).height)/2
		print x
		print y
		self.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' % content
		self.device.type(content)
 
	def press(self,keycode,type):
 
		if DEBUG: 
			print 'device press the %s ' % keycode
		self.device.press(keycode,type)			
 



你可能感兴趣的:(android,object,api,function,脚本,import)