UAT_runnerCommand.py 35 KB

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