UAT_focusCommand.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. # -*- coding:utf-8 -*-
  2. import time,sys,os
  3. from UAT_tree import UATTree
  4. from UAT_log import error,info,debug
  5. from UAT_PathManage import UATPathManage
  6. from UIParamUtil import UIParamUtil
  7. # from UAT_runnerCommand import UATRunnerCommand
  8. from ssat_sdk.python_uiautomator import PyUIAutomator,DirectionManageAndroid,FocusManageAndroid
  9. import math
  10. import traceback
  11. ERROR = True
  12. INFO = True
  13. DEBUG = True # 上传SVN版本此参数关闭
  14. '''
  15. 采用uiautomator技术,处理界面定位问题,用于移动焦点到目标组件上
  16. '''
  17. class FocusCommand():
  18. cls = "FocusCommand"
  19. UIObjParam = UIParamUtil.UIObjParam
  20. UATParamMap = UIParamUtil.UATParamMap
  21. def __init__(self, runnerCommand):
  22. self.runnerCommand = runnerCommand
  23. self.pyU = PyUIAutomator()
  24. self.dm = DirectionManageAndroid()
  25. self.fm = FocusManageAndroid(self.pyU, self.dm)
  26. self.inLayout= False
  27. '''
  28. 解析option的焦点组件参数,返回focusView或者focus-select组件参数
  29. :param option: option字典
  30. :return 返回 focus焦点参数字典,采用setObjParam可以转换成UIObject识别需要的参数。
  31. 如果为None,则表示不需要焦点选中。
  32. '''
  33. def getFPFromOption(self, option):
  34. focusParam = None
  35. fsParam = option[UATTree.TAB_FOCUS_SELECT]
  36. fvParam = option[UATTree.TAB_FOCUSE_VIEW]
  37. if fsParam[UATTree.FS_Type].__len__() > 1:
  38. if fsParam[UATTree.FS_Type].lower() == "focus":
  39. fsParam["focus"] = True
  40. focusParam = fsParam
  41. elif fsParam[UATTree.FS_Type].lower() == "select":
  42. fsParam["select"] = True
  43. focusParam = fsParam
  44. elif fsParam[UATTree.FS_Type].lower() == "no":
  45. focusParam = None
  46. else:
  47. error(self.cls, "getFPFromOption", "Option %s focus-select参数异常。" % option[UATTree.TAB_NAME], ERROR)
  48. else:
  49. focusParam = fvParam
  50. return focusParam
  51. '''
  52. 解析option的焦点组件参数,返回focusView或者focus-select组件参数.
  53. 在getFPFromOption返回值上,采用setObjParam转换成UIObject识别需要的参数。
  54. :param option: option字典
  55. :return 返回 focus焦点UIObject参数字典.
  56. '''
  57. def getFPObjParam(self, option):
  58. focusParam = self.getFPFromOption(option)
  59. fpObjParam = UIParamUtil.setObjParam(focusParam)
  60. return fpObjParam
  61. '''
  62. 解析存储在UATree里的moveKey字典,得到按键类型、键值数组、最大尝试次数
  63. :param moveKey, parent字典中moveKey字典
  64. :param optionCount: moveKey对应的parent的option个数
  65. :return keyType, keyCodeList, Max_Trye
  66. '''
  67. def parseMoveKey(self,moveKey, optionCount):
  68. if moveKey[UATTree.Max_Try] != "":
  69. Max_Try = int(moveKey[UATTree.Max_Try])
  70. else:
  71. Max_Try = optionCount
  72. findDirection = ["down","up"]
  73. keyType = UATTree.Key_Event
  74. if moveKey[UATTree.Key_Event].__len__() > 1:
  75. findDirection = moveKey[UATTree.Key_Event]
  76. keyType = UATTree.Key_Event
  77. elif moveKey[UATTree.Key_IR].__len__() > 1:
  78. findDirection = moveKey[UATTree.Key_IR]
  79. keyType = UATTree.Key_IR
  80. elif moveKey[UATTree.Key_Input].__len__() > 1:
  81. inputCmd = moveKey[UATTree.Key_Input]
  82. #TODO input情况的处理
  83. return None,None,None
  84. else:
  85. return None,None,None
  86. return keyType, findDirection, Max_Try
  87. '''
  88. 在parent界面,将焦点移动到目标option上。
  89. 焦点定位:根据layout是不是限制焦点范围,进行焦点组件寻找,焦点组件类型:focus、select、focusView、long-click
  90. 目标定位:纯粹是根据optionView配置组建坐标定位
  91. :param parent:当前电视界面的parent字典
  92. :param option:目标option字典
  93. :return True/False
  94. '''
  95. def focusOptionView(self, parent, option,chooseType):
  96. #是否采用layout限制焦点判断范围
  97. layout = parent[UATTree.TAB_LAYOUT]
  98. print "focusOptionView,layout:",layout
  99. self.inLayout = layout[UATTree.Layout_Limit]
  100. # 找到目标optionView参数
  101. layoutUIObj = {}
  102. optionUIObjParam = UIParamUtil.setObjParam(option[UATTree.TAB_OPTION_VIEW])
  103. if self.inLayout == 1:
  104. layoutUIObjParam = UIParamUtil.setObjParam(layout)
  105. else:
  106. layoutUIObjParam = {}
  107. # 获取move key按键
  108. moveKey = parent[UATTree.TAB_MOVE_KEY]
  109. keyType, findDirection,Max_Try = self.parseMoveKey(moveKey, parent[UATTree.TAB_OPTION].__len__())
  110. if keyType is None:
  111. error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option 读取 move_key失败", ERROR)
  112. return False
  113. #获取optionview的View_sambounds参数
  114. osamBounds = option[UATTree.TAB_OPTION_VIEW][UATTree.View_sambounds]
  115. #获取焦点view参数
  116. focusParam = self.getFPFromOption(option)
  117. focusUIParam = UIParamUtil.setObjParam(focusParam)
  118. if focusParam is None:
  119. fsamBounds = []
  120. else:
  121. fsamBounds = focusParam[UATTree.View_sambounds]
  122. if chooseType.lower()== 'no':
  123. return self.getNoFocusedObj(option, Max_Try, findDirection, keyType, layoutUIObjParam)
  124. else:
  125. return self.toDestObj(parent, option, Max_Try, findDirection, keyType, layoutUIObjParam,fsamBounds,osamBounds)
  126. '''
  127. 传入uiautomator需要的目标组件参数,获取无聚焦属性的option对象
  128. '''
  129. def getNoFocusedObj(self,option, Max_Try, findDirection, keyType, layoutUIObjParam):
  130. count = 0
  131. directIndex = 0
  132. while(True):
  133. if count >= Max_Try and directIndex >= findDirection.__len__()-1:
  134. break
  135. time.sleep(0.1)
  136. destUIObject = self.getOptionUIObj(option, layoutUIObjParam)
  137. if destUIObject and destUIObject.exists():
  138. info(self.cls,"getNoFocusedObj","找到目标option %s"%option[UATTree.TAB_NAME],INFO)
  139. return True
  140. else:
  141. if count < Max_Try:
  142. count = count + 1
  143. elif count >= Max_Try and directIndex<findDirection.__len__() -1:
  144. count=0
  145. Max_Try *= 2
  146. directIndex = directIndex + 1
  147. self.fm.pressKeyByType(findDirection[directIndex], keyType)
  148. error(self.cls, "getNoFocusedObj", "执行%s 次查找,仍未找到目标焦点!!" % (str(Max_Try)), ERROR)
  149. return False
  150. '''
  151. 传入uiautomator需要的目标组件参数和焦点框参数,用于寻找目标。
  152. '''
  153. def toDestObj(self, parent, option, Max_Try=10, findDirections=["down","up"], keyType=UATTree.Key_Event,
  154. layoutUIParam={}, fsamBounds=[],osamBounds=[]):
  155. # 按照传入的方向寻找Max_Try次,如果仍未聚焦到选中area,则按照传入方向的反方向 反向寻找 2*Max_Try 次
  156. print "toDestObj,enter:",Max_Try,findDirections,keyType,fsamBounds,osamBounds
  157. count = 0
  158. Max_Try = Max_Try
  159. directIndex = 0
  160. focusCount = 0 # 当目标和聚焦点在一个页面同时存在时,从当前聚焦点到目标的移动次数
  161. focusedBoundsPre = {u'top': 0, u'left': 0, u'right': 0, u'bottom': 0} # 上一轮聚焦点的坐标
  162. destBoundsPre = {u'top': 0, u'left': 0, u'right': 0, u'bottom': 0} # 上一轮目标的坐标
  163. while (True):
  164. if count >= Max_Try and directIndex >= findDirections.__len__()-1:
  165. break
  166. # 等待聚焦效果刷新完成,稳定之后再获取相关的属性
  167. time.sleep(0.1)
  168. destUIObject = self.getOptionUIObj(option,layoutUIParam)
  169. focusParam = self.getFPFromOption(option)
  170. #option是否需要聚焦
  171. needFocus = True
  172. if focusParam.has_key(UATTree.FS_Type) and \
  173. focusParam[UATTree.FS_Type] == "no":
  174. needFocus = False
  175. if destUIObject and destUIObject.exists(): # 有聚焦属性的option存在不代表已聚焦,无聚焦属性的option存在即当作已聚焦
  176. try:
  177. retCode, focusedUIObject = self.getChooseUIObjP(parent, layoutUIParam,needFocus=needFocus)
  178. debug(self.cls, "toDestObj", "Parent %s 当前聚焦框retCode:%d" % (parent[UATTree.TAB_NAME],retCode), DEBUG)
  179. #如果焦点不存在,只需要判断optionView存在即可算是聚焦成功
  180. if retCode == self.Focus_Need_Not:
  181. return True
  182. #根据已获取到destUIObject和focusedUIObject,进行是否选中目标option判断,
  183. #未选中,则继续利用moveKey查找
  184. focusedBounds = focusedUIObject.info['bounds']
  185. info(self.cls,"toDestObj","当前聚焦框所在坐标:%s"%focusedBounds,INFO)
  186. destBounds = destUIObject.info['bounds']
  187. info(self.cls, "toDestObj", "已寻找%s次" % focusCount, INFO)
  188. info(self.cls,"toDestObj","目标Option %s坐标:%s"%(option[UATTree.TAB_NAME], destBounds),INFO)
  189. if self.hasFocusDest(focusedBounds, destBounds, fsamBounds=fsamBounds, osamBounds=osamBounds):
  190. info(self.cls,"toDestObj","成功聚焦到目标焦点:%s"%option[UATTree.TAB_NAME],INFO)
  191. return True
  192. # 如果往同一个方向跑了5次,聚焦坐标和目标坐标的位置都没变化,则说明目标可能为非聚焦,跑不动了
  193. if focusCount<5:
  194. direction = self.dm.getTargetDirection(focusedBounds, destBounds, findDirections)
  195. self.dm.goOneStep(self.pyU, direction, keyType)
  196. isSameFocused = self.dm.isSameBounds(focusedBoundsPre,focusedBounds) # 前一次聚焦点与当前聚焦点坐标对比
  197. isSameDest = self.dm.isSameBounds(destBoundsPre,destBounds)
  198. if isSameFocused == True and isSameDest == True:
  199. focusCount += 1
  200. focusedBoundsPre = focusedBounds
  201. destBoundsPre = destBounds
  202. if focusCount == 0:
  203. focusCount += 1 # 如果focusCount=0,则将当前聚焦点和目标坐标赋值给前一次
  204. focusedBoundsPre = focusedBounds
  205. destBoundsPre = destBounds
  206. else:
  207. error(self.cls, "toDestObj", "未找到目标焦点!!!", ERROR)
  208. return False
  209. except Exception,e:
  210. info(self.cls,"toDestObj","未获取到目标/焦点对象坐标:count:%d,Max_Try:%d,directIndex:%d"%(count,Max_Try,directIndex),INFO)
  211. traceback.print_exc()
  212. # 出现控件出现一半的时候,获取控件信息会报错
  213. if count < Max_Try:
  214. count = count + 1
  215. elif count >= Max_Try and directIndex < findDirections.__len__() -1:
  216. count=0
  217. Max_Try *= 2
  218. directIndex = directIndex + 1
  219. self.fm.pressKeyByType(findDirections[directIndex], keyType)
  220. # 如果界面中没有目标文字的控件出现,则按照传入的方向寻找Max_Try次;仍然未找到 则反方向寻找2*Max_Try次
  221. else:
  222. if count < Max_Try:
  223. count = count + 1
  224. elif count >= Max_Try and directIndex<findDirections.__len__() -1:
  225. count=0
  226. Max_Try *= 2
  227. directIndex = directIndex + 1
  228. self.fm.pressKeyByType(findDirections[directIndex], keyType)
  229. error(self.cls, "toDestObj", "执行%s 次查找,仍未找到目标焦点!!" % (str(Max_Try)), ERROR)
  230. return False
  231. '''
  232. 根据配置的样本焦点框和OptionView 区域坐标,计算是否聚焦
  233. :param focusedBounds 当前电视焦点框坐标
  234. :param destBounds 当前电视目标坐标
  235. :param fsamBounds 取样时的焦点框坐标
  236. :param osamBounds 取样时的目标坐标
  237. :return True/False True:焦点在目标上;False:焦点不在目标上
  238. '''
  239. def hasFocusDest(self,focusedBounds, destBounds, fsamBounds=[],osamBounds=[]):
  240. # print "hasFocusDest,focusedBounds,destBounds,fsamBounds,osamBounds:",focusedBounds, destBounds,fsamBounds,osamBounds
  241. if fsamBounds.__len__() < 1 or osamBounds.__len__()<1:
  242. return self.dm.isHasAnotherBounds(focusedBounds, destBounds)
  243. else:#在焦点框bounds特别大时,同时包含多个目标optionView时,用此方法判断是否选中。
  244. focusBounds = UIParamUtil.atxBounds2UATBounds(focusedBounds)
  245. destBounds = UIParamUtil.atxBounds2UATBounds(destBounds)
  246. print "fsamBounds,osamBounds"
  247. sdrate = self.calPointAngle(fsamBounds[0], osamBounds[0])
  248. sdLine = self.calPointLine(fsamBounds[0], osamBounds[0])
  249. print "focusBounds,destBounds"
  250. drate = self.calPointAngle(focusBounds[0],destBounds[0])
  251. dLine = self.calPointLine(focusBounds[0],destBounds[0])
  252. if abs(drate - sdrate) < 5 and abs(dLine - sdLine) < 30:
  253. return True
  254. else:
  255. return False
  256. '''
  257. 计算点p2相对点p1的角度
  258. '''
  259. def calPointAngle(self, p1, p2):
  260. angle = 0.0
  261. print "calPointAngle,p1,p2:", p1,p2
  262. dx = float(p2[0])-float(p1[0])
  263. dy = float(p2[1])-float(p1[1])
  264. print "calPointAngle,dx,dy:",dx,dy
  265. if dx == 0 and dy >= 0:
  266. return 90
  267. elif dx == 0 and dy < 0:
  268. return 180
  269. radian = math.atan(dy/dx) #计算出弧度值
  270. angle = 180 * radian/math.pi
  271. print "calPointAngle,angle,radian:",angle,radian
  272. return angle
  273. def calPointLine(self, p1,p2):
  274. print "calPointLine,p1,p2:", p1,p2
  275. dx = float(p2[0])-float(p1[0])
  276. dy = float(p2[1])-float(p1[1])
  277. line = round(math.sqrt(dx*dx + dy*dy),1)
  278. print "calPointLine,line:", line
  279. return line
  280. '''
  281. 检测option组件,在电视上是否被选中了
  282. :param option 数据字典
  283. :param layoutUIParam, parent的layout组件定位字典
  284. :param focusUIObj, 焦点组件
  285. :return -1:未进入option的页面,找不到option UIObject;0:进入了option的页面,未选中option;1 已经选中option
  286. -2:代表未找到焦点
  287. '''
  288. def checkOptionChoose(self, option, layoutUIParam, focusUIObj):
  289. optionUIObj = self.getOptionUIObj(option, layoutUIParam)
  290. if optionUIObj is None or optionUIObj.exists() is False:
  291. return -1
  292. if focusUIObj is None or focusUIObj.exists() is False:
  293. return -2
  294. focusParam = self.getFPFromOption(option)
  295. if focusParam is None:
  296. return 1
  297. try:
  298. focusedBounds = focusUIObj.info()["bounds"]
  299. destBounds = optionUIObj.info()['bounds']
  300. except Exception,e:
  301. return 0
  302. fsamBounds = focusParam[UATTree.View_sambounds]
  303. osamBounds = option[UATTree.TAB_OPTION_VIEW][UATTree.View_sambounds]
  304. if self.hasFocusDest(focusedBounds, destBounds,fsamBounds, osamBounds):
  305. return 1
  306. else:
  307. return 0
  308. def checkOptionExist(self, option, parent):
  309. # print "checkOptionExist,option:",option
  310. moveKey = parent[UATTree.TAB_MOVE_KEY]
  311. optionName = option['name']
  312. print "checkOptionExist,moveKey:", moveKey
  313. Max_Try = moveKey[UATTree.Max_Try]
  314. if Max_Try == "":
  315. Max_Try = 1
  316. else:
  317. Max_Try = int(Max_Try)
  318. Reverse_Max_Try = Max_Try * 2
  319. print "Max_Try:", Max_Try
  320. count = 0
  321. Reversecount = 0
  322. lastObj = ""
  323. while (True):
  324. if count >= Max_Try and Reversecount >= Reverse_Max_Try:
  325. break
  326. # 如果找到目标直接返回
  327. debug(self.cls,"checkOptionExist","optionView:%s"%option,DEBUG)
  328. opUIParam = UIParamUtil.setObjParam(option["optionView"])
  329. ###
  330. try:
  331. opOBJ = self.pyU.getUiObject2(opUIParam)
  332. opOBJInfo = opOBJ.info
  333. debug(self.cls,"checkOptionExist","opOBJInfo: %s"%opOBJInfo,DEBUG)
  334. break
  335. except Exception,e:
  336. opOBJ = None
  337. if count < Max_Try:
  338. flag = 1
  339. count += 1
  340. print "now count:", count
  341. else:
  342. flag = 0
  343. Reversecount += 1
  344. print "now Reversecount:", Reversecount
  345. self.runnerCommand.executeMoveKey(moveKey, flag)
  346. if opOBJ is not None and opOBJ.exists():
  347. info(self.cls, "checkOptionExist", "当前页面已找到option: %s" % optionName, INFO)
  348. return True
  349. info(self.cls, "checkOptionExist", "注意!!!当前页面未找到option: %s" % optionName, INFO)
  350. return False
  351. '''
  352. 根据option配置的焦点方式,返回当前页面焦点组件
  353. :param option: option字典
  354. :param layoutUIObj : option对应组件的根组件
  355. :return retCode,UIObject对象:reCode=0表示默认为不需要选中状态,retCode=1表示option默认为需要选中状态
  356. '''
  357. Focus_Need = 1
  358. Focus_Need_Not = 0
  359. def getChooseUIObj(self, option, layoutUIObj = None):
  360. debug(self.cls, "getChooseUIObj", "option name:"+option[UATTree.TAB_NAME],DEBUG)
  361. focusObjParam = self.getFPObjParam(option)
  362. debug(self.cls, "getChooseUIObj", "focusObjParam:"+str(focusObjParam)+"; layoutUIObj:"+str(layoutUIObj),DEBUG)
  363. if focusObjParam.__len__() <= 0:
  364. return self.Focus_Need_Not,None
  365. if layoutUIObj is None:
  366. return self.Focus_Need,self.pyU.getUiObject2(focusObjParam)
  367. else:
  368. return self.Focus_Need,layoutUIObj.child(**focusObjParam)
  369. '''
  370. 根据parent字典,检索parent是否有一个option被选中,任何一个备选中,返回option的UIObject
  371. :param parent: parent 字典
  372. :param needFocus: 目标option需要聚焦,则优先找焦点,再处理select type为no情况
  373. :return retCode,UIObject对象:reCode=0表示默认为不需要选中状态,retCode=1表示option默认为需要选中状态
  374. '''
  375. def getChooseUIObjP(self, parent, layoutUIParam, needFocus = True):
  376. if layoutUIParam.__len__() > 0:
  377. layoutUIObj = self.pyU.getUiObject2(layoutUIParam)
  378. else:
  379. layoutUIObj = None
  380. # 筛选焦点定位类型,减少重复判断
  381. focusOptionDict = self.reMuFocusOption(parent[UATTree.TAB_OPTION])
  382. debug(self.cls,"getChooseUIObjP","focusOptionDict:"+str(focusOptionDict.keys()),DEBUG)
  383. for optionName in focusOptionDict:
  384. option = focusOptionDict[optionName]
  385. retCode, uiObj = self.getChooseUIObj(option, layoutUIObj)
  386. if retCode == self.Focus_Need_Not and needFocus is False:
  387. return self.Focus_Need_Not, None
  388. elif retCode == self.Focus_Need and uiObj is not None and uiObj.exists():
  389. return retCode, uiObj
  390. return self.Focus_Need,None
  391. def reMuFocusOption(self, optionDict):
  392. retDict = {}
  393. for optionName in optionDict:
  394. option = optionDict[optionName]
  395. focusObjParam = self.getFPObjParam(option)
  396. if retDict.__len__() == 0:
  397. retDict[optionName] = option
  398. continue
  399. isSame = False
  400. for retName in retDict:
  401. retOption = optionDict[retName]
  402. retFocusObjParam = self.getFPObjParam(option)
  403. if UIParamUtil.cmpObjParam(focusObjParam, retFocusObjParam):
  404. isSame = True
  405. break
  406. if isSame is False:
  407. retDict[optionName] = option
  408. return retDict
  409. '''
  410. 根据option和layoutUIParam参数,获取option UIObject对象
  411. layoutUIParam参数有效,则在layout里查找子组件option,否则全界面查找子组件。
  412. '''
  413. def getOptionUIObj(self, option, layoutUIParam):
  414. debug(self.cls, "getOptionUIObj","OptionView:"+str(option[UATTree.TAB_OPTION_VIEW]),DEBUG)
  415. destUIParam = UIParamUtil.setObjParam(option[UATTree.TAB_OPTION_VIEW])
  416. debug(self.cls, "getOptionUIObj", "destUIParam:" + str(destUIParam), DEBUG)
  417. destUITextExist = False
  418. try:
  419. destUIText = destUIParam['text']
  420. destUITextExist = True
  421. except Exception, e:
  422. info(self.cls, "getOptionUIObj", "目标对象%s的optionView无text属性"%option[UATTree.TAB_NAME], INFO)
  423. if destUITextExist is True:
  424. #####################
  425. if type(destUIParam['text']) is type([]):
  426. destUIObjectExist = False
  427. tempTextList = destUIParam['text']
  428. txtCount = 0
  429. for txt in tempTextList:
  430. destUIParam['text'] = txt
  431. uix = self.pyU.dump_hierarchy()
  432. if txt not in uix:
  433. continue
  434. if layoutUIParam.__len__() > 0:
  435. layoutUIObj = self.pyU.getUiObject2(layoutUIParam)
  436. try:
  437. destUIObject = layoutUIObj.child(**destUIParam)
  438. destUIObjectInfo = destUIObject.info
  439. destUIObjectExist = True
  440. info(self.cls, "getOptionUIObj", "文本%s对应的option对象已找到" % txt, INFO)
  441. except Exception, e:
  442. nextText = tempTextList[txtCount + 1]
  443. error(self.cls, "getOptionUIObj", "文本%s对应的option对象未找到,匹配下一个文本%s" % (txt, nextText), ERROR)
  444. if destUIObjectExist is True:
  445. break
  446. else:
  447. try:
  448. destUIObject = self.pyU.getUiObject2(destUIParam)
  449. destUIObjectInfo = destUIObject.info
  450. destUIObjectExist = True
  451. info(self.cls, "getOptionUIObj", "文本%s对应的option对象已找到" % txt, INFO)
  452. except Exception, e:
  453. nextText = tempTextList[txtCount + 1]
  454. error(self.cls, "getOptionUIObj", "文本%s对应的option对象未找到,匹配下一个文本%s" % (txt, nextText), ERROR)
  455. if destUIObjectExist is True:
  456. break
  457. txtCount += 1
  458. else:
  459. if layoutUIParam.__len__() > 0:
  460. layoutUIObj = self.pyU.getUiObject2(layoutUIParam)
  461. destUIObject = layoutUIObj.child(**destUIParam)
  462. else:
  463. destUIObject = self.pyU.getUiObject2(destUIParam)
  464. #########################
  465. else:
  466. if layoutUIParam.__len__() > 0:
  467. layoutUIObj = self.pyU.getUiObject2(layoutUIParam)
  468. destUIObject = layoutUIObj.child(**destUIParam)
  469. else:
  470. destUIObject = self.pyU.getUiObject2(destUIParam)
  471. return destUIObject
  472. '''
  473. 检测parent,在电视上是否被选中了。一个option被选中,表示选中
  474. :param option 数据字典
  475. :return
  476. -3:代表UIView判断未通过;
  477. -2:代表选中了parent,但未找到焦点;
  478. -1:未进入parent的页面,找不到parent layout;
  479. 0:进入了parent的页面,未选中parent;
  480. 1:已经选中parent
  481. '''
  482. def checkParentChoose(self, parent):
  483. debug(self.cls, "checkParentChoose", "parent:" + parent[UATTree.TAB_NAME], DEBUG)
  484. chooseTypeDict = {}
  485. layoutParam = parent[UATTree.TAB_LAYOUT]
  486. layoutUIParam = UIParamUtil.setObjParam(layoutParam)
  487. layoutResId = layoutParam[UATTree.Layout_ID]
  488. uiView = parent[UATTree.TAB_UI_VIEW]
  489. uiViewResId = uiView[UATTree.View_ID]
  490. uiViewText = uiView[UATTree.View_Text]
  491. uiViewDesc = uiView[UATTree.View_Desc]
  492. # print "checkParentChoose,layoutUIParam:",layoutUIParam
  493. if layoutResId == "" and uiViewResId == "" and uiViewText == "" and uiViewDesc == "":
  494. debug(self.cls, "checkParentChoose",
  495. "Warning:Parent %s的Layout resId和UIView信息获取失败!!请注意检查UATree文件!!!" % parent[UATTree.TAB_NAME], DEBUG)
  496. return -1
  497. elif layoutUIParam.__len__() > 0:
  498. # 如果存在UIView信息,则先判断UIView
  499. isExist = self.checkUIViewExist(uiView)
  500. if isExist is False:
  501. info(self.cls, "checkParentChoose", "当前页面不存在Parent:%s的UIView组件,判断该界面非此parent" % parent[UATTree.TAB_NAME],
  502. INFO)
  503. return -3
  504. else:
  505. debug(self.cls, "checkParentChoose", "已识别出Parent:%s的UIView组件" % parent[UATTree.TAB_NAME], DEBUG)
  506. # 判断Layout是否存在
  507. layoutUIObj = self.pyU.getUiObject2(layoutUIParam)
  508. if layoutUIObj is None or layoutUIObj.exists() is False:
  509. info(self.cls, "checkParentChoose", "parent %s layout 不存在 "%parent[UATTree.TAB_NAME], INFO)
  510. return -1
  511. debug(self.cls, "checkParentChoose", "parent %s layout 存在"%parent[UATTree.TAB_NAME], DEBUG)
  512. # 获取焦点组件,判断是否在layout中
  513. retCode, focusObj = self.getChooseUIObjP(parent, layoutUIParam, needFocus=False)
  514. if retCode == self.Focus_Need_Not:
  515. debug(self.cls, "checkParentChoose",
  516. "已找到目标parent %s,不需要选中。" % (parent['name']), DEBUG)
  517. elif focusObj is not None and focusObj.exists():
  518. debug(self.cls, "checkParentChoose",
  519. "已找到目标parent %s,焦点在Layout中。" % (parent['name']), DEBUG)
  520. else:
  521. info(self.cls, "checkParentChoose",
  522. "已找到目标parent %s,但是焦点不在Layout中。" % (parent['name']), INFO)
  523. return -2
  524. # 判断parent中存在一个option,则标识parent被选中。注意:为了快速消失界面相应
  525. if layoutParam[UATTree.Layout_Limit] <> 1:
  526. layoutUIParam = {}
  527. optionUIObj = None
  528. optionExist = False
  529. for optionName in parent["option"]:
  530. option = parent["option"][optionName]
  531. ret = self.checkOptionExist(option,parent)
  532. if ret is True:
  533. info(self.cls, "checkParentChoose",
  534. "已找到目标parent %s,识别出该parent下的option %s。" % (parent['name'], option['name']),
  535. INFO)
  536. return 1
  537. info(self.cls, "checkParentChoose",
  538. "已找到目标parent %s,但是parent下的option未被发现。" % (parent['name']), INFO)
  539. return 0
  540. else:
  541. isExist = self.checkUIViewExist(uiView)
  542. if not isExist:
  543. return -3
  544. else:
  545. info(self.cls, "checkParentChoose",
  546. "识别出parent %s的UIView参数,判断处于该界面中" % (parent['name']), INFO)
  547. return 1
  548. '''
  549. 检测某个弹窗是否存在。
  550. 弹窗相关的参数放在UIView中配置。
  551. 当存在resId参数时,使用resId获取对象并获取文本,再与text做比较;
  552. 当不存在resId参数时,使用text直接获取对象,判断对象是否存在。
  553. 与checkParentChoose判断不一致,做特殊处理。
  554. :return 整型:0代表不存在,1代表存在
  555. '''
  556. def checkDialogExist(self, dialog):
  557. debug(self.cls, "checkDialogExist", "dialog:" + dialog["name"], DEBUG)
  558. dialogText = dialog[UATTree.TAB_UI_VIEW][UATTree.UIView_Text]
  559. dialogResId = dialog[UATTree.TAB_UI_VIEW][UATTree.UIView_ID]
  560. # print "checkDialogExist.dialogText:", dialogText
  561. # print "checkDialogExist.dialogResId:", dialogResId
  562. if dialogResId != "":
  563. textObj = self.pyU.getUiObject(resourceId=dialogResId)
  564. if textObj.exists:
  565. objText = textObj.info['text']
  566. # print "checkDialogExist.objText:", objText
  567. if dialogText in objText:
  568. return 1
  569. else:
  570. return 0
  571. else:
  572. return 0
  573. else:
  574. textObj = self.pyU.getUiObject(text=dialogText)
  575. if textObj.exists:
  576. return 1
  577. else:
  578. return 0
  579. def checkUIViewExist(self, uiView):
  580. #uiview未配置默认为检测通过
  581. if uiView.__len__() == 0:
  582. return True
  583. uiviewObjParam = UIParamUtil.setObjParam(uiView)
  584. uiViewObj = self.pyU.getUiObject2(uiviewObjParam)
  585. if uiViewObj is None or uiViewObj.exists is False:
  586. return False
  587. else:
  588. return True
  589. if __name__ == "__main__":
  590. uatPathManage = UATPathManage()
  591. pyU = PyUIAutomator()
  592. dm = DirectionManageAndroid()
  593. fm = FocusManageAndroid(pyU, dm)
  594. runnerCmd = UATRunnerCommand(uatPathManage, pyU, dm, fm)
  595. focusCmd = FocusCommand(runnerCmd)
  596. # ret = focusCmd.calPointAngle([0,0],[244,151])
  597. ret = focusCmd.calPointAngle([244,151],[107,641])
  598. # focusCmd.strToBounds('[801,116][1280,180]')
  599. # option = focusCmd.runnerCommand.uatPathManage.uatData.getOption("av_devices_settings")
  600. # parent = focusCmd.runnerCommand.uatPathManage.uatData.getParentByOption(option)
  601. # focusCmd.focusOptionView(parent, option)
  602. # print "dump_hierarchy 0:",time.time()
  603. # print focusCmd.pyU.dump_hierarchy()
  604. # print "dump_hierarchy 1:",time.time()
  605. # uiobjg = focusCmd.pyU.getUiObject()