UAT_runnerCommand.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. # -*- coding:utf-8 -*-
  2. from UAT_tree import UATTree
  3. from ssat_sdk.python_uiautomator import PyUIAutomator,FocusManageAndroid, DirectionManageAndroid
  4. from UAT_log import error,debug,info
  5. from ssat_sdk.utils import string_util
  6. from ssat_sdk.tv_operator import TvOperator
  7. from ssat_sdk.device_manage.capturecard_manager import CCardManager
  8. from ssat_sdk.sat_environment import getSATTmpDIR
  9. from ssat_sdk.pic_tool import ImageCMP
  10. from ssat_sdk.ocr_convert import OCRConvert
  11. from ssat_sdk.utils.string_util import strcmp, getDigitFromString
  12. from UAT_valueCommand import ValueCommand
  13. from UAT_focusCommand import FocusCommand
  14. import cv2 as cv
  15. import os, sys, time
  16. DEBUG = True
  17. INFO =True
  18. ERROR = True
  19. class UATRunnerCommand():
  20. cls = "UATRunnerCommand"
  21. def __init__(self, uatPathManage, pyU, dm, fm):
  22. self.uatPathManage = uatPathManage
  23. self.pyU = pyU
  24. self.dm = dm
  25. self.fm = fm
  26. self.tvOperator = TvOperator()
  27. self.eventKeyCode = self.uatPathManage.uatData.getKeyCodeDict()
  28. self.CC = CCardManager()
  29. self.imgCMP = ImageCMP()
  30. self.ocrConvert = OCRConvert()
  31. self.valueCmd = ValueCommand( self)
  32. self.focusCmd = FocusCommand(self)
  33. #检测option组件是不是在电视上显示了
  34. def checkOptionExist(self, option, parent):
  35. return self.focusCmd.checkOptionExist(option,parent)
  36. '''
  37. 检测parent,在电视上是否被选中了。一个option被选中,表示选中
  38. :param option 数据字典
  39. :return
  40. -3:代表UIView判断未通过;
  41. -2:代表选中了parent,但未找到焦点;
  42. -1:未进入parent的页面,找不到parent layout;
  43. 0:进入了parent的页面,未选中parent;
  44. 1:已经选中parent
  45. '''
  46. def checkParentChoose(self, parent):
  47. return self.focusCmd.checkParentChoose(parent)
  48. def focusTargetOption(self, parent, option):
  49. if parent[UATTree.TAB_MOVE_KEY][UATTree.Max_Try] != "":
  50. Max_Try = int(parent[UATTree.TAB_MOVE_KEY][UATTree.Max_Try])
  51. else:
  52. Max_Try = parent[UATTree.TAB_OPTION].__len__()
  53. moveKey = parent[UATTree.TAB_MOVE_KEY]
  54. print "moveKey:", moveKey
  55. findDirection = ""
  56. keyType = UATTree.Key_Event
  57. if moveKey[UATTree.Key_Event].__len__() > 1:
  58. findDirection = moveKey[UATTree.Key_Event][0]
  59. keyType = UATTree.Key_Event
  60. elif moveKey[UATTree.Key_IR].__len__() > 1:
  61. findDirection = moveKey[UATTree.Key_IR][0]
  62. keyType = UATTree.Key_IR
  63. elif moveKey[UATTree.Key_Input].__len__() > 1:
  64. inputCmd = moveKey[UATTree.Key_Input]
  65. elif moveKey[UATTree.Key_AM].__len__() > 1:
  66. amCmd = moveKey[UATTree.Key_AM]
  67. else:
  68. error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option 读取 move_key失败", ERROR)
  69. return False
  70. chooseType = option[UATTree.TAB_FOCUS_SELECT][UATTree.FS_Type]
  71. optionView = option[UATTree.TAB_OPTION_VIEW]
  72. viewId = optionView[UATTree.View_ID]
  73. viewText = optionView[UATTree.View_Text]
  74. viewIndex = optionView[UATTree.View_Index]
  75. viewClass = optionView[UATTree.View_Class]
  76. viewDesc = optionView[UATTree.View_Desc]
  77. recyclerViewId = parent[UATTree.TAB_LAYOUT][UATTree.Layout_ID]
  78. focuseView = option[UATTree.TAB_FOCUSE_VIEW]
  79. focuseViewId = focuseView[UATTree.View_ID]
  80. others = parent[UATTree.TAB_OTHERS]
  81. hb_keyDict = others[UATTree.Key_HeartBeat]
  82. print "%s chooseType: %s"%(option["name"], chooseType)
  83. if chooseType.__len__() > 1:
  84. if chooseType.lower() == "focus" or chooseType.lower() == "select" or chooseType.lower() == "no":
  85. return self.focusCmd.focusOptionView(parent,option,chooseType)
  86. elif chooseType.lower() == "long_click":
  87. info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " going to long click type!!!", info)
  88. targetObj = self.pyU.getUiObject(resourceId=viewId)
  89. if targetObj.exists:
  90. info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " 已找到目标Object,长点击目标Object。", info)
  91. targetObj.long_click()
  92. return True
  93. else:
  94. info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " 目标Object当前界面未找到!!!", info)
  95. return False
  96. # elif chooseType.lower() == "no":
  97. # info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " 目标Object的chooseType为no,默认为不需要选中。", info)
  98. # retValue = self.checkOptionExist(option,parent)
  99. # if retValue is True:
  100. # return True
  101. # else:
  102. # error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option在当前界面不存在", ERROR)
  103. # return False
  104. else:
  105. error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option focus-select属性配置异常", ERROR)
  106. return False
  107. elif viewText.__len__() > 0\
  108. or focuseViewId.__len__() > 2\
  109. or focuseView[UATTree.Focus_Desc].__len__() > 0:
  110. info(self.cls, "focusTargetOption",
  111. "going toDestFocusByText_for_FocusView, Option:%s" % option[UATTree.TAB_NAME], INFO)
  112. return self.fm.toDestFocusByText_for_FocusView(text = viewText,
  113. FocusViewResourceId = focuseViewId,
  114. findDirection = findDirection,
  115. Max_Try = Max_Try,
  116. keyType = keyType,
  117. hb_keyDict=hb_keyDict)
  118. else:
  119. error(self.cls, "focusTargetOption", "Warning:Option %s读取的参数未能找到合适的执行方式,执行失败,请注意检查UATree文件!!!"%option[UATTree.TAB_NAME], ERROR)
  120. return False
  121. '''
  122. 执行uatree中的各种直接按键,不携带其他参数。限enter_key和toParent_Key。
  123. am和input等带参数的指令,无法处理。
  124. mkey:parent里面的key字典
  125. '''
  126. def executeKey(self, mkey):
  127. eventList = mkey[UATTree.Key_Event]
  128. irList = mkey[UATTree.Key_IR]
  129. print "executeKey,eventList:",eventList
  130. print "executeKey,irList:",irList
  131. times = 1
  132. wait = 0.5
  133. keyList = []
  134. if eventList.__len__() <= 0:
  135. if irList.__len__() <= 0:
  136. info(self.cls, "executeKey", "传入的keyDict中,event和ir的key list均为空!!!默认executeKey执行成功!!!", INFO)
  137. return True
  138. # 读取其中是否存在wait或者times属性
  139. for irKey in irList:
  140. if str(irKey).startswith("times="):
  141. times = int(irKey.lstrip("times="))
  142. if str(irKey).startswith("wait="):
  143. wait = float(irKey.lstrip("wait="))
  144. else:
  145. keyList.append(irKey)
  146. #执行keyList
  147. info(self.cls, "executeKey", "executing irKey, wait=%s, times=%s"%(wait, times), INFO)
  148. for i in range(0, times):
  149. for irKey in keyList:
  150. print "executeKey, sendKey:%s"%irKey
  151. self.tvOperator.sendKey(irKey, duration=wait)
  152. else:
  153. # 读取其中是否存在wait或者times属性
  154. for eventKey in eventList:
  155. if str(eventKey).startswith("times="):
  156. times = int(eventKey.lstrip("times="))
  157. if str(eventKey).startswith("wait="):
  158. wait = float(eventKey.lstrip("wait="))
  159. else:
  160. keyList.append(eventKey)
  161. #执行keyList
  162. info(self.cls, "executeKey", "executing eventKey, wait=%s, times=%s"%(wait, times), INFO)
  163. for i in range(0, times):
  164. for eventKey in keyList:
  165. print "executeKey, pressKeyTimes:%s"%eventKey
  166. if self.eventKeyCode.has_key(eventKey.upper()):
  167. keyCode = self.eventKeyCode[eventKey.upper()]
  168. info(self.cls, "executeKey", "eventKeyCode has key %s, code is %s" % (eventKey, keyCode), INFO)
  169. self.pyU.pressKeyTimes(keyCode)
  170. else:
  171. self.pyU.pressKeyTimes(eventKey)
  172. time.sleep(wait)
  173. return True
  174. '''
  175. 用于执行option的EnterKey
  176. :param mkeys:option的EnterKey字典
  177. :nextParent: option要进入的下一个parent字典,如果parent=None,则进入和option同名的parent。
  178. '''
  179. def executeEnterKey(self, mkeys, nextParent=None):
  180. enterKey = None
  181. if nextParent is None:
  182. # 不指定nextParent,默认用第一个
  183. try:
  184. enterKey = mkeys[0][UATTree.Key_Param]
  185. except Exception,e:
  186. enterKey = None
  187. error(self.cls, "executeEnterKey", "This option has no enterKey",ERROR)
  188. return True
  189. else:
  190. # 指定nextParent,根据parentName查找匹配的enterKey
  191. enterKey = self.uatPathManage.uatData.UATree.getEKByParent(mkeys, nextParent[UATTree.TAB_NAME])
  192. if enterKey is None:
  193. error(self.cls,"executeEnterKey","Next parent %s has not enterKey"%nextParent[UATTree.TAB_NAME],ERROR)
  194. return True
  195. for keyname in enterKey[UATTree.Key_Value]:
  196. ret = self.executeKeyByType(keyname,
  197. enterKey[UATTree.Key_Type],
  198. times=enterKey[UATTree.Key_Times],
  199. duration=enterKey[UATTree.Duration_Time])
  200. time.sleep(enterKey[UATTree.Wait_Time])
  201. if ret is False:
  202. return False
  203. return True
  204. '''
  205. 执行uatree中的moveKey
  206. :param mkey move_key的字典
  207. flag 决定执行的方向,传入值为0和1
  208. '''
  209. def executeMoveKey(self, mkey, flag):
  210. eventList = mkey[UATTree.Key_Event]
  211. irList = mkey[UATTree.Key_IR]
  212. print "executeMoveKey,eventList:",eventList
  213. print "executeMoveKey,irList:",irList
  214. times = 1
  215. wait = 0.5
  216. if eventList.__len__() <= 0:
  217. if irList.__len__() <= 0:
  218. return False
  219. # 读取其中是否存在wait或者times属性
  220. for item in irList:
  221. if str(item).startswith("times="):
  222. times = int(item.lstrip("times="))
  223. irList.remove(item)
  224. if str(item).startswith("wait="):
  225. wait = float(item.lstrip("wait="))
  226. irList.remove(item)
  227. #执行move_key
  228. info(self.cls, "executeMoveKey", "executing irMoveKey:%s, wait=%s, times=%s, flag=%s"%(irList[flag],wait, times, flag), INFO)
  229. return self.tvOperator.sendKey(irList[flag], duration=wait)
  230. else:
  231. # 读取其中是否存在wait或者times属性
  232. for item in eventList:
  233. if str(item).startswith("times="):
  234. times = int(item.lstrip("times="))
  235. eventList.remove(item)
  236. if str(item).startswith("wait="):
  237. wait = float(item.lstrip("wait="))
  238. eventList.remove(item)
  239. #执行move_key
  240. info(self.cls, "executeMoveKey", "executing eventMoveKey:%s, wait=%s, times=%s, flag=%s"%(eventList[flag], wait, times, flag), INFO)
  241. self.pyU.pressKeyTimes(eventList[flag])
  242. time.sleep(wait)
  243. # pressKeyTimes不返回Bool结果
  244. return True
  245. '''
  246. 根据keyType执行key值
  247. '''
  248. def executeKeyByType(self, key, keyType, times = 1, duration = 1.0):
  249. info(self.cls, "executeKeyByType", "executeKeyByType key %s, keyType %s, " % (key, keyType), INFO)
  250. if keyType == UATTree.Key_Event:
  251. if self.eventKeyCode.has_key(key.upper()):
  252. keyCode = self.eventKeyCode[key.upper()]
  253. info(self.cls, "executeKeyByType", "eventKeyCode has key %s, code is %s" % (key, keyCode), INFO)
  254. self.pyU.pressKeyTimes(keyCode, times, duration)
  255. else:
  256. self.pyU.pressKeyTimes(key, times, duration)
  257. return True
  258. elif keyType == UATTree.Key_IR:
  259. self.tvOperator.sendKey(key, times, duration)
  260. return True
  261. else:
  262. return False
  263. '''
  264. 执行firstParent的shortCut_key
  265. '''
  266. def executeShortCutKey(self, parent):
  267. amList = str(parent[UATTree.TAB_SHORTCUT_KEY][UATTree.Key_AM]).split(",")
  268. waitTime = 0
  269. print "amList:", amList
  270. if amList[-1].startswith("wait="):
  271. waitTime = float(amList[-1].lstrip("wait="))
  272. print "executing ShortCutKey,amWaitTime:",waitTime
  273. if amList[0].lower() == "activity":
  274. print "executeShortCutKey,amList:",amList,parent[UATTree.TAB_UI_VIEW]
  275. activityParam = parent[UATTree.TAB_UI_VIEW][UATTree.UIView_Activity]
  276. # 通过 / 号分割pkgName和activity,部分项目不存在activity。
  277. pkgList = string_util.strToList(activityParam,"/")
  278. pkgName = pkgList[0]
  279. if pkgList.__len__() == 1:
  280. self.pyU.startApp(pkgName)
  281. elif pkgList.__len__() == 2:
  282. activity = pkgList[1]
  283. self.pyU.startApp(pkgName, activity)
  284. else:
  285. debug(self.cls, "executeShortCutKey", "UATree中 %s activity参数异常!!请检查!!"%parent[UATTree.TAB_NAME], DEBUG)
  286. time.sleep(waitTime)
  287. return True
  288. else:
  289. return self.executeKey(parent[UATTree.TAB_SHORTCUT_KEY])
  290. '''
  291. 用于处理parent可能出现的弹窗,isForword用于判断是处理进入弹窗还是退出弹窗
  292. '''
  293. def executeDialog(self, parent, isForward=True):
  294. print "parent %s executeDialog parent[UATTree.TAB_UI_VIEW]:%s"%(parent[UATTree.TAB_NAME], parent[UATTree.TAB_UI_VIEW])
  295. if isForward is True:
  296. dialog_A = parent[UATTree.TAB_UI_VIEW][UATTree.UIView_Dialog_F]
  297. type = "forward"
  298. else:
  299. dialog_A = parent[UATTree.TAB_UI_VIEW][UATTree.UIView_Dialog_B]
  300. type = "back"
  301. if dialog_A.__len__ > 0:
  302. info(self.cls, "executeDialog", "parent %s has %s dialog:%s"%(parent[UATTree.TAB_NAME], type, dialog_A), INFO)
  303. for dialogName in dialog_A:
  304. dialog = dialog_A[dialogName]
  305. ret = self.focusCmd.checkDialogExist(dialog)
  306. if ret < 1:
  307. info(self.cls, "executeDialog", "parent %s dialog %s doesn't popup."%(parent[UATTree.TAB_NAME], dialogName), INFO)
  308. continue
  309. tarOption = dialog["choose"]
  310. if not self.focusTargetOption(dialog, tarOption):
  311. error(self.cls, "executeDialog", "dialog %s focus choose option %s fail"%(dialogName, tarOption), ERROR)
  312. return 0
  313. print "executing enter_key"
  314. self.executeEnterKey(tarOption[UATTree.TAB_ENTER_KEY])
  315. time.sleep(1)
  316. # 考虑到弹窗优先级与遍历顺序可能不一致的问题,重新再进行一次弹窗遍历
  317. return self.executeDialog(parent, isForward)
  318. else:
  319. info(self.cls, "executeDialog", "parent %s has no %s dialog"%(parent[UATTree.TAB_NAME], type), INFO)
  320. return 1
  321. '''
  322. 根据excel表格的enter_key中读取的按键设置checkbox的动作
  323. '''
  324. def setCheckboxAction(self,option,textValue):
  325. info(self.cls, "setCheckboxAction", "textValue:%s" % textValue, INFO)
  326. if textValue != "":
  327. enterKey = option[UATTree.TAB_ENTER_KEY]
  328. return self.executeEnterKey(enterKey)
  329. else:
  330. error(self.cls, "setCheckboxAction", " Option %s textValue is empty!" % option[UATTree.TAB_NAME], ERROR)
  331. return False
  332. def setCheckbox(self, option, value, currentValue, textValue, resourceId):
  333. if currentValue == value:
  334. info(self.cls, "setCheckbox", " is %s, no need to change!" % value, INFO)
  335. else:
  336. self.setCheckboxAction(option,textValue)
  337. objInfo = self.pyU.getFocusedUIObject().child(resourceId=resourceId).info # checkbox的info
  338. ifChecked = objInfo['checked']
  339. if ifChecked == value:
  340. info(self.cls, "setCheckbox", " to %s success!" % value, INFO)
  341. return True
  342. else:
  343. info(self.cls, "setCheckbox", " to %s fail!" % value, INFO)
  344. return False
  345. '''
  346. 执行设值动作,区分数字设值以及非数字设值
  347. '''
  348. def setValue(self, option, value):
  349. textValue = option[UATTree.TAB_TEXT_VALUE]
  350. sign = textValue['sign']
  351. info(self.cls, "setValue", "textValue:%s"%textValue, INFO)
  352. if textValue[UATTree.ValueView_Value] != "":
  353. #EnterKey作为textValue配置中的值设置时的按键,enterKey只能配置一个
  354. enterKey = option[UATTree.TAB_ENTER_KEY][0][UATTree.Key_Param]
  355. if enterKey[UATTree.Key_Value].__len__() > 0:
  356. keyList = enterKey[UATTree.Key_Value]
  357. keyType = enterKey[UATTree.Key_Type]
  358. else:
  359. info(self.cls, "setValue", "textValue在有值的情况下,enterKey读取失败,无法进行setValue的动作。", INFO)
  360. return False
  361. if textValue.has_key(UATTree.ValueView_Min) and textValue.has_key(UATTree.ValueView_Max):
  362. valueViewResId = textValue[UATTree.ValueView_ID]
  363. count = 0
  364. # 默认步长为1
  365. stepSize = 1.0
  366. if textValue[UATTree.ValueView_StepSize] != "":
  367. stepSize = float(textValue[UATTree.ValueView_StepSize])
  368. # 默认设值间隔为0.2s
  369. duration = 0.2
  370. if textValue[UATTree.ValueView_Duration] != "":
  371. duration = float(textValue[UATTree.ValueView_Duration])
  372. info(self.cls, "setValue", "stepSize:%s, duration:%s"%(stepSize, duration), INFO)
  373. while(count < 3):
  374. # 获取数值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
  375. # 先在聚焦组件内找数值
  376. retCode, choosedObj = self.focusCmd.getChooseUIObj(option)
  377. # print "choosedObj.info:", choosedObj.info
  378. text = self.getValueInObjectByTextResourceId(choosedObj, valueViewResId)
  379. if text is None:
  380. # 组件之中找不到时,则直接用resourceId在整个页面找
  381. textObj = self.pyU.getUiObject(resourceId=valueViewResId)
  382. if textObj is None or textObj.info is None:
  383. error(self.cls, "setValue", "查不到组件,请检查%s 的textValue resid是否正确" % option[UATTree.TAB_NAME], ERROR)
  384. return False
  385. text = textObj.info['text']
  386. if text is None:
  387. error(self.cls, "setValue", "获取数值失败,请检查%s 的textValue resid是否正确"%option[UATTree.TAB_NAME], ERROR)
  388. return False
  389. if text[0] not in sign:
  390. dList = getDigitFromString(text)
  391. if dList.__len__() > 0:
  392. text_num = float(dList[-1])
  393. else:
  394. text_num = None
  395. else:
  396. if text[0] == sign[0]:
  397. text_num = -float(text.strip(text[0]))
  398. elif text[0] == sign[1]:
  399. text_num = float(text.strip(text[0]))
  400. else:
  401. text_num = float(text)
  402. info(self.cls, "setValue", "当前设值为%s"%text, INFO)
  403. value = str(value)
  404. if len(sign)!=0:
  405. if value[0]==sign[0]:
  406. value_num = -float(value.strip(value[0]))
  407. elif value[0]==sign[1]:
  408. value_num = float(value.strip(value[0]))
  409. else:
  410. value_num = float(value)
  411. else:
  412. value_num = float(value)
  413. if text_num is None:
  414. return False
  415. if value_num < text_num:
  416. self.executeKeyByType(keyList[0], keyType, int((text_num - value_num)/stepSize), duration)
  417. elif value_num > text_num:
  418. self.executeKeyByType(keyList[1], keyType, int((value_num - text_num)/stepSize), duration)
  419. else:
  420. return True
  421. count += 1
  422. else:
  423. info(self.cls, "setValue", "%s次设值仍未能设置为%s,设值失败"%(count+1, value), INFO)
  424. return False
  425. elif textValue[UATTree.ValueView_Value].lower() == "input":
  426. info(self.cls, "setValue", "设值类型为input类型,传入的值为%s"%value, INFO)
  427. # 设值类型必须要有设值textBox Object
  428. resid = textValue[UATTree.ValueView_ID]
  429. text = textValue[UATTree.ValueView_Text]
  430. desc = textValue[UATTree.ValueView_Desc]
  431. textBoxObj = self.pyU.getUiObject(resourceId=resid, text=text, description=desc)
  432. if not textBoxObj.exists:
  433. debug(self.cls, "setValue", "未能获取到设值的TextBox Object!!!请检查相关配置是否能获取到TextBox!!!", DEBUG)
  434. return False
  435. setValueSuccess = textBoxObj.set_text(value)
  436. if not setValueSuccess:
  437. debug(self.cls, "setValue", "TextBox使用set_text()接口设值失败,值为%s"%value, DEBUG)
  438. return False
  439. else:
  440. print "executing enter_key"
  441. return self.executeEnterKey(option[UATTree.TAB_ENTER_KEY])
  442. else:
  443. info(self.cls, "setValue", "读取到value参数配置为非数值value,开始进行非数值型value设值。", INFO)
  444. try:
  445. valueView = textValue[value]
  446. valueViewText = valueView["text"]
  447. valueViewResId = valueView["resid"]
  448. except Exception,e:
  449. info(self.cls, "setValue", "value %s的配置信息读取失败!!!设值失败!!!请确保该value的text属性和resid属性均已按格式配好!!!"%value, INFO)
  450. return False
  451. Max_Try = textValue[UATTree.ValueView_Value].__len__()
  452. count = 0
  453. reverse_count = 0
  454. while (True):
  455. # 获取当前值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
  456. # 先在聚焦组件内找当前值
  457. retCode, choosedObj = self.focusCmd.getChooseUIObj(option)
  458. print "choosedObj.info:", choosedObj.info
  459. for i in range(choosedObj.info["childCount"]):
  460. childUIObject = choosedObj.child_by_instance(i, resourceId=valueViewResId)
  461. if childUIObject.exists:
  462. text = childUIObject.info['text']
  463. break
  464. else:
  465. # 组件之中找不到时,则直接用resourceId在整个页面找
  466. text = self.getValueByTextResourceId(valueViewResId)
  467. if text is None:
  468. error(self.cls, "setValue", "获取数值失败,请检查%s 的textValue resid是否正确" % option[UATTree.TAB_NAME],
  469. ERROR)
  470. return False
  471. info(self.cls, "setValue", "当前设值为%s" % text, INFO)
  472. if text == valueViewText:
  473. info(self.cls, "setValue",
  474. "option %s设值为 %s 成功!!!"%(option[UATTree.TAB_NAME], value), INFO)
  475. return True
  476. if count < Max_Try:
  477. self.executeKeyByType(keyList[0], keyType)
  478. count += 1
  479. elif reverse_count < Max_Try:
  480. self.executeKeyByType(keyList[1], keyType)
  481. reverse_count += 1
  482. else:
  483. info(self.cls, "setValue",
  484. "option %s设值已经尝试最大次数%s次,未能切换至目标value %s"%(option[UATTree.TAB_NAME], Max_Try, value) % value, INFO)
  485. return False
  486. else:
  487. optionName = value
  488. option = self.uatPathManage.uatData.getOption(optionName)
  489. parent = self.uatPathManage.uatData.getParentByOption(option)
  490. return self.valueCmd.setParentValue(parent, option)
  491. '''
  492. 根据传入路径进行退出菜单的动作。
  493. 顺着传入的parent path,逐个确认当前页面是否为该parent。如果不是该parent,则往path后面继续遍历;如果是该parent,则执行该层parent的toParentKey
  494. '''
  495. def exitMenuByPath(self, path):
  496. info(self.cls, "exitMenuByPath", "", INFO)
  497. for i in range(0, path.__len__()):
  498. parent = path[i]
  499. ret = self.checkParentChoose(parent)
  500. info(self.cls, "exitMenuByPath", "check parent:%s,ret=%d"%(parent[UATTree.TAB_NAME],ret), INFO)
  501. if ret < 1:
  502. continue
  503. toParentKey = parent[UATTree.TAB_TOPARENT_KEY]
  504. info(self.cls, "exitMenuByPath", "now is in parent:%s"%parent[UATTree.TAB_NAME], INFO)
  505. if toParentKey[UATTree.Key_Event] == [] and toParentKey[UATTree.Key_IR] == []:
  506. info(self.cls, "exitMenuByPath", "parent:%s has no toparent_key, using default back" % parent[UATTree.TAB_NAME], INFO)
  507. self.executeKeyByType("back", UATTree.Key_Event)
  508. time.sleep(1)
  509. else:
  510. self.executeKey(toParentKey)
  511. time.sleep(1)
  512. ret = self.executeDialog(parent, False)
  513. if ret < 1:
  514. return False
  515. return True
  516. def getUIObjPic(self, obj):
  517. picName = time.time()
  518. picPath = os.path.join(getSATTmpDIR(), "%s.png"%picName)
  519. if obj == None:
  520. return False, picPath, ""
  521. self.CC.takePicture(picPath)
  522. try:
  523. bounds = obj.info["bounds"]
  524. area = [bounds["left"], bounds["top"], bounds["right"], bounds["bottom"]]
  525. cutPicPath = os.path.join(getSATTmpDIR(), "%s_cutPic_%s.png"%(picName, area))
  526. self.imgCMP.saveCropPic(picPath, cutPicPath, area)
  527. except Exception,e:
  528. return False, picPath, ""
  529. return True, picPath, cutPicPath
  530. def infoViewCmpText(self, cmpText, picPath):
  531. ocrParamList = [("ENG_CHN", 10000),
  532. ("ENG_CHN", 10001)]
  533. for ocrParam in ocrParamList:
  534. ocr_str = self.ocrConvert.getStr(picPath, ocrParam[0], ocrParam[1])
  535. info(self.cls, "infoViewCmpText", "cmpText:%s ocr_str:%s ocrParam:%s"%(cmpText, ocr_str, ocrParam),INFO)
  536. if strcmp(ocr_str, cmpText):
  537. info(self.cls, "infoViewCmpText", "OCR文字识别对比成功",INFO)
  538. return 1
  539. else:
  540. error(self.cls, "infoViewCmpText", "Waring:OCR文字识别对比失败!!",ERROR)
  541. return 0
  542. def infoViewCmpPic(self, cmpPic, picPath):
  543. cmpImg = cv.imread(cmpPic)
  544. cutImg = cv.imread(picPath)
  545. result = self.imgCMP.cmpImgTotal(cmpImg, cutImg)
  546. if result is True:
  547. info(self.cls, "infoViewCmpText", "图片对比成功", INFO)
  548. return 1
  549. else:
  550. error(self.cls, "infoViewCmpText", "Waring:图片对比失败!!",ERROR)
  551. return 0
  552. def getValueByTextResourceId(self, textResourceId):
  553. try:
  554. textObj = self.pyU.getUiObject(resourceId=textResourceId)
  555. text = textObj.info['text']
  556. digitList = getDigitFromString(text)
  557. return digitList[-1]
  558. except Exception, e:
  559. return None
  560. def getValueInObjectByTextResourceId(self, object, textResourceId):
  561. try:
  562. for i in range(object.info["childCount"]):
  563. childUIObject = object.child_by_instance(i, resourceId=textResourceId)
  564. if childUIObject.exists:
  565. text = childUIObject.info['text']
  566. digitList = getDigitFromString(text)
  567. return digitList[-1]
  568. else:
  569. return None
  570. except Exception, e:
  571. error(self.cls, "getValueInObjectByTextResourceId", "getValueInObjectByByTextResourceId:Object loading error!!!!!!error msg:%s"%e, ERROR)
  572. return None