"""
对Selenium2Library element操作的健壮性封装、普通操作不受影响.
每个element操作方法生成一个操作对象
‘‘‘ 2类对象:
father类型: fatherName=None eleMethodName=webObj.browserName,
一般在webObj创建时配套创建, _eleMethod_objDc中记录,形如{fatherName:{}}
非father型: fatherName!=None eleMethodName=webObj.b下的eleMethodName
在webObj中以"self.w.eleMethodName()"形式使用、首次使用时创建
_eleMethod_objDc中记录,形如{fatherName:{eleMethodName:eleMethodObj}}
‘‘‘
exam:
class WebObj(class):
def __init__(url, brType="ff", brName="1"):
self.b = Selenium2Library()
self.b.open_browser(url, browser=brType, alias=brName)
self.w = WebEleCall(‘__%s‘%brName, webObj=self, fatherName=None)
#end class
web = WebObj("http://www.baidu.com")
web.w.get_text(locator, _TryTimeout_=3) #等效于如下代码:
‘‘‘
for i in range(0,3):
try:
web.b.self.b.wait_until_element_is_visible(locator, _TryTimeout_/3)
except,eObj:
print eObj
web.b.get_text(locator)
‘‘‘
"""
1 # 在webObj创建时配套创建 2 class WebEleCall(object): 3 """ 4 对Selenium2Library element操作的健壮性封装、普通操作不受影响. 5 每个element操作方法生成一个操作对象 6 ‘‘‘ 2类对象: 7 father类型: fatherName=None eleMethodName=webObj.browserName, 8 一般在webObj创建时配套创建, _eleMethod_objDc中记录,形如{fatherName:{}} 9 非father型: fatherName!=None eleMethodName=webObj.b下的eleMethodName 10 在webObj中以"self.w.eleMethodName()"形式使用、首次使用时创建 11 _eleMethod_objDc中记录,形如{fatherName:{eleMethodName:eleMethodObj}} 12 ‘‘‘ 13 exam: 14 class WebObj(class): 15 def __init__(url, brType="ff", brName="1"): 16 self.b = Selenium2Library() 17 self.b.open_browser(url, browser=brType, alias=brName) 18 self.w = WebEleCall(‘__%s‘%brName, webObj=self, fatherName=None) 19 #end class 20 web = WebObj("http://www.baidu.com") 21 web.w.get_text(locator, _TryTimeout_=3) #等效于如下代码: 22 ‘‘‘ 23 for i in range(0,3): 24 try: 25 web.b.self.b.wait_until_element_is_visible(locator, _TryTimeout_/3) 26 except,eObj: 27 print eObj 28 web.b.get_text(locator) 29 ‘‘‘ 30 """ 31 _eleMethod_objDc = {} #每个fatherObj.eleMethod对应一个操作对象 32 @classmethod 33 def _get_eleMethods_(cls, browserObj): 34 _eleMethods = [] 35 for i in dir(browserObj): 36 iObj = getattr(browserObj, i) 37 if not (i[0].islower() and hasattr(iObj, ‘__call__‘)): 38 continue 39 try: 40 iargs = inspect.getargspec(iObj).args 41 except BaseException,eObj: 42 print "i=%s , eObj={%s}"%(i,eObj) 43 continue 44 if len(iargs)>1 and iargs[1]==‘locator‘: 45 _eleMethods.append(i) 46 return _eleMethods 47 def __init__(self, eleMethodName, webObj, fatherName=None): 48 self.eleMethodName = eleMethodName 49 self.b = webObj.b 50 self.web = webObj 51 self.fatherName = fatherName # 52 if fatherName==None: # 父对象自己的fatherName=None 53 self.__class__._eleMethod_objDc[eleMethodName] = {} 54 self._TryTimeout_ = 3 55 self._eleMethods = WebEleCall._get_eleMethods_(self.b) #获取所有的eleMethods 56 def __getattr__(self, unkonwnAttr): #封装Selenium2Library的方法 57 if unkonwnAttr in self._eleMethods: 58 eleMethodName = unkonwnAttr 59 fatherName = self.eleMethodName 60 cls = self.__class__ 61 if unkonwnAttr in cls._eleMethod_objDc[fatherName].keys(): 62 obj = cls._eleMethod_objDc[fatherName][eleMethodName] 63 else: 64 obj = cls(eleMethodName, webObj=self.web, fatherName=fatherName) 65 cls._eleMethod_objDc[fatherName][eleMethodName] = obj 66 return obj 67 else: 68 return getattr(self.b, unkonwnAttr) 69 def __call__(self, locator, *args, **kw): 70 print " --> b.%s(‘%s‘, *%s, **%s)"%(self.eleMethodName,locator,args,kw) 71 if locator==None: 72 print "assert: locator==None" 73 _TryTimeout_ = None 74 if ‘_TryTimeout_‘ in kw.keys(): 75 _TryTimeout_ = kw[‘_TryTimeout_‘] 76 kw.pop(‘_TryTimeout_‘) 77 if self.eleMethodName==‘wait_until_element_is_visible‘ and (‘timeout‘ in kw.keys()): 78 _TryTimeout_ = kw[‘timeout‘] 79 if _TryTimeout_==None: _TryTimeout_=self._TryTimeout_ 80 if self.eleMethodName!=‘‘: 81 msgPre = "b.wait_until_element_is_visible(%s)"%locator 82 if _TryTimeout_!=self._TryTimeout_: 83 print " --> b.wait_until_element_is_visible(%s, _TryTimeout_=%s)"%(locator,_TryTimeout_) 84 pyx.Try_withTimes(_TryTimes=3, _TryGap=0, _msgPre=msgPre, 85 _reFunc=None, _reFKw=None, 86 _func=self.b.wait_until_element_is_visible, locator=locator, timeout=_TryTimeout_/3) 87 eleMethod = getattr(self.b, self.eleMethodName) 88 return eleMethod(locator, *args, **kw)
class WebEleCall
1 def Try_withTimes(_TryTimes,_TryGap,_msgPre,_reFunc,_reFKw,_func, *fargs, **fKw): 2 """ 当reFunc想表示为空时,建议为time.sleep(0.01) """ 3 if _reFunc==None: 4 _reFunc = str 5 _reFKw = {} 6 for i in range(_TryTimes): 7 try: 8 iret = _func(*fargs, **fKw) 9 break 10 except BaseException,ierrObj: 11 ierr = err_objToStr(ierrObj, ifDetail=False) 12 logging.warning(" ----> %s() times=%s, ierr={%s}"%(_msgPre, (i+1), ierr)) 13 try: 14 _reFunc(**_reFKw) 15 except BaseException,jerrObj: 16 jerr = err_objToStr(jerrObj) 17 logging.warning(" ----> %s().%s times=%s, jerr={%s}" 18 %(_msgPre,_reFunc, (i+1), jerr)) 19 if (i+1<_TryTimes): 20 time.sleep(_TryGap) 21 else: 22 assert False, "----> %s() Fail. ierr={%s}"%(_msgPre,ierr) 23 return iret
pyx.Try_withTimes
# 有兴趣的可以加我QQ406032224