UAT_runnerCommand.py 30 KB

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