123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687 |
- # -*- 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
- from UAT_valueCommand import ValueCommand
- from UAT_focusCommand import FocusCommand
- from UIParamUtil import UIParamUtil
- 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()
- self.valueCmd = ValueCommand( self)
- self.focusCmd = FocusCommand(self)
- #检测option组件是不是在电视上显示了
- def checkOptionExist(self, option, parent):
- return self.focusCmd.checkOptionExist(option,parent)
- '''
- 检测parent,在电视上是否被选中了。一个option被选中,表示选中
- :param option 数据字典
- :return
- -3:代表UIView判断未通过;
- -2:代表选中了parent,但未找到焦点;
- -1:未进入parent的页面,找不到parent layout;
- 0:进入了parent的页面,未选中parent;
- 1:已经选中parent
- '''
- def checkParentChoose(self, parent):
- return self.focusCmd.checkParentChoose(parent)
- 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.View_ID]
- focuseView = option[UATTree.TAB_FOCUSE_VIEW]
- focuseViewId = focuseView[UATTree.View_ID]
- focusViewClass = focuseView[UATTree.View_Class]
- focuseViewDesc = focuseView[UATTree.View_Desc]
- 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" or chooseType.lower() == "select" or chooseType.lower() == "no":
- return self.focusCmd.focusOptionView(parent,option,chooseType)
- 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)
- # retValue = self.checkOptionExist(option,parent)
- # if retValue is True:
- # return True
- # else:
- # error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option在当前界面不存在", ERROR)
- # return False
- else:
- error(self.cls, "focusTargetOption", option[UATTree.TAB_NAME] + " option focus-select属性配置异常", ERROR)
- return False
- elif viewText.__len__() > 0\
- or focuseViewId.__len__() > 2\
- or focuseViewDesc.__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,
- FocusViewClass = focusViewClass,
- FocusViewDesc = focuseViewDesc,
- 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
- '''
- 用于执行option的EnterKey
- :param mkeys:option的EnterKey字典
- :nextParent: option要进入的下一个parent字典,如果parent=None,则进入和option同名的parent。
- '''
- def executeEnterKey(self, mkeys, nextParent=None):
- if nextParent is None:
- # 不指定nextParent,默认用第一个
- try:
- enterKey = mkeys[0][UATTree.Key_Param]
- except Exception,e:
- error(self.cls, "executeEnterKey", "This option has no enterKey",ERROR)
- return True
- else:
- # 指定nextParent,根据parentName查找匹配的enterKey
- enterKey = self.uatPathManage.uatData.UATree.getEKByParent(mkeys, nextParent[UATTree.TAB_NAME])
- if enterKey is None:
- error(self.cls,"executeEnterKey","Next parent %s has not enterKey"%nextParent[UATTree.TAB_NAME],ERROR)
- return True
- for keyname in enterKey[UATTree.Key_Value]:
- ret = self.executeKeyByType(keyname,
- enterKey[UATTree.Key_Type],
- times=enterKey[UATTree.Key_Times],
- duration=enterKey[UATTree.Duration_Time])
- time.sleep(enterKey[UATTree.Wait_Time])
- if ret is False:
- return False
- 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):
- info(self.cls, "executeKeyByType", "executeKeyByType key %s, keyType %s, " % (key, keyType), INFO)
- 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.focusCmd.checkDialogExist(dialog)
- if ret < 1:
- info(self.cls, "executeDialog", "parent %s dialog %s doesn't 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.executeEnterKey(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
- '''
- 根据excel表格的enter_key中读取的按键设置checkbox的动作
- '''
- def setCheckboxAction(self,option,textValue):
- info(self.cls, "setCheckboxAction", "textValue:%s" % textValue, INFO)
- if textValue != "":
- enterKey = option[UATTree.TAB_ENTER_KEY]
- return self.executeEnterKey(enterKey)
- else:
- error(self.cls, "setCheckboxAction", " Option %s textValue is empty!" % option[UATTree.TAB_NAME], ERROR)
- return False
- def setCheckbox(self, option, value, currentValue, textValue, resourceId):
- if currentValue == value:
- info(self.cls, "setCheckbox", " is %s, no need to change!" % value, INFO)
- else:
- self.setCheckboxAction(option,textValue)
- objInfo = self.pyU.getFocusedUIObject().child(resourceId=resourceId).info # checkbox的info
- ifChecked = objInfo['checked']
- if ifChecked == value:
- info(self.cls, "setCheckbox", " to %s success!" % value, INFO)
- return True
- else:
- info(self.cls, "setCheckbox", " to %s fail!" % value, INFO)
- return False
- '''
- 执行设值动作,区分数字设值以及非数字设值
- '''
- def setValue(self, option, value):
- textValue = option[UATTree.TAB_TEXT_VALUE]
- sign = textValue['sign']
- info(self.cls, "setValue", "textValue:%s"%textValue, INFO)
- if textValue[UATTree.ValueView_Value] != "":
- #EnterKey作为textValue配置中的值设置时的按键,enterKey只能配置一个
- enterKey = option[UATTree.TAB_ENTER_KEY][0][UATTree.Key_Param]
- if enterKey[UATTree.Key_Value].__len__() > 0:
- keyList = enterKey[UATTree.Key_Value]
- keyType = enterKey[UATTree.Key_Type]
- 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.View_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):
- # 获取数值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
- # 先在聚焦组件内找数值
- retCode, choosedObj = self.focusCmd.getChooseUIObj(option)
- # print "choosedObj.info:", choosedObj.info
- text = self.getValueInObjectByTextResourceId(choosedObj, valueViewResId)
- if text is None:
- # 组件之中找不到时,则直接用resourceId在整个页面找
- textObj = self.pyU.getUiObject(resourceId=valueViewResId)
- if textObj is None or textObj.info is None:
- error(self.cls, "setValue", "查不到组件,请检查%s 的textValue resid是否正确" % option[UATTree.TAB_NAME], ERROR)
- return False
- text = textObj.info['text']
- if text is None:
- error(self.cls, "setValue", "获取数值失败,请检查%s 的textValue resid是否正确"%option[UATTree.TAB_NAME], ERROR)
- return False
- if text[0] not in sign:
- dList = getDigitFromString(text)
- if dList.__len__() > 0:
- text_num = float(dList[-1])
- else:
- text_num = None
- else:
- if text[0] == sign[0]:
- text_num = -float(text.strip(text[0]))
- elif text[0] == sign[1]:
- text_num = float(text.strip(text[0]))
- else:
- text_num = float(text)
- info(self.cls, "setValue", "当前设值为%s"%text, INFO)
- value = str(value)
- if len(sign)!=0:
- if value[0]==sign[0]:
- value_num = -float(value.strip(value[0]))
- elif value[0]==sign[1]:
- value_num = float(value.strip(value[0]))
- else:
- value_num = float(value)
- else:
- 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.View_ID]
- text = textValue[UATTree.View_Text]
- desc = textValue[UATTree.View_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.executeEnterKey(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):
- # 获取当前值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
- # 先在聚焦组件内找当前值
- retCode, choosedObj = self.focusCmd.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)
- return self.valueCmd.setParentValue(parent, option)
- '''
- 执行读取值的动作,区分数字设值以及非数字设值,非数值设值中,如果选项为radioButton机制,则使用radioButton组件来定位。
- '''
- def getValue(self, option):
- textValue = option[UATTree.TAB_TEXT_VALUE]
- sign = textValue['sign']
- info(self.cls, "getValue", "textValue:%s"%textValue, INFO)
- if textValue[UATTree.ValueView_Value] != "":
- result = True
- if textValue.has_key(UATTree.ValueView_Min) and textValue.has_key(UATTree.ValueView_Max):
- valueViewResId = textValue[UATTree.View_ID]
- # 获取数值时存在两种情况,一种是数值文本在聚焦组件里,一种在聚焦组件之外,分别处理。
- # 先在聚焦组件内找数值
- retCode, choosedObj = self.focusCmd.getChooseUIObj(option)
- # print "choosedObj.info:", choosedObj.info
- text = self.getValueInObjectByTextResourceId(choosedObj, valueViewResId)
- if text is None:
- # 组件之中找不到时,则直接用resourceId在整个页面找
- textObj = self.pyU.getUiObject(resourceId=valueViewResId)
- if textObj is None or textObj.info is None:
- error(self.cls, "getValue", "查不到组件,请检查%s 的textValue resid是否正确" % option[UATTree.TAB_NAME], ERROR)
- result = False
- text = textObj.info['text']
- curValue = text
- if curValue is None:
- error(self.cls, "getValue", "获取数值失败,请检查%s 的textValue resid是否正确"%option[UATTree.TAB_NAME], ERROR)
- result = False
- info(self.cls, "getValue", "当前value值为%s" % curValue, INFO)
- return result, option[UATTree.TAB_NAME], curValue
- else:
- parentName = option[UATTree.TAB_NAME]
- parent = self.uatPathManage.uatData.getParentDict(parentName)
- fsDict = parent['option'][parent['option'].keys()[0]][UATTree.TAB_FOCUS_SELECT]
- print "fsDict:", fsDict
- if fsDict[UATTree.FS_checkType] == "RadioButton":
- fsParam = {}
- if fsDict[UATTree.FS_Type].lower() == "focus":
- fsParam["focused"] = "true"
- elif fsDict[UATTree.FS_Type].lower() == "select":
- fsParam["selected"] = "true"
- if fsDict[UATTree.View_ID] != "":
- fsParam["resourceId"] = fsDict[UATTree.View_ID]
- if fsDict[UATTree.View_Class] != "":
- fsParam["className"] = fsDict[UATTree.View_Class]
- if fsDict[UATTree.View_Desc] != "":
- fsParam["description"] = fsDict[UATTree.View_Desc]
- if parent[UATTree.TAB_MOVE_KEY][UATTree.Key_Event].__len__() > 0:
- moveKey = parent[UATTree.TAB_MOVE_KEY][UATTree.Key_Event]
- keyType = UATTree.Key_Event
- else:
- moveKey = parent[UATTree.TAB_MOVE_KEY][UATTree.Key_IR]
- keyType = UATTree.Key_IR
- maxTry = len(parent['option'])
- ret, choosedObj = self.focusCmd.moveToCheckedRedioButton(fsParam, findDirection=moveKey[1], keyType=keyType, maxTry=maxTry)
- # 聚焦至单选框失败,则取值失败
- if not ret:
- return False, None, ""
- # 获取聚焦框的文本内容
- textObj = choosedObj.child(className="android.widget.TextView")
- text = textObj.info['text']
- for optionP in parent['option']:
- if text == parent['option'][optionP][UATTree.TAB_OPTION_VIEW][UATTree.View_Text]:
- return True, optionP, text
- # 遍历完所有option仍未能对比到目标的文本,则取值失败
- else:
- return False, None, text
- else:
- layoutParam = parent[UATTree.TAB_LAYOUT]
- layoutUIParam = UIParamUtil.setObjParam(layoutParam)
- retCode, option = self.focusCmd.getChooseOptionP(parent, layoutUIParam, needFocus=True)
- if option is None:
- return False, None, ""
- else:
- return True, option[UATTree.TAB_NAME], ""
- '''
- 根据传入路径进行退出菜单的动作。
- 顺着传入的parent path,逐个确认当前页面是否为该parent。如果不是该parent,则往path后面继续遍历;如果是该parent,则执行该层parent的toParentKey
- '''
- def exitMenuByPath(self, path):
- info(self.cls, "exitMenuByPath", "", INFO)
- for i in range(0, path.__len__()):
- parent = path[i]
- ret = self.checkParentChoose(parent)
- info(self.cls, "exitMenuByPath", "check parent:%s,ret=%d"%(parent[UATTree.TAB_NAME],ret), INFO)
- 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)
- time.sleep(1)
- 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
|