123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810 |
- # -*- coding:utf-8 -*-
- from UAT_tree import UATTree
- from ssat_sdk.python_uiautomator import PyUIAutomator,FocusManageAndroid, DirectionManageAndroid
- from UAT_log import error,debug,info
- from ssat_sdk.utils import string_util
- from ssat_sdk.tv_operator import TvOperator
- from ssat_sdk.device_manage.capturecard_manager import CCardManager
- from ssat_sdk.sat_environment import getSATTmpDIR
- from ssat_sdk.pic_tool import ImageCMP
- from ssat_sdk.ocr_convert import OCRConvert
- from ssat_sdk.utils.string_util import strcmp, getDigitFromString
- import cv2 as cv
- import os, sys, time
- DEBUG = True
- INFO =True
- ERROR = True
- class UATRunnerCommand():
- cls = "UATRunnerCommand"
- def __init__(self, uatPathManage, pyU, dm, fm):
- self.uatPathManage = uatPathManage
- self.pyU = pyU
- self.dm = dm
- self.fm = fm
- self.tvOperator = TvOperator()
- self.eventKeyCode = self.uatPathManage.uatData.getKeyCodeDict()
- self.CC = CCardManager()
- self.imgCMP = ImageCMP()
- self.ocrConvert = OCRConvert()
- #检测option组件是不是在电视上显示了
- def checkOptionExist(self, option, parent):
- #print "checkOptionExist,option:",option
- moveKey = parent[UATTree.TAB_MOVE_KEY]
- print "moveKey:", moveKey
- Max_Try = moveKey[UATTree.Max_Try]
- if Max_Try == "":
- Max_Try = 1
- else:
- Max_Try = int(Max_Try)
- Reverse_Max_Try = Max_Try * 2
- print "Max_Try:",Max_Try
- count = 0
- Reversecount = 0
- lastObj = ""
- while(True):
- if count >= Max_Try and Reversecount >= Reverse_Max_Try:
- break
- # 如果找到目标直接返回
- if self.pyU.isElementExist(resourceId=option["optionView"][UATTree.UIView_ID],
- text=option["optionView"][UATTree.UIView_Text],
- description=option["optionView"][UATTree.UIView_Desc]):
- return True
- # choosingObj = self.getChooseUIObj(option)
- # if lastObj != "":
- # if choosingObj.info == lastObj.info:
- # print "choosingObj.info == lastObj.info!!!!"
- # if count < Max_Try:
- # Max_Try = count
- # else:
- # break
- # lastObj = choosingObj
- if count < Max_Try:
- flag = 1
- count += 1
- print "now count:", count
- else:
- flag = 0
- Reversecount += 1
- print "now Reversecount:", Reversecount
- self.executeMoveKey(moveKey, flag)
- return False
- '''
- 检测option组件,在电视上是否被选中了
- :param option 数据字典
- :return -1:未进入option的页面,找不到option UIObject;0:进入了option的页面,未选中option;1 已经选中option
- -2:代表未找到焦点
- '''
- def checkOptionChoose(self, option):
- optionUIObj = self.pyU.getUiObject(text=option["optionView"][UATTree.View_Text],
- resourceId=option["optionView"][UATTree.View_ID],
- description=option["optionView"][UATTree.View_Desc])
- if optionUIObj is None or optionUIObj.count == 0:
- return -1
- curUIObj = self.getChooseUIObj(option)
- if curUIObj is None or curUIObj.count == 0:
- return -2
- if self.dm.isHasAnotherBounds(curUIObj.info["bounds"], optionUIObj.info['bounds']):
- return 1
- else:
- return 0
- '''
- 根据option配置的焦点方式,返回当前页面焦点组件
- '''
- def getChooseUIObj(self, option):
- print "getChooseUIObj option:",option["focus-select"]
- if option["focus-select"]["type"].__len__() > 1:
- chooseType = option["focus-select"][UATTree.FS_Type]
- if chooseType.lower() == "focus":
- return self.pyU.getFocusedUIObject()
- elif chooseType.lower() == "select":
- return self.pyU.getSelectedUIObject()
- elif chooseType.lower() == "no":
- resId = option[UATTree.TAB_OPTION_VIEW][UATTree.View_ID]
- text = option[UATTree.TAB_OPTION_VIEW][UATTree.View_Text]
- className = option[UATTree.TAB_OPTION_VIEW][UATTree.View_Class]
- desc = option[UATTree.TAB_OPTION_VIEW][UATTree.View_Desc]
- return self.pyU.getUiObject(resourceId=resId, text=text, className=className, description=desc)
- else:
- error(self.cls, "getChooseUIObj", option["name"]+" option focus-select属性配置异常", ERROR)
- return None
- elif option["focuseView"][UATTree.Focus_Text].__len__() > 0\
- or option["focuseView"][UATTree.Focus_ID].__len__() > 1\
- or option["focuseView"][UATTree.Focus_Desc].__len__() > 0:
- return self.pyU.getUiObject(text=option["focuseView"][UATTree.Focus_Text],
- resourceId=option["focuseView"][UATTree.Focus_ID],
- description=option["focuseView"][UATTree.Focus_Desc])
- else:
- error(self.cls, "getChooseUIObj", option["name"] + " option 需要配置 focus-select或者FocusView", ERROR)
- return None
- '''
- 检测parent,在电视上是否被选中了。一个option被选中,表示选中
- :param option 数据字典
- :return
- -3:代表UIView判断未通过;
- -2:代表选中了parent,但未找到焦点;
- -1:未进入parent的页面,找不到parent layout;
- 0:进入了parent的页面,未选中parent;
- 1:已经选中parent
- '''
- def checkParentChoose(self, parent):
- debug(self.cls, "checkParentChoose", "parent:" + parent["name"], DEBUG)
- chooseTypeDict = {}
- layoutResId = parent["layout"][UATTree.Layout_ID]
- uiView = parent[UATTree.TAB_UI_VIEW]
- uiViewResId = uiView[UATTree.View_ID]
- uiViewText = uiView[UATTree.View_Text]
- uiViewDesc = uiView[UATTree.View_Desc]
- if layoutResId == "" and uiViewResId == "" and uiViewText == "" and uiViewDesc == "":
- debug(self.cls, "checkParentChoose", "Warning:Parent %s的Layout resId和UIView信息获取失败!!请注意检查UATree文件!!!"%parent['name'], DEBUG)
- return -1
- elif layoutResId != "":
- # 如果存在UIView信息,则先判断UIView
- if uiViewResId != "" or uiViewText != "" or uiViewDesc != "":
- isExist = self.checkUIViewExist(uiView)
- if isExist is False:
- info(self.cls, "checkParentChoose", "当前页面不存在Parent:%s的UIView组件,判断该界面非此parent"%parent['name'], INFO)
- return -3
- else:
- info(self.cls, "checkParentChoose", "已识别出Parent:%s的UIView组件"%parent['name'], INFO)
- description = parent["layout"][UATTree.Layout_Desc]
- if description != "":
- layoutUIObj = self.pyU.getUiObject(resourceId=parent["layout"][UATTree.Layout_ID],
- description=parent["layout"][UATTree.Layout_Desc])
- else:
- layoutUIObj = self.pyU.getUiObject(resourceId=parent["layout"][UATTree.Layout_ID])
- if layoutUIObj is None or layoutUIObj.count == 0:
- # debug(self.cls, "checkParentChoose", "parent isn't " + parent["name"], DEBUG)
- return -1
- # print "checkParentChoose, layoutUIObj:",layoutUIObj.info
- debug(self.cls, "checkParentChoose", "layoutUIObj:"+str(layoutUIObj.info), DEBUG)
- for optionName in parent["option"]:
- option = parent["option"][optionName]
- if option["focus-select"]["type"].__len__() > 1:
- chooseTypeDict[option["focus-select"][UATTree.FS_Type]] = option
- elif option["focuseView"][UATTree.Focus_Text].__len__() > 0\
- or option["focuseView"][UATTree.Focus_ID].__len__() > 1\
- or option["focuseView"][UATTree.Focus_Desc].__len__() > 0:
- chooseTypeDict[option["focuseView"][UATTree.Focus_ID]
- + option["focuseView"][UATTree.Focus_Text]
- + option["focuseView"][UATTree.Focus_Desc]] = option
- info(self.cls, "checkParentChoose", str(chooseTypeDict), INFO)
- for key in chooseTypeDict.keys():
- option = chooseTypeDict[key]
- chooseUIObj = self.getChooseUIObj(option)
- #TODO 如果选中效果,是没有type[no],例如快捷键,如何处理?
- if chooseUIObj is None or chooseUIObj.count == 0:
- return -2
- debug(self.cls, "checkParentChoose", "chooseUIObj:" + str(chooseUIObj.info), DEBUG)
- # 获取到chooseUIObj后,与layout对比坐标,确认焦点是否存在于layout之中
- layoutBounds = layoutUIObj.info['bounds']
- choosingBounds = chooseUIObj.info['bounds']
- if not self.dm.isHasAnotherBounds(layoutBounds, choosingBounds):
- info(self.cls, "checkParentChoose",
- "识别出parent %s的Layout,但焦点不在该Layout之中,判断界面未处于该parent" % (parent['name']), INFO)
- return -2
- # 如果该页面text属性不为空,则确认该parent下所有option的text,是否存在于当前页面中;如果text属性为空,则判断index属性
- for optionName in parent["option"]:
- option = parent["option"][optionName]
- # 如果有text属性则确认Text,是否能对比成功
- optionText = option[UATTree.TAB_OPTION_VIEW][UATTree.View_Text]
- optionId = option[UATTree.TAB_OPTION_VIEW][UATTree.View_ID]
- optionIndex = option[UATTree.TAB_OPTION_VIEW][UATTree.View_Index]
- if optionText.__len__() >0:
- info(self.cls, "checkParentChoose", "checking parent %s text %s"%(parent['name'], optionText), INFO)
- textObj = self.pyU.getUiObject(text=optionText)
- elif optionId.__len__() >0:
- info(self.cls, "checkParentChoose", "checking parent %s optionId %s" % (parent['name'], optionId),
- INFO)
- textObj = self.pyU.getUiObject(resourceId=optionId)
- elif optionIndex != "":
- # index属性无法用于判断option,如果在以index属性作为判断依据的页面,则直接返回在这个页面
- info(self.cls, "checkParentChoose",
- "已找到目标parent %s,该页面为Index item页面,无法判断具体option"%parent['name'], INFO)
- return 1
- else:
- error(self.cls, "checkParentChoose", "当前参数不足以判断option %s是否存在"%(option['name']), ERROR)
- continue
- if textObj.exists:
- info(self.cls, "checkParentChoose",
- "已找到目标parent %s,并识别出该parent下的option %s" % (parent['name'], option['name']), INFO)
- return 1
- else:
- info(self.cls, "checkParentChoose",
- "return 0" , INFO)
- return 0
- else:
- isExist = self.checkUIViewExist(uiView)
- if not isExist:
- return -3
- else:
- info(self.cls, "checkParentChoose",
- "识别出parent %s的UIView参数,判断处于该界面中" % (parent['name']), INFO)
- return 1
- def checkUIViewExist(self, uiView):
- resid = uiView[UATTree.View_ID]
- text = uiView[UATTree.View_Text]
- description = uiView[UATTree.View_Desc]
- uiViewObj = self.pyU.getUiObject(resourceId=resid,
- text=text,
- description=description)
- if not uiViewObj.exists:
- return False
- else:
- return True
- '''
- 检测某个弹窗是否存在。
- 弹窗相关的参数放在UIView中配置。
- 当存在resId参数时,使用resId获取对象并获取文本,再与text做比较;
- 当不存在resId参数时,使用text直接获取对象,判断对象是否存在。
- 与checkParentChoose判断不一致,做特殊处理。
- '''
- def checkDialogExist(self, dialog):
- debug(self.cls, "checkDialogExist", "dialog:" + dialog["name"], DEBUG)
- dialogText = dialog[UATTree.TAB_UI_VIEW][UATTree.UIView_Text]
- dialogResId = dialog[UATTree.TAB_UI_VIEW][UATTree.UIView_ID]
- # print "checkDialogExist.dialogText:", dialogText
- # print "checkDialogExist.dialogResId:", dialogResId
- if dialogResId != "":
- textObj = self.pyU.getUiObject(resourceId=dialogResId)
- if textObj.exists:
- objText = textObj.info['text']
- # print "checkDialogExist.objText:", objText
- if dialogText in objText:
- return 1
- else:
- return 0
- else:
- return 0
- else:
- textObj = self.pyU.getUiObject(text=dialogText)
- if textObj.exists:
- return 1
- else:
- return 0
- def focusTargetOption(self, parent, option):
- if parent[UATTree.TAB_MOVE_KEY][UATTree.Max_Try] != "":
- Max_Try = int(parent[UATTree.TAB_MOVE_KEY][UATTree.Max_Try])
- else:
- Max_Try = parent[UATTree.TAB_OPTION].__len__()
- moveKey = parent[UATTree.TAB_MOVE_KEY]
- print "moveKey:", moveKey
- findDirection = ""
- keyType = UATTree.Key_Event
- if moveKey[UATTree.Key_Event].__len__() > 1:
- findDirection = moveKey[UATTree.Key_Event][0]
- keyType = UATTree.Key_Event
- elif moveKey[UATTree.Key_IR].__len__() > 1:
- findDirection = moveKey[UATTree.Key_IR][0]
- keyType = UATTree.Key_IR
- elif moveKey[UATTree.Key_Input].__len__() > 1:
- inputCmd = moveKey[UATTree.Key_Input]
- elif moveKey[UATTree.Key_AM].__len__() > 1:
- amCmd = moveKey[UATTree.Key_AM]
- else:
- error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option 读取 move_key失败", ERROR)
- return False
- chooseType = option[UATTree.TAB_FOCUS_SELECT][UATTree.FS_Type]
- optionView = option[UATTree.TAB_OPTION_VIEW]
- viewId = optionView[UATTree.View_ID]
- viewText = optionView[UATTree.View_Text]
- viewIndex = optionView[UATTree.View_Index]
- viewClass = optionView[UATTree.View_Class]
- viewDesc = optionView[UATTree.View_Desc]
- recyclerViewId = parent[UATTree.TAB_LAYOUT][UATTree.Layout_ID]
- focuseView = option[UATTree.TAB_FOCUSE_VIEW]
- focuseViewId = focuseView[UATTree.View_ID]
- others = parent[UATTree.TAB_OTHERS]
- hb_keyDict = others[UATTree.Key_HeartBeat]
- print "%s chooseType: %s"%(option["name"], chooseType)
- if chooseType.__len__() > 1:
- if chooseType.lower() == "focus":
- if viewText != "" and viewId != "":
- info(self.cls, "focusTargetOption", "going toDestFocusByText_with_FocuseResourceId, Option:%s"%option[UATTree.TAB_NAME], INFO)
- info(self.cls, "focusTargetOption", "viewText=%s, focuseResId=%s"%(viewText, focuseViewId), INFO)
- return self.fm.toDestFocusByText_with_FocuseResourceId(text=viewText,
- focuseResId=viewId,
- Max_Try=Max_Try,
- keyType=keyType,
- hb_keyDict=hb_keyDict)
- elif viewText != "":
- info(self.cls, "focusTargetOption", "going toDestFocusByText_for_RecyclerView, Option:%s"%option[UATTree.TAB_NAME], INFO)
- return self.fm.toDestFocusByText_for_RecyclerView(text = viewText,
- findDirection = findDirection,
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- elif viewId != "":
- info(self.cls, "focusTargetOption", "going toDestTargetByResourceId_for_RecyclerView, Option:%s"%option[UATTree.TAB_NAME], INFO)
- return self.fm.toDestTargetByResourceId_for_RecyclerView(resourceId = viewId,
- chooseType = chooseType.lower(),
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- elif viewDesc != "":
- info(self.cls, "focusTargetOption", "going toDestFocusByDescription_for_RecyclerView, Option:%s"%option[UATTree.TAB_NAME], INFO)
- return self.fm.toDestFocusByDescription_for_RecyclerView(description=viewDesc,
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- elif viewIndex != "":
- info(self.cls, "focusTargetOption", "going focusItemByIndexFromRecyclerView, Option:%s"%option[UATTree.TAB_NAME], INFO)
- return self.fm.focusItemByIndexFromRecyclerView(recyclerView_resId = recyclerViewId,
- child_class = viewClass,
- target_index = viewIndex,
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- else:
- error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option focus-select属性配置异常", ERROR)
- return False
- elif chooseType.lower() == "select":
- if viewText != "":
- info(self.cls, "focusTargetOption", "going toDestSelectByText_for_RecyclerView, Option:%s"%option[UATTree.TAB_NAME], INFO)
- return self.fm.toDestSelectByText_for_RecyclerView(text = viewText,
- optionViewResoucreId = viewId,
- listViewResourceId = recyclerViewId,
- findDirection = findDirection,
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- elif viewId != "":
- info(self.cls, "focusTargetOption", "going toDestTargetByResourceId_for_RecyclerView, Option:%s"%option[UATTree.TAB_NAME], INFO)
- return self.fm.toDestTargetByResourceId_for_RecyclerView(resourceId = viewId,
- chooseType = chooseType.lower(),
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- elif chooseType.lower() == "long_click":
- info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " going to long click type!!!", info)
- targetObj = self.pyU.getUiObject(resourceId=viewId)
- if targetObj.exists:
- info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " 已找到目标Object,长点击目标Object。", info)
- targetObj.long_click()
- return True
- else:
- info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " 目标Object当前界面未找到!!!", info)
- return False
- elif chooseType.lower() == "no":
- info(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " 目标Object的chooseType为no,默认为不需要选中。", info)
- return True
- else:
- error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option focus-select属性配置异常", ERROR)
- return False
- elif viewText.__len__() > 0\
- or focuseViewId.__len__() > 2\
- or focuseView[UATTree.Focus_Desc].__len__() > 0:
- info(self.cls, "focusTargetOption",
- "going toDestFocusByText_for_FocusView, Option:%s" % option[UATTree.TAB_NAME], INFO)
- return self.fm.toDestFocusByText_for_FocusView(text = viewText,
- FocusViewResourceId = focuseViewId,
- findDirection = findDirection,
- Max_Try = Max_Try,
- keyType = keyType,
- hb_keyDict=hb_keyDict)
- else:
- error(self.cls, "focusTargetOption", "Warning:Option %s读取的参数未能找到合适的执行方式,执行失败,请注意检查UATree文件!!!"%option[UATTree.TAB_NAME], ERROR)
- return False
- '''
- 执行uatree中的各种直接按键,不携带其他参数。限enter_key和toParent_Key。
- am和input等带参数的指令,无法处理。
- mkey:parent里面的key字典
- '''
- def executeKey(self, mkey):
- eventList = mkey[UATTree.Key_Event]
- irList = mkey[UATTree.Key_IR]
- print "executeKey,eventList:",eventList
- print "executeKey,irList:",irList
- times = 1
- wait = 0.5
- keyList = []
- if eventList.__len__() <= 0:
- if irList.__len__() <= 0:
- info(self.cls, "executeKey", "传入的keyDict中,event和ir的key list均为空!!!默认executeKey执行成功!!!", INFO)
- return True
- # 读取其中是否存在wait或者times属性
- for irKey in irList:
- if str(irKey).startswith("times="):
- times = int(irKey.lstrip("times="))
- if str(irKey).startswith("wait="):
- wait = float(irKey.lstrip("wait="))
- else:
- keyList.append(irKey)
- #执行keyList
- info(self.cls, "executeKey", "executing irKey, wait=%s, times=%s"%(wait, times), INFO)
- for i in range(0, times):
- for irKey in keyList:
- print "executeKey, sendKey:%s"%irKey
- self.tvOperator.sendKey(irKey, duration=wait)
- else:
- # 读取其中是否存在wait或者times属性
- for eventKey in eventList:
- if str(eventKey).startswith("times="):
- times = int(eventKey.lstrip("times="))
- if str(eventKey).startswith("wait="):
- wait = float(eventKey.lstrip("wait="))
- else:
- keyList.append(eventKey)
- #执行keyList
- info(self.cls, "executeKey", "executing eventKey, wait=%s, times=%s"%(wait, times), INFO)
- for i in range(0, times):
- for eventKey in keyList:
- print "executeKey, pressKeyTimes:%s"%eventKey
- if self.eventKeyCode.has_key(eventKey.upper()):
- keyCode = self.eventKeyCode[eventKey.upper()]
- info(self.cls, "executeKey", "eventKeyCode has key %s, code is %s" % (eventKey, keyCode), INFO)
- self.pyU.pressKeyTimes(keyCode)
- else:
- self.pyU.pressKeyTimes(eventKey)
- time.sleep(wait)
- return True
- '''
- 执行uatree中的moveKey
- :param mkey move_key的字典
- flag 决定执行的方向,传入值为0和1
- '''
- def executeMoveKey(self, mkey, flag):
- eventList = mkey[UATTree.Key_Event]
- irList = mkey[UATTree.Key_IR]
- print "executeMoveKey,eventList:",eventList
- print "executeMoveKey,irList:",irList
- times = 1
- wait = 0.5
- if eventList.__len__() <= 0:
- if irList.__len__() <= 0:
- return False
- # 读取其中是否存在wait或者times属性
- for item in irList:
- if str(item).startswith("times="):
- times = int(item.lstrip("times="))
- irList.remove(item)
- if str(item).startswith("wait="):
- wait = float(item.lstrip("wait="))
- irList.remove(item)
- #执行move_key
- info(self.cls, "executeMoveKey", "executing irMoveKey:%s, wait=%s, times=%s, flag=%s"%(irList[flag],wait, times, flag), INFO)
- return self.tvOperator.sendKey(irList[flag], duration=wait)
- else:
- # 读取其中是否存在wait或者times属性
- for item in eventList:
- if str(item).startswith("times="):
- times = int(item.lstrip("times="))
- eventList.remove(item)
- if str(item).startswith("wait="):
- wait = float(item.lstrip("wait="))
- eventList.remove(item)
- #执行move_key
- info(self.cls, "executeMoveKey", "executing eventMoveKey:%s, wait=%s, times=%s, flag=%s"%(eventList[flag], wait, times, flag), INFO)
- self.pyU.pressKeyTimes(eventList[flag])
- time.sleep(wait)
- # pressKeyTimes不返回Bool结果
- return True
- '''
- 根据keyType执行key值
- '''
- def executeKeyByType(self, key, keyType, times = 1, duration = 1.0):
- if keyType == UATTree.Key_Event:
- if self.eventKeyCode.has_key(key.upper()):
- keyCode = self.eventKeyCode[key.upper()]
- info(self.cls, "executeKeyByType", "eventKeyCode has key %s, code is %s" % (key, keyCode), INFO)
- self.pyU.pressKeyTimes(keyCode, times, duration)
- else:
- self.pyU.pressKeyTimes(key, times, duration)
- return True
- elif keyType == UATTree.Key_IR:
- self.tvOperator.sendKey(key, times, duration)
- return True
- else:
- return False
- '''
- 执行firstParent的shortCut_key
- '''
- def executeShortCutKey(self, parent):
- amList = str(parent[UATTree.TAB_SHORTCUT_KEY][UATTree.Key_AM]).split(",")
- waitTime = 0
- print "amList:", amList
- if amList[-1].startswith("wait="):
- waitTime = float(amList[-1].lstrip("wait="))
- print "executing ShortCutKey,amWaitTime:",waitTime
- if amList[0].lower() == "activity":
- print "executeShortCutKey,amList:",amList,parent[UATTree.TAB_UI_VIEW]
- activityParam = parent[UATTree.TAB_UI_VIEW][UATTree.UIView_Activity]
- # 通过 / 号分割pkgName和activity,部分项目不存在activity。
- pkgList = string_util.strToList(activityParam,"/")
- pkgName = pkgList[0]
- if pkgList.__len__() == 1:
- self.pyU.startApp(pkgName)
- elif pkgList.__len__() == 2:
- activity = pkgList[1]
- self.pyU.startApp(pkgName, activity)
- else:
- debug(self.cls, "executeShortCutKey", "UATree中 %s activity参数异常!!请检查!!"%parent[UATTree.TAB_NAME], DEBUG)
- time.sleep(waitTime)
- return True
- else:
- return self.executeKey(parent[UATTree.TAB_SHORTCUT_KEY])
- '''
- 用于处理parent可能出现的弹窗,isForword用于判断是处理进入弹窗还是退出弹窗
- '''
- def executeDialog(self, parent, isForward=True):
- print "parent %s executeDialog parent[UATTree.TAB_UI_VIEW]:%s"%(parent[UATTree.TAB_NAME], parent[UATTree.TAB_UI_VIEW])
- if isForward is True:
- dialog_A = parent[UATTree.TAB_UI_VIEW][UATTree.UIView_Dialog_F]
- type = "forward"
- else:
- dialog_A = parent[UATTree.TAB_UI_VIEW][UATTree.UIView_Dialog_B]
- type = "back"
- if dialog_A.__len__ > 0:
- info(self.cls, "executeDialog", "parent %s has %s dialog:%s"%(parent[UATTree.TAB_NAME], type, dialog_A), INFO)
- for dialogName in dialog_A:
- dialog = dialog_A[dialogName]
- ret = self.checkDialogExist(dialog)
- if ret < 1:
- info(self.cls, "executeDialog", "parent %s dialog %s doesnt popup."%(parent[UATTree.TAB_NAME], dialogName), INFO)
- continue
- tarOption = dialog["choose"]
- if not self.focusTargetOption(dialog, tarOption):
- error(self.cls, "executeDialog", "dialog %s focus choose option %s fail"%(dialogName, tarOption), ERROR)
- return 0
- print "executing enter_key"
- self.executeKey(tarOption[UATTree.TAB_ENTER_KEY])
- time.sleep(1)
- # 考虑到弹窗优先级与遍历顺序可能不一致的问题,重新再进行一次弹窗遍历
- return self.executeDialog(parent, isForward)
- else:
- info(self.cls, "executeDialog", "parent %s has no %s dialog"%(parent[UATTree.TAB_NAME], type), INFO)
- return 1
- '''
- 执行设值动作,区分数字设值以及非数字设值
- '''
- def setValue(self, option, value):
- textValue = option[UATTree.TAB_TEXT_VALUE]
- info(self.cls, "setValue", "textValue:%s"%textValue, INFO)
- if textValue[UATTree.ValueView_Value] != "":
- enterKey = option[UATTree.TAB_ENTER_KEY]
- if enterKey[UATTree.Key_Event] != "":
- keyList = enterKey[UATTree.Key_Event]
- keyType = UATTree.Key_Event
- elif enterKey[UATTree.Key_IR] != "":
- keyList = enterKey[UATTree.Key_IR]
- keyType = UATTree.Key_IR
- else:
- info(self.cls, "setValue", "textValue在有值的情况下,enterKey读取失败,无法进行setValue的动作。", INFO)
- return False
- if textValue.has_key(UATTree.ValueView_Min) and textValue.has_key(UATTree.ValueView_Max):
- valueViewResId = textValue[UATTree.ValueView_ID]
- count = 0
- # 默认步长为1
- stepSize = 1.0
- if textValue[UATTree.ValueView_StepSize] != "":
- stepSize = float(textValue[UATTree.ValueView_StepSize])
- # 默认设值间隔为0.2s
- duration = 0.2
- if textValue[UATTree.ValueView_Duration] != "":
- duration = float(textValue[UATTree.ValueView_Duration])
- info(self.cls, "setValue", "stepSize:%s, duration:%s"%(stepSize, duration), INFO)
- while(count < 3):
- # 获取数值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
- # 先在聚焦组件内找数值
- choosedObj = self.getChooseUIObj(option)
- print "choosedObj.info:", choosedObj.info
- text = self.getValueInObjectByTextResourceId(choosedObj, valueViewResId)
- if text is None:
- # 组件之中找不到时,则直接用resourceId在整个页面找
- text = self.getValueByTextResourceId(valueViewResId)
- if text is None:
- error(self.cls, "setValue", "获取数值失败,请检查%s 的textValue resid是否正确"%option[UATTree.TAB_NAME], ERROR)
- return False
- info(self.cls, "setValue", "当前设值为%s"%text, INFO)
- text_num = float(text)
- value_num = float(value)
- if text_num is None:
- return False
- if value_num < text_num:
- self.executeKeyByType(keyList[0], keyType, int((text_num - value_num)/stepSize), duration)
- elif value_num > text_num:
- self.executeKeyByType(keyList[1], keyType, int((value_num - text_num)/stepSize), duration)
- else:
- return True
- count += 1
- else:
- info(self.cls, "setValue", "%s次设值仍未能设置为%s,设值失败"%(count+1, value), INFO)
- return False
- elif textValue[UATTree.ValueView_Value].lower() == "input":
- info(self.cls, "setValue", "设值类型为input类型,传入的值为%s"%value, INFO)
- # 设值类型必须要有设值textBox Object
- resid = textValue[UATTree.ValueView_ID]
- text = textValue[UATTree.ValueView_Text]
- desc = textValue[UATTree.ValueView_Desc]
- textBoxObj = self.pyU.getUiObject(resourceId=resid, text=text, description=desc)
- if not textBoxObj.exists:
- debug(self.cls, "setValue", "未能获取到设值的TextBox Object!!!请检查相关配置是否能获取到TextBox!!!", DEBUG)
- return False
- setValueSuccess = textBoxObj.set_text(value)
- if not setValueSuccess:
- debug(self.cls, "setValue", "TextBox使用set_text()接口设值失败,值为%s"%value, DEBUG)
- return False
- else:
- print "executing enter_key"
- return self.executeKey(option[UATTree.TAB_ENTER_KEY])
- else:
- info(self.cls, "setValue", "读取到value参数配置为非数值value,开始进行非数值型value设值。", INFO)
- try:
- valueView = textValue[value]
- valueViewText = valueView["text"]
- valueViewResId = valueView["resid"]
- except Exception,e:
- info(self.cls, "setValue", "value %s的配置信息读取失败!!!设值失败!!!请确保该value的text属性和resid属性均已按格式配好!!!"%value, INFO)
- return False
- Max_Try = textValue[UATTree.ValueView_Value].__len__()
- count = 0
- reverse_count = 0
- while (True):
- # 获取当前值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
- # 先在聚焦组件内找当前值
- choosedObj = self.getChooseUIObj(option)
- print "choosedObj.info:", choosedObj.info
- for i in range(choosedObj.info["childCount"]):
- childUIObject = choosedObj.child_by_instance(i, resourceId=valueViewResId)
- if childUIObject.exists:
- text = childUIObject.info['text']
- break
- else:
- # 组件之中找不到时,则直接用resourceId在整个页面找
- text = self.getValueByTextResourceId(valueViewResId)
- if text is None:
- error(self.cls, "setValue", "获取数值失败,请检查%s 的textValue resid是否正确" % option[UATTree.TAB_NAME],
- ERROR)
- return False
- info(self.cls, "setValue", "当前设值为%s" % text, INFO)
- if text == valueViewText:
- info(self.cls, "setValue",
- "option %s设值为 %s 成功!!!"%(option[UATTree.TAB_NAME], value), INFO)
- return True
- if count < Max_Try:
- self.executeKeyByType(keyList[0], keyType)
- count += 1
- elif reverse_count < Max_Try:
- self.executeKeyByType(keyList[1], keyType)
- reverse_count += 1
- else:
- info(self.cls, "setValue",
- "option %s设值已经尝试最大次数%s次,未能切换至目标value %s"%(option[UATTree.TAB_NAME], Max_Try, value) % value, INFO)
- return False
- else:
- optionName = value
- option = self.uatPathManage.uatData.getOption(optionName)
- parent = self.uatPathManage.uatData.getParentByOption(option)
- ret = self.focusTargetOption(parent, option)
- if not ret:
- info(self.cls, "setValue", "未能聚焦至目标value %s,设值失败"%value, INFO)
- return False
- info(self.cls, "setValue", "已聚焦至目标value %s"%value, INFO)
- print "executing enter_key"
- self.executeKey(option[UATTree.TAB_ENTER_KEY])
- return True
- '''
- 根据传入路径进行退出菜单的动作。
- 顺着传入的parent path,逐个确认当前页面是否为该parent。如果不是该parent,则往path后面继续遍历;如果是该parent,则执行该层parent的toParentKey
- '''
- def exitMenuByPath(self, path):
- # print "exitMenuByPath path:", path
- for i in range(0, path.__len__()):
- parent = path[i]
- info(self.cls, "exitMenuByPath", "check parent:%s"%parent[UATTree.TAB_NAME], INFO)
- ret = self.checkParentChoose(parent)
- if ret < 1:
- continue
- toParentKey = parent[UATTree.TAB_TOPARENT_KEY]
- info(self.cls, "exitMenuByPath", "now is in parent:%s"%parent[UATTree.TAB_NAME], INFO)
- if toParentKey[UATTree.Key_Event] == [] and toParentKey[UATTree.Key_IR] == []:
- info(self.cls, "exitMenuByPath", "parent:%s has no toparent_key, using default back" % parent[UATTree.TAB_NAME], INFO)
- self.executeKeyByType("back", UATTree.Key_Event)
- else:
- self.executeKey(toParentKey)
- time.sleep(1)
- ret = self.executeDialog(parent, False)
- if ret < 1:
- return False
- return True
- def getUIObjPic(self, obj):
- picName = time.time()
- picPath = os.path.join(getSATTmpDIR(), "%s.png"%picName)
- if obj == None:
- return False, picPath, ""
- self.CC.takePicture(picPath)
- try:
- bounds = obj.info["bounds"]
- area = [bounds["left"], bounds["top"], bounds["right"], bounds["bottom"]]
- cutPicPath = os.path.join(getSATTmpDIR(), "%s_cutPic_%s.png"%(picName, area))
- self.imgCMP.saveCropPic(picPath, cutPicPath, area)
- except Exception,e:
- return False, picPath, ""
- return True, picPath, cutPicPath
- def infoViewCmpText(self, cmpText, picPath):
- ocrParamList = [("ENG_CHN", 10000),
- ("ENG_CHN", 10001)]
- for ocrParam in ocrParamList:
- ocr_str = self.ocrConvert.getStr(picPath, ocrParam[0], ocrParam[1])
- info(self.cls, "infoViewCmpText", "cmpText:%s ocr_str:%s ocrParam:%s"%(cmpText, ocr_str, ocrParam),INFO)
- if strcmp(ocr_str, cmpText):
- info(self.cls, "infoViewCmpText", "OCR文字识别对比成功",INFO)
- return 1
- else:
- error(self.cls, "infoViewCmpText", "Waring:OCR文字识别对比失败!!",ERROR)
- return 0
- def infoViewCmpPic(self, cmpPic, picPath):
- cmpImg = cv.imread(cmpPic)
- cutImg = cv.imread(picPath)
- result = self.imgCMP.cmpImgTotal(cmpImg, cutImg)
- if result is True:
- info(self.cls, "infoViewCmpText", "图片对比成功", INFO)
- return 1
- else:
- error(self.cls, "infoViewCmpText", "Waring:图片对比失败!!",ERROR)
- return 0
- def getValueByTextResourceId(self, textResourceId):
- try:
- textObj = self.pyU.getUiObject(resourceId=textResourceId)
- text = textObj.info['text']
- digitList = getDigitFromString(text)
- return digitList[-1]
- except Exception, e:
- return None
- def getValueInObjectByTextResourceId(self, object, textResourceId):
- try:
- for i in range(object.info["childCount"]):
- childUIObject = object.child_by_instance(i, resourceId=textResourceId)
- if childUIObject.exists:
- text = childUIObject.info['text']
- digitList = getDigitFromString(text)
- return digitList[-1]
- else:
- return None
- except Exception, e:
- error(self.cls, "getValueInObjectByTextResourceId", "getValueInObjectByByTextResourceId:Object loading error!!!!!!error msg:%s"%e, ERROR)
- return None
|