# _*_ coding:utf-8 _*_

__author__ = 'bobby'
__date__ = '2018/10/9 15:55'
import os
import uiautomator2 as u2
import time
from AndroidUITest.util.gettimestr import GetTimeStr  # 导入获取时间串函数
from AndroidUITest.util.my_log import MyLog
from PIL import Image   #导入Image
from PIL import ImageEnhance  #导入ImageEnhance


class BaseFrame(object):
    def __init__(self, outdevice=None, apppacakagename=None, isusb=None):
        print(outdevice)
        try:
            if outdevice is None:
                # self.d = u2.connect('192.168.199.168')
                # self.d = u2.connect_usb('192.168.1.100:5555')
                self.d = u2.connect()  # 没有输入设备号则默认不要设备号
            else:
                self.d = u2.connect_usb(outdevice)

        except Exception as e:
            print(e)
            self.install_uiaumator2_yilai()  # 先安装

            # 然后再运行
            if outdevice is None:
                # self.d = u2.connect('192.168.199.168')
                # self.d = u2.connect_usb('192.168.1.100:5555')
                self.d = u2.connect()  # 没有输入设备号则默认不要设备号
            else:
                self.d = u2.connect_usb(outdevice)

        # if apppacakagename == None:
        #     self.packagename = "com.ahdi.qrindo.wallet"
        # else:
        #     self.packagename =apppacakagename
        self.timeStr = GetTimeStr()  # 实例化
        self.isstartapp = False

    # 打印日志
    def outPutMyLog(self, context):
        mylog = MyLog(context)
        mylog.runMyLog()

    # 打印错误日志
    def outPutErrorMyLog(self, context):
        mylog = MyLog(context)
        mylog.runErrorLog()

    # 执行shell命令

    def adbshell(self, order):
        d = self.d
        d.shell(order)
        # self.outPutMyLog('输入shell命令:',order,'。\n')

    # python -m weditor
    def start_weditor(self):
        # order = "python -m uiautomator2 init"
        order = "python -m weditor"
        self.adbshell(order=order)

    # 执行cmd命令
    def exec_order(self, orderName):
        check = os.popen(orderName)
        c = check.read()
        print("执行命令：%s" % orderName)
        print("执行命令后返回的内容：%s" % c)
        check.close()
        return c

    # 安装uiautomator2依赖脚本
    def install_uiaumator2_yilai(self):
        # 获取当前文件路径
        current_path = os.path.dirname(os.path.abspath(__file__))
        print(current_path)

        # 安装app-uiautomator.apk
        app_uiautomator_apk_path = str(
            current_path) + "\\" + "uiautomator2yilaibao" + "\\" + "app-uiautomator.apk"
        print(app_uiautomator_apk_path)
        install_app_uiautomator_apk_order = "adb install %s" % str(
            app_uiautomator_apk_path)
        self.exec_order(orderName=install_app_uiautomator_apk_order)
        self.delaytime(5)

        # 安装app-uiautomator-test.apk
        app_uiautomator_test_apk_path = str(
            current_path) + "\\" + "uiautomator2yilaibao" + "\\" + "app-uiautomator-test.apk"
        install_app_uiautomator_test_apk_order = "adb install %s" % str(
            app_uiautomator_test_apk_path)
        self.exec_order(orderName=install_app_uiautomator_test_apk_order)

        self.delaytime(5)

        # 将atx-agent推送到手机的/data/local/tmp/目录中
        atx_agent_path = str(current_path) + "\\" + \
            "uiautomator2yilaibao" + "\\" + "atx-agent"
        push_atx_agent_order = "adb push %s /data/local/tmp" % str(
            atx_agent_path)
        self.exec_order(orderName=push_atx_agent_order)
        self.delaytime(5)

        # 设置手机端atx-agent权限为755，支持所有用户组可读可执行
        set_atx_agent_order = "adb shell chmod 755 /data/local/tmp/atx-agent"
        self.exec_order(orderName=set_atx_agent_order)
        self.delaytime(5)

        # 启动atx-agent并切换后台运行，默认**端口是7912
        start_atx_agent_order = "adb shell /data/local/tmp/atx-agent server -d"
        self.exec_order(orderName=start_atx_agent_order)
        self.delaytime(5)

    # 启动app

    def startapp(self, packagename, activityname=None):
        self.stopapp(packagename=packagename)  # 先关闭应用
        self.d.app_start(packagename, activityname)
        self.outPutMyLog('启动包名为[%s]的应用---------' % packagename)
        # self.outPutMyLog('设备信息：', d.device_info)
        self.delaytime(3)

    # 关闭app
    def stopapp(self, packagename):
        self.d.app_stop(packagename)
        self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename)

    # 延时
    def delaytime(self, dalaytime):
        dalaytime = int(dalaytime)
        time.sleep(dalaytime)
        self.outPutMyLog('等待%d秒...' % dalaytime)

    # 点击返回按钮
    def clickback(self):
        self.d.press("back")
        self.outPutMyLog('点击返回按键')
        self.delaytime(3)

    # 点击Home按钮
    def clickhome(self):
        self.d.press("home")
        self.outPutMyLog('点击Home按键')
        self.delaytime(3)

    # 元素是否存在
    def ele_is_exist(self, findstyle, styleparame):
        try:
            if findstyle == "resourceId":
                ele = self.d(resourceId=styleparame)
                ele.info
                # self.outPutMyLog("ele.info:%s" % ele.info)
                # self.outPutMyLog("找到元素")
            elif findstyle == "text":
                ele = self.d(text=styleparame)
                ele.info
                # self.outPutMyLog("ele.info:%s"% ele.info)
                # self.outPutMyLog("找到元素")
        except Exception as e:
            # self.outPutMyLog("报错：%s"% e)
            return False
        else:
            return True

    # 查找元素
    def findelement(self, findstyle, styleparame):
        try:
            if findstyle == "resourceId":
                ele = self.d(resourceId=styleparame)
            elif findstyle == "text":
                ele = self.d(text=styleparame)
            elif findstyle == "xpath":
                ele = self.d.xpath(styleparame)

            elerect = self.geteleinfo_rect_screen(ele)  #调用元素属性，如果没有成功，则说明元素不存在


        except Exception as e:
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog("出错原因：定位控件失败.具体原因：%s" % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)

    # 查找到元素并且输入内容

    def findelement_and_input(self, findstyle, styleparame, inputtext):
        ele = self.findelement(findstyle, styleparame)
        self.ele_input(ele, inputtext)

    # 查找到元素并且点击该元素
    def findelement_and_click(
            self,
            findstyle,
            styleparame,
            outpretoastmessage=None):
        ele = self.findelement(findstyle, styleparame)
        toastmessage = self.ele_click_and_return_toastmessage(
            ele, outpretoastmessage)
        return toastmessage

    # 查找到元素并且返回enabled属性的状态
    def findelement_and_return_enabledstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    # 查找到元素并且text内容
    def findelement_and_return_text(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    # 查找到元素并且返回selected属性状态
    def findelement_and_return_selectedstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    # 通过ID查找到元素
    def findbyresourceId(self, resourceId):
        d = self.d
        try:
            ele = d(resourceId=resourceId)
            eletext = self.geteleinfo_text(ele)
            if eletext == "":
                self.outPutMyLog("定位到resourceId为[%s]的控件。" % resourceId)
            else:
                self.outPutMyLog("定位到text为[%s]的控件。" % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog("出错原因：定位控件失败.具体原因：%s" % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)

    # 通过ID查找到元素并且输入内容
    def findbyresourceId_and_input(self, resourceId, inputtext):
        ele = self.findbyresourceId(resourceId)
        self.ele_input(ele, inputtext)

    # 通过ID查找到元素并且点击该元素
    def findbyresourceId_and_click(self, resourceId, outpretoastmessage=None):
        ele = self.findbyresourceId(resourceId)
        toastmessage = self.ele_click_and_return_toastmessage(
            ele, outpretoastmessage)
        return toastmessage

    # 通过ID查找到元素并且返回enabled属性的状态
    def findbyresourceId_and_return_enabledstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    # 通过ID查找到元素并且text内容
    def findbyresourceId_and_return_text(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    # 通过ID查找到元素并且返回selected属性状态
    def findbyresourceId_and_return_selectedstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    # 通过text查找到元素
    def findbytext(self, text):
        d = self.d
        try:
            ele = d(text=text)
            eletext = self.geteleinfo_text(ele)
            if eletext == "":
                self.outPutMyLog("没有定位到控件。")
            else:
                self.outPutMyLog("定位到text为[%s]的控件。" % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog("出错原因：定位控件失败.具体原因：%s" % e)
            self.delaytime(3)

    # 通过text查找到元素并且输入内容

    def findbytext_and_input(self, text, inputtext):
        ele = self.findbytext(text)
        self.ele_input(ele, inputtext)

    # 通过text查找到元素并且点击内容
    def findbytext_and_click(self, text, outpretoastmessage=None):
        ele = self.findbytext(text)
        toastmessage = self.ele_click_and_return_toastmessage(
            ele, outpretoastmessage)
        return toastmessage

    # 通过text查找到元素并且返回enabled状态
    def findbytext_and_return_enabledstatus(self, text):
        ele = self.findbytext(text)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    # 通过text查找到元素并且返回enabled状态
    def findbytext_and_return_text(self, text):
        ele = self.findbytext(text)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    # 通过text查找到元素并且返回selected状态
    def findbytext_and_return_selectedstatus(self, text):
        ele = self.findbytext(text)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    # 元素中输入内容
    def ele_input(self, ele, inputtext):
        ele.clear_text()
        ele.send_keys(inputtext)
        self.outPutMyLog("输入:%s。" % inputtext)
        self.delaytime(1)

    # 点击元素并返回toast提示信息
    def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None):
        ele.click()
        self.outPutMyLog("点击该控件。")
        if outpretoastmessage is None:
            self.delaytime(3)
            return None
        else:
            toastmessage = self.getToast()
            self.outPutMyLog("pretoastmessage:", outpretoastmessage)
            return toastmessage

    # 得到元素enabled属性值
    def geteleinfo_enabled(self, ele):
        value = 'enabled'
        eleinfo_enabled = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的enabled属性的值为：%s' % eleinfo_enabled)
        return eleinfo_enabled

    # 得到元素text属性值
    def geteleinfo_text(self, ele):
        value = 'text'
        eleinfo_text = self.geteleinfo_value(ele, value)
        if eleinfo_text != '':
            self.outPutMyLog('该控件的text属性的值为：%s' % eleinfo_text)
        return eleinfo_text

    # 得到元素selected属性值
    def geteleinfo_selected(self, ele):
        value = 'selected'
        eleinfo_selected = self.geteleinfo_value(ele, value)
        if eleinfo_selected != '':
            self.outPutMyLog('该控件的selected属性的值为：%s' % eleinfo_selected)
        return eleinfo_selected

    # 得到元素rect属性值
    def geteleinfo_rect(self, ele):
        value = 'bounds'
        eleinfo_bounds = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的bounds属性的值为：%s' % str(eleinfo_bounds))

        eleinfo_rect = {}
        rect_x = eleinfo_bounds["left"]
        rect_y = eleinfo_bounds["top"]
        rect_width = int(eleinfo_bounds["right"])-int(rect_x)
        rect_height = int(eleinfo_bounds["bottom"])-int(rect_y)
        # {"x": 692, "y": 838, "width": 216, "height": 278}

        eleinfo_rect["x"] = rect_x
        eleinfo_rect["y"] = rect_y
        eleinfo_rect["width"] = rect_width
        eleinfo_rect["height"] = rect_height

        self.outPutMyLog('bounds转为rect的值为:' )
        self.outPutMyLog(eleinfo_rect)

        return eleinfo_rect

    def get_zhidingquyu_image(self,rect_x,rect_y,rect_width,rect_height):
        #页面全图
        screen_path = self.getScreenshotNormal()

        coderange = (int(rect_x),int(rect_y),int(int(rect_x)+int(rect_width)),int(int(rect_y)+int(rect_height))) #写成我们需要截取的位置坐标
        pageScreenshot = Image.open(screen_path)   #打开截图
        imageScreen = pageScreenshot.crop(coderange)   #使用Image的crop函数，从截图中再次截取我们需要的区域,即验证码区域

        currentny = self.getTimeStrNY()  # 获取当前时间的年月

        firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),currentny)
        self.createdir(filedir=firedir)
        tStr = self.getTimeStr()
        path = '%s/eleimage_%s.png' % (firedir,tStr)

        imageScreen.save(path)   #保存区域图片
        return path

    #获取到元素控件的截图
    def geteleinfo_rect_screen(self, ele):
        eleinfo_rect_dict = self.geteleinfo_rect(ele=ele)
        rect_x = eleinfo_rect_dict["x"]
        rect_y = eleinfo_rect_dict["y"]
        rect_width = eleinfo_rect_dict["width"]
        rect_height = eleinfo_rect_dict["height"]
        ele_image_path = self.get_zhidingquyu_image(rect_x,rect_y,rect_width,rect_height)
        print("获取到元素的截图：")
        print(ele_image_path)
        return ele_image_path



    # 得到元素属性值
    def geteleinfo_value(self, ele, value):
        eleinfo = ele.info
        print("元素的信息：")
        print(eleinfo)
        # self.outPutMyLog('eleinfo:',eleinfo)
        eleinfo_value = eleinfo[value]
        return eleinfo_value


    # 得到toast提示信息
    def getToast(self):
        toastmessage = self.d.toast.get_message(5.0, default="")
        self.outPutMyLog("toastmessage:", toastmessage)
        return toastmessage

    # 得到设备信息
    def getdeviceinfo(self):
        deviceinfo = self.d.device_info
        self.outPutMyLog("deviceinfo:%s" % deviceinfo)
        self.outPutMyLog("deviceinfo类型:%s" % type(deviceinfo))
        return deviceinfo

    # 建立crash监听
    def createwatcher(self):
        d = self.d
        d.watcher('crash').when(text='很抱歉，“QRindo MCH”已停止运行。').click(text="确定")
        d.watcher("crash").triggered
        self.outPutMyLog('d.watcher:', d.watcher)

    # 获取时间串

    def getTimeStr(self):
        tStr = self.timeStr.getTimeStr()
        return tStr

    def getTimeStrNY(self):
        tStrNY = self.timeStr.getTimeStrNY()
        return tStrNY

    # 出错时，获取页面截图
    def getScreenshotError(self):
        d = self.d
        self.outPutMyLog("调用截取图片函数")
        currentny = self.getTimeStrNY()  # 获取当前时间的年月
        tStr = self.getTimeStr()
        # path = "../screenshots/screenpicture_%s.png"% tStr
        firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), currentny)
        self.createdir(filedir=firedir)
        path = '%s/screenpicture_%s.png' % (firedir, tStr)
        pathaboutmysql = r'media\report\%s\screenshots\screenpicture_%s.png' % (
            currentny, tStr)
        # path = '%s/screenshots/screenpicture_%s.png'%(str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),tStr)
        d.screenshot(path)
        self.outPutMyLog("*****")
        # self.outPutMyLog(path)
        self.outPutMyLog(pathaboutmysql)
        self.outPutMyLog("*****")
        return path

    # 正常，获取页面截图
    def getScreenshotNormal(self):
        d = self.d
        self.outPutMyLog("调用截取图片函数")
        tStr = self.getTimeStr()
        filedir = "%s/screenshots/" % str(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        self.createdir(filedir=filedir)
        path = '%s/screenpicture_%s.png' % (filedir, tStr)
        d.screenshot(path)
        self.outPutMyLog("*****")
        self.outPutMyLog(path)
        self.outPutMyLog("*****")
        return path

    # 自动创建目录
    def createdir(self, filedir):
        filelist = filedir.split("/")
        # print(filelist)
        long = len(filelist)
        # print(long)
        zuhefiledir = filelist[0]
        for i in range(1, long):
            zuhefiledir = zuhefiledir + "/" + filelist[i]
            if os.path.exists(zuhefiledir):
                self.outPutMyLog("已经存在目录：%s" % zuhefiledir)
            else:
                os.mkdir(zuhefiledir)
                self.outPutMyLog("已经创建目录：%s" % zuhefiledir)


if __name__ == "__main__":
    outdevice = "TWP6R20821001328"
    apppacakagename = "com.android.contacts"
    activityname = ".activities.PeopleActivity"
    # "com.android.settings/.HWSettings"
    #
    bf = BaseFrame(outdevice=outdevice)
    # bf.install_uiaumator2_yilai()
    # # # bf.findbytext_and_click(outpretoastmessage='',text="Show QR")
    dv = bf.getdeviceinfo()
    bf.startapp(packagename=apppacakagename,activityname=activityname)
    resourceId = "com.android.contacts:id/text_no_contacts"
    findstyle = "resourceId"
    styleparame = "com.android.contacts:id/text_no_contacts"
    bf.findelement(findstyle, styleparame)


    # # bf.start_weditor()
    # # bf.startapp(packagename=apppacakagename,activityname=activityname)
    # bf.install_uiaumator2_yilai()
    # # print(dv["model"])
    # # print(dv["version"])
    # # print(dv["display"]["width"])
    # # print(dv["display"]["height"])
    # bf.startapp()
    # pass
    # import uiautomator2 as u2
    # d = u2.connect_usb("XPL5T20618001379")
    # print(d.info)

3a057c1280b88955b93f910435c03e5868b37df1
<mask token>


class BaseFrame(object):

    def __init__(self, outdevice=None, apppacakagename=None, isusb=None):
        print(outdevice)
        try:
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        except Exception as e:
            print(e)
            self.install_uiaumator2_yilai()
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        self.timeStr = GetTimeStr()
        self.isstartapp = False

    def outPutMyLog(self, context):
        mylog = MyLog(context)
        mylog.runMyLog()

    def outPutErrorMyLog(self, context):
        mylog = MyLog(context)
        mylog.runErrorLog()

    def adbshell(self, order):
        d = self.d
        d.shell(order)
    <mask token>
    <mask token>

    def install_uiaumator2_yilai(self):
        current_path = os.path.dirname(os.path.abspath(__file__))
        print(current_path)
        app_uiautomator_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator.apk'
        print(app_uiautomator_apk_path)
        install_app_uiautomator_apk_order = 'adb install %s' % str(
            app_uiautomator_apk_path)
        self.exec_order(orderName=install_app_uiautomator_apk_order)
        self.delaytime(5)
        app_uiautomator_test_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator-test.apk'
        install_app_uiautomator_test_apk_order = 'adb install %s' % str(
            app_uiautomator_test_apk_path)
        self.exec_order(orderName=install_app_uiautomator_test_apk_order)
        self.delaytime(5)
        atx_agent_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'atx-agent'
        push_atx_agent_order = 'adb push %s /data/local/tmp' % str(
            atx_agent_path)
        self.exec_order(orderName=push_atx_agent_order)
        self.delaytime(5)
        set_atx_agent_order = 'adb shell chmod 755 /data/local/tmp/atx-agent'
        self.exec_order(orderName=set_atx_agent_order)
        self.delaytime(5)
        start_atx_agent_order = 'adb shell /data/local/tmp/atx-agent server -d'
        self.exec_order(orderName=start_atx_agent_order)
        self.delaytime(5)

    def startapp(self, packagename, activityname=None):
        self.stopapp(packagename=packagename)
        self.d.app_start(packagename, activityname)
        self.outPutMyLog('启动包名为[%s]的应用---------' % packagename)
        self.delaytime(3)

    def stopapp(self, packagename):
        self.d.app_stop(packagename)
        self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename)

    def delaytime(self, dalaytime):
        dalaytime = int(dalaytime)
        time.sleep(dalaytime)
        self.outPutMyLog('等待%d秒...' % dalaytime)
    <mask token>

    def clickhome(self):
        self.d.press('home')
        self.outPutMyLog('点击Home按键')
        self.delaytime(3)
    <mask token>

    def findelement(self, findstyle, styleparame):
        try:
            if findstyle == 'resourceId':
                ele = self.d(resourceId=styleparame)
            elif findstyle == 'text':
                ele = self.d(text=styleparame)
            elif findstyle == 'xpath':
                ele = self.d.xpath(styleparame)
            elerect = self.geteleinfo_rect_screen(ele)
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)

    def findelement_and_input(self, findstyle, styleparame, inputtext):
        ele = self.findelement(findstyle, styleparame)
        self.ele_input(ele, inputtext)
    <mask token>

    def findelement_and_return_enabledstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled
    <mask token>

    def findelement_and_return_selectedstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected
    <mask token>
    <mask token>
    <mask token>

    def findbyresourceId_and_return_enabledstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    def findbyresourceId_and_return_text(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    def findbyresourceId_and_return_selectedstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbytext(self, text):
        d = self.d
        try:
            ele = d(text=text)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('没有定位到控件。')
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)

    def findbytext_and_input(self, text, inputtext):
        ele = self.findbytext(text)
        self.ele_input(ele, inputtext)

    def findbytext_and_click(self, text, outpretoastmessage=None):
        ele = self.findbytext(text)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None):
        ele.click()
        self.outPutMyLog('点击该控件。')
        if outpretoastmessage is None:
            self.delaytime(3)
            return None
        else:
            toastmessage = self.getToast()
            self.outPutMyLog('pretoastmessage:', outpretoastmessage)
            return toastmessage
    <mask token>

    def geteleinfo_text(self, ele):
        value = 'text'
        eleinfo_text = self.geteleinfo_value(ele, value)
        if eleinfo_text != '':
            self.outPutMyLog('该控件的text属性的值为：%s' % eleinfo_text)
        return eleinfo_text
    <mask token>

    def geteleinfo_rect(self, ele):
        value = 'bounds'
        eleinfo_bounds = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的bounds属性的值为：%s' % str(eleinfo_bounds))
        eleinfo_rect = {}
        rect_x = eleinfo_bounds['left']
        rect_y = eleinfo_bounds['top']
        rect_width = int(eleinfo_bounds['right']) - int(rect_x)
        rect_height = int(eleinfo_bounds['bottom']) - int(rect_y)
        eleinfo_rect['x'] = rect_x
        eleinfo_rect['y'] = rect_y
        eleinfo_rect['width'] = rect_width
        eleinfo_rect['height'] = rect_height
        self.outPutMyLog('bounds转为rect的值为:')
        self.outPutMyLog(eleinfo_rect)
        return eleinfo_rect

    def get_zhidingquyu_image(self, rect_x, rect_y, rect_width, rect_height):
        screen_path = self.getScreenshotNormal()
        coderange = int(rect_x), int(rect_y), int(int(rect_x) + int(rect_width)
            ), int(int(rect_y) + int(rect_height))
        pageScreenshot = Image.open(screen_path)
        imageScreen = pageScreenshot.crop(coderange)
        currentny = self.getTimeStrNY()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        tStr = self.getTimeStr()
        path = '%s/eleimage_%s.png' % (firedir, tStr)
        imageScreen.save(path)
        return path
    <mask token>
    <mask token>

    def getToast(self):
        toastmessage = self.d.toast.get_message(5.0, default='')
        self.outPutMyLog('toastmessage:', toastmessage)
        return toastmessage

    def getdeviceinfo(self):
        deviceinfo = self.d.device_info
        self.outPutMyLog('deviceinfo:%s' % deviceinfo)
        self.outPutMyLog('deviceinfo类型:%s' % type(deviceinfo))
        return deviceinfo
    <mask token>

    def getTimeStr(self):
        tStr = self.timeStr.getTimeStr()
        return tStr

    def getTimeStrNY(self):
        tStrNY = self.timeStr.getTimeStrNY()
        return tStrNY
    <mask token>
    <mask token>
    <mask token>


<mask token>

<mask token>


class BaseFrame(object):

    def __init__(self, outdevice=None, apppacakagename=None, isusb=None):
        print(outdevice)
        try:
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        except Exception as e:
            print(e)
            self.install_uiaumator2_yilai()
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        self.timeStr = GetTimeStr()
        self.isstartapp = False

    def outPutMyLog(self, context):
        mylog = MyLog(context)
        mylog.runMyLog()

    def outPutErrorMyLog(self, context):
        mylog = MyLog(context)
        mylog.runErrorLog()

    def adbshell(self, order):
        d = self.d
        d.shell(order)
    <mask token>
    <mask token>

    def install_uiaumator2_yilai(self):
        current_path = os.path.dirname(os.path.abspath(__file__))
        print(current_path)
        app_uiautomator_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator.apk'
        print(app_uiautomator_apk_path)
        install_app_uiautomator_apk_order = 'adb install %s' % str(
            app_uiautomator_apk_path)
        self.exec_order(orderName=install_app_uiautomator_apk_order)
        self.delaytime(5)
        app_uiautomator_test_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator-test.apk'
        install_app_uiautomator_test_apk_order = 'adb install %s' % str(
            app_uiautomator_test_apk_path)
        self.exec_order(orderName=install_app_uiautomator_test_apk_order)
        self.delaytime(5)
        atx_agent_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'atx-agent'
        push_atx_agent_order = 'adb push %s /data/local/tmp' % str(
            atx_agent_path)
        self.exec_order(orderName=push_atx_agent_order)
        self.delaytime(5)
        set_atx_agent_order = 'adb shell chmod 755 /data/local/tmp/atx-agent'
        self.exec_order(orderName=set_atx_agent_order)
        self.delaytime(5)
        start_atx_agent_order = 'adb shell /data/local/tmp/atx-agent server -d'
        self.exec_order(orderName=start_atx_agent_order)
        self.delaytime(5)

    def startapp(self, packagename, activityname=None):
        self.stopapp(packagename=packagename)
        self.d.app_start(packagename, activityname)
        self.outPutMyLog('启动包名为[%s]的应用---------' % packagename)
        self.delaytime(3)

    def stopapp(self, packagename):
        self.d.app_stop(packagename)
        self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename)

    def delaytime(self, dalaytime):
        dalaytime = int(dalaytime)
        time.sleep(dalaytime)
        self.outPutMyLog('等待%d秒...' % dalaytime)
    <mask token>

    def clickhome(self):
        self.d.press('home')
        self.outPutMyLog('点击Home按键')
        self.delaytime(3)
    <mask token>

    def findelement(self, findstyle, styleparame):
        try:
            if findstyle == 'resourceId':
                ele = self.d(resourceId=styleparame)
            elif findstyle == 'text':
                ele = self.d(text=styleparame)
            elif findstyle == 'xpath':
                ele = self.d.xpath(styleparame)
            elerect = self.geteleinfo_rect_screen(ele)
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)

    def findelement_and_input(self, findstyle, styleparame, inputtext):
        ele = self.findelement(findstyle, styleparame)
        self.ele_input(ele, inputtext)

    def findelement_and_click(self, findstyle, styleparame,
        outpretoastmessage=None):
        ele = self.findelement(findstyle, styleparame)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage

    def findelement_and_return_enabledstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled
    <mask token>

    def findelement_and_return_selectedstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbyresourceId(self, resourceId):
        d = self.d
        try:
            ele = d(resourceId=resourceId)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('定位到resourceId为[%s]的控件。' % resourceId)
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
    <mask token>
    <mask token>

    def findbyresourceId_and_return_enabledstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    def findbyresourceId_and_return_text(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    def findbyresourceId_and_return_selectedstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbytext(self, text):
        d = self.d
        try:
            ele = d(text=text)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('没有定位到控件。')
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)

    def findbytext_and_input(self, text, inputtext):
        ele = self.findbytext(text)
        self.ele_input(ele, inputtext)

    def findbytext_and_click(self, text, outpretoastmessage=None):
        ele = self.findbytext(text)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None):
        ele.click()
        self.outPutMyLog('点击该控件。')
        if outpretoastmessage is None:
            self.delaytime(3)
            return None
        else:
            toastmessage = self.getToast()
            self.outPutMyLog('pretoastmessage:', outpretoastmessage)
            return toastmessage
    <mask token>

    def geteleinfo_text(self, ele):
        value = 'text'
        eleinfo_text = self.geteleinfo_value(ele, value)
        if eleinfo_text != '':
            self.outPutMyLog('该控件的text属性的值为：%s' % eleinfo_text)
        return eleinfo_text
    <mask token>

    def geteleinfo_rect(self, ele):
        value = 'bounds'
        eleinfo_bounds = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的bounds属性的值为：%s' % str(eleinfo_bounds))
        eleinfo_rect = {}
        rect_x = eleinfo_bounds['left']
        rect_y = eleinfo_bounds['top']
        rect_width = int(eleinfo_bounds['right']) - int(rect_x)
        rect_height = int(eleinfo_bounds['bottom']) - int(rect_y)
        eleinfo_rect['x'] = rect_x
        eleinfo_rect['y'] = rect_y
        eleinfo_rect['width'] = rect_width
        eleinfo_rect['height'] = rect_height
        self.outPutMyLog('bounds转为rect的值为:')
        self.outPutMyLog(eleinfo_rect)
        return eleinfo_rect

    def get_zhidingquyu_image(self, rect_x, rect_y, rect_width, rect_height):
        screen_path = self.getScreenshotNormal()
        coderange = int(rect_x), int(rect_y), int(int(rect_x) + int(rect_width)
            ), int(int(rect_y) + int(rect_height))
        pageScreenshot = Image.open(screen_path)
        imageScreen = pageScreenshot.crop(coderange)
        currentny = self.getTimeStrNY()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        tStr = self.getTimeStr()
        path = '%s/eleimage_%s.png' % (firedir, tStr)
        imageScreen.save(path)
        return path
    <mask token>
    <mask token>

    def getToast(self):
        toastmessage = self.d.toast.get_message(5.0, default='')
        self.outPutMyLog('toastmessage:', toastmessage)
        return toastmessage

    def getdeviceinfo(self):
        deviceinfo = self.d.device_info
        self.outPutMyLog('deviceinfo:%s' % deviceinfo)
        self.outPutMyLog('deviceinfo类型:%s' % type(deviceinfo))
        return deviceinfo
    <mask token>

    def getTimeStr(self):
        tStr = self.timeStr.getTimeStr()
        return tStr

    def getTimeStrNY(self):
        tStrNY = self.timeStr.getTimeStrNY()
        return tStrNY

    def getScreenshotError(self):
        d = self.d
        self.outPutMyLog('调用截取图片函数')
        currentny = self.getTimeStrNY()
        tStr = self.getTimeStr()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        path = '%s/screenpicture_%s.png' % (firedir, tStr)
        pathaboutmysql = (
            'media\\report\\%s\\screenshots\\screenpicture_%s.png' % (
            currentny, tStr))
        d.screenshot(path)
        self.outPutMyLog('*****')
        self.outPutMyLog(pathaboutmysql)
        self.outPutMyLog('*****')
        return path
    <mask token>
    <mask token>


<mask token>

<mask token>


class BaseFrame(object):

    def __init__(self, outdevice=None, apppacakagename=None, isusb=None):
        print(outdevice)
        try:
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        except Exception as e:
            print(e)
            self.install_uiaumator2_yilai()
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        self.timeStr = GetTimeStr()
        self.isstartapp = False

    def outPutMyLog(self, context):
        mylog = MyLog(context)
        mylog.runMyLog()

    def outPutErrorMyLog(self, context):
        mylog = MyLog(context)
        mylog.runErrorLog()

    def adbshell(self, order):
        d = self.d
        d.shell(order)
    <mask token>

    def exec_order(self, orderName):
        check = os.popen(orderName)
        c = check.read()
        print('执行命令：%s' % orderName)
        print('执行命令后返回的内容：%s' % c)
        check.close()
        return c

    def install_uiaumator2_yilai(self):
        current_path = os.path.dirname(os.path.abspath(__file__))
        print(current_path)
        app_uiautomator_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator.apk'
        print(app_uiautomator_apk_path)
        install_app_uiautomator_apk_order = 'adb install %s' % str(
            app_uiautomator_apk_path)
        self.exec_order(orderName=install_app_uiautomator_apk_order)
        self.delaytime(5)
        app_uiautomator_test_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator-test.apk'
        install_app_uiautomator_test_apk_order = 'adb install %s' % str(
            app_uiautomator_test_apk_path)
        self.exec_order(orderName=install_app_uiautomator_test_apk_order)
        self.delaytime(5)
        atx_agent_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'atx-agent'
        push_atx_agent_order = 'adb push %s /data/local/tmp' % str(
            atx_agent_path)
        self.exec_order(orderName=push_atx_agent_order)
        self.delaytime(5)
        set_atx_agent_order = 'adb shell chmod 755 /data/local/tmp/atx-agent'
        self.exec_order(orderName=set_atx_agent_order)
        self.delaytime(5)
        start_atx_agent_order = 'adb shell /data/local/tmp/atx-agent server -d'
        self.exec_order(orderName=start_atx_agent_order)
        self.delaytime(5)

    def startapp(self, packagename, activityname=None):
        self.stopapp(packagename=packagename)
        self.d.app_start(packagename, activityname)
        self.outPutMyLog('启动包名为[%s]的应用---------' % packagename)
        self.delaytime(3)

    def stopapp(self, packagename):
        self.d.app_stop(packagename)
        self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename)

    def delaytime(self, dalaytime):
        dalaytime = int(dalaytime)
        time.sleep(dalaytime)
        self.outPutMyLog('等待%d秒...' % dalaytime)
    <mask token>

    def clickhome(self):
        self.d.press('home')
        self.outPutMyLog('点击Home按键')
        self.delaytime(3)
    <mask token>

    def findelement(self, findstyle, styleparame):
        try:
            if findstyle == 'resourceId':
                ele = self.d(resourceId=styleparame)
            elif findstyle == 'text':
                ele = self.d(text=styleparame)
            elif findstyle == 'xpath':
                ele = self.d.xpath(styleparame)
            elerect = self.geteleinfo_rect_screen(ele)
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)

    def findelement_and_input(self, findstyle, styleparame, inputtext):
        ele = self.findelement(findstyle, styleparame)
        self.ele_input(ele, inputtext)

    def findelement_and_click(self, findstyle, styleparame,
        outpretoastmessage=None):
        ele = self.findelement(findstyle, styleparame)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage

    def findelement_and_return_enabledstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled
    <mask token>

    def findelement_and_return_selectedstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbyresourceId(self, resourceId):
        d = self.d
        try:
            ele = d(resourceId=resourceId)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('定位到resourceId为[%s]的控件。' % resourceId)
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)

    def findbyresourceId_and_input(self, resourceId, inputtext):
        ele = self.findbyresourceId(resourceId)
        self.ele_input(ele, inputtext)

    def findbyresourceId_and_click(self, resourceId, outpretoastmessage=None):
        ele = self.findbyresourceId(resourceId)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage

    def findbyresourceId_and_return_enabledstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    def findbyresourceId_and_return_text(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    def findbyresourceId_and_return_selectedstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbytext(self, text):
        d = self.d
        try:
            ele = d(text=text)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('没有定位到控件。')
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)

    def findbytext_and_input(self, text, inputtext):
        ele = self.findbytext(text)
        self.ele_input(ele, inputtext)

    def findbytext_and_click(self, text, outpretoastmessage=None):
        ele = self.findbytext(text)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage
    <mask token>
    <mask token>

    def findbytext_and_return_selectedstatus(self, text):
        ele = self.findbytext(text)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def ele_input(self, ele, inputtext):
        ele.clear_text()
        ele.send_keys(inputtext)
        self.outPutMyLog('输入:%s。' % inputtext)
        self.delaytime(1)

    def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None):
        ele.click()
        self.outPutMyLog('点击该控件。')
        if outpretoastmessage is None:
            self.delaytime(3)
            return None
        else:
            toastmessage = self.getToast()
            self.outPutMyLog('pretoastmessage:', outpretoastmessage)
            return toastmessage
    <mask token>

    def geteleinfo_text(self, ele):
        value = 'text'
        eleinfo_text = self.geteleinfo_value(ele, value)
        if eleinfo_text != '':
            self.outPutMyLog('该控件的text属性的值为：%s' % eleinfo_text)
        return eleinfo_text
    <mask token>

    def geteleinfo_rect(self, ele):
        value = 'bounds'
        eleinfo_bounds = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的bounds属性的值为：%s' % str(eleinfo_bounds))
        eleinfo_rect = {}
        rect_x = eleinfo_bounds['left']
        rect_y = eleinfo_bounds['top']
        rect_width = int(eleinfo_bounds['right']) - int(rect_x)
        rect_height = int(eleinfo_bounds['bottom']) - int(rect_y)
        eleinfo_rect['x'] = rect_x
        eleinfo_rect['y'] = rect_y
        eleinfo_rect['width'] = rect_width
        eleinfo_rect['height'] = rect_height
        self.outPutMyLog('bounds转为rect的值为:')
        self.outPutMyLog(eleinfo_rect)
        return eleinfo_rect

    def get_zhidingquyu_image(self, rect_x, rect_y, rect_width, rect_height):
        screen_path = self.getScreenshotNormal()
        coderange = int(rect_x), int(rect_y), int(int(rect_x) + int(rect_width)
            ), int(int(rect_y) + int(rect_height))
        pageScreenshot = Image.open(screen_path)
        imageScreen = pageScreenshot.crop(coderange)
        currentny = self.getTimeStrNY()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        tStr = self.getTimeStr()
        path = '%s/eleimage_%s.png' % (firedir, tStr)
        imageScreen.save(path)
        return path
    <mask token>
    <mask token>

    def getToast(self):
        toastmessage = self.d.toast.get_message(5.0, default='')
        self.outPutMyLog('toastmessage:', toastmessage)
        return toastmessage

    def getdeviceinfo(self):
        deviceinfo = self.d.device_info
        self.outPutMyLog('deviceinfo:%s' % deviceinfo)
        self.outPutMyLog('deviceinfo类型:%s' % type(deviceinfo))
        return deviceinfo

    def createwatcher(self):
        d = self.d
        d.watcher('crash').when(text='很抱歉，“QRindo MCH”已停止运行。').click(text='确定')
        d.watcher('crash').triggered
        self.outPutMyLog('d.watcher:', d.watcher)

    def getTimeStr(self):
        tStr = self.timeStr.getTimeStr()
        return tStr

    def getTimeStrNY(self):
        tStrNY = self.timeStr.getTimeStrNY()
        return tStrNY

    def getScreenshotError(self):
        d = self.d
        self.outPutMyLog('调用截取图片函数')
        currentny = self.getTimeStrNY()
        tStr = self.getTimeStr()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        path = '%s/screenpicture_%s.png' % (firedir, tStr)
        pathaboutmysql = (
            'media\\report\\%s\\screenshots\\screenpicture_%s.png' % (
            currentny, tStr))
        d.screenshot(path)
        self.outPutMyLog('*****')
        self.outPutMyLog(pathaboutmysql)
        self.outPutMyLog('*****')
        return path
    <mask token>
    <mask token>


<mask token>

<mask token>


class BaseFrame(object):

    def __init__(self, outdevice=None, apppacakagename=None, isusb=None):
        print(outdevice)
        try:
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        except Exception as e:
            print(e)
            self.install_uiaumator2_yilai()
            if outdevice is None:
                self.d = u2.connect()
            else:
                self.d = u2.connect_usb(outdevice)
        self.timeStr = GetTimeStr()
        self.isstartapp = False

    def outPutMyLog(self, context):
        mylog = MyLog(context)
        mylog.runMyLog()

    def outPutErrorMyLog(self, context):
        mylog = MyLog(context)
        mylog.runErrorLog()

    def adbshell(self, order):
        d = self.d
        d.shell(order)

    def start_weditor(self):
        order = 'python -m weditor'
        self.adbshell(order=order)

    def exec_order(self, orderName):
        check = os.popen(orderName)
        c = check.read()
        print('执行命令：%s' % orderName)
        print('执行命令后返回的内容：%s' % c)
        check.close()
        return c

    def install_uiaumator2_yilai(self):
        current_path = os.path.dirname(os.path.abspath(__file__))
        print(current_path)
        app_uiautomator_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator.apk'
        print(app_uiautomator_apk_path)
        install_app_uiautomator_apk_order = 'adb install %s' % str(
            app_uiautomator_apk_path)
        self.exec_order(orderName=install_app_uiautomator_apk_order)
        self.delaytime(5)
        app_uiautomator_test_apk_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'app-uiautomator-test.apk'
        install_app_uiautomator_test_apk_order = 'adb install %s' % str(
            app_uiautomator_test_apk_path)
        self.exec_order(orderName=install_app_uiautomator_test_apk_order)
        self.delaytime(5)
        atx_agent_path = str(current_path
            ) + '\\' + 'uiautomator2yilaibao' + '\\' + 'atx-agent'
        push_atx_agent_order = 'adb push %s /data/local/tmp' % str(
            atx_agent_path)
        self.exec_order(orderName=push_atx_agent_order)
        self.delaytime(5)
        set_atx_agent_order = 'adb shell chmod 755 /data/local/tmp/atx-agent'
        self.exec_order(orderName=set_atx_agent_order)
        self.delaytime(5)
        start_atx_agent_order = 'adb shell /data/local/tmp/atx-agent server -d'
        self.exec_order(orderName=start_atx_agent_order)
        self.delaytime(5)

    def startapp(self, packagename, activityname=None):
        self.stopapp(packagename=packagename)
        self.d.app_start(packagename, activityname)
        self.outPutMyLog('启动包名为[%s]的应用---------' % packagename)
        self.delaytime(3)

    def stopapp(self, packagename):
        self.d.app_stop(packagename)
        self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename)

    def delaytime(self, dalaytime):
        dalaytime = int(dalaytime)
        time.sleep(dalaytime)
        self.outPutMyLog('等待%d秒...' % dalaytime)

    def clickback(self):
        self.d.press('back')
        self.outPutMyLog('点击返回按键')
        self.delaytime(3)

    def clickhome(self):
        self.d.press('home')
        self.outPutMyLog('点击Home按键')
        self.delaytime(3)

    def ele_is_exist(self, findstyle, styleparame):
        try:
            if findstyle == 'resourceId':
                ele = self.d(resourceId=styleparame)
                ele.info
            elif findstyle == 'text':
                ele = self.d(text=styleparame)
                ele.info
        except Exception as e:
            return False
        else:
            return True

    def findelement(self, findstyle, styleparame):
        try:
            if findstyle == 'resourceId':
                ele = self.d(resourceId=styleparame)
            elif findstyle == 'text':
                ele = self.d(text=styleparame)
            elif findstyle == 'xpath':
                ele = self.d.xpath(styleparame)
            elerect = self.geteleinfo_rect_screen(ele)
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)

    def findelement_and_input(self, findstyle, styleparame, inputtext):
        ele = self.findelement(findstyle, styleparame)
        self.ele_input(ele, inputtext)

    def findelement_and_click(self, findstyle, styleparame,
        outpretoastmessage=None):
        ele = self.findelement(findstyle, styleparame)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage

    def findelement_and_return_enabledstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled
    <mask token>

    def findelement_and_return_selectedstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbyresourceId(self, resourceId):
        d = self.d
        try:
            ele = d(resourceId=resourceId)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('定位到resourceId为[%s]的控件。' % resourceId)
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)

    def findbyresourceId_and_input(self, resourceId, inputtext):
        ele = self.findbyresourceId(resourceId)
        self.ele_input(ele, inputtext)

    def findbyresourceId_and_click(self, resourceId, outpretoastmessage=None):
        ele = self.findbyresourceId(resourceId)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage

    def findbyresourceId_and_return_enabledstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    def findbyresourceId_and_return_text(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    def findbyresourceId_and_return_selectedstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def findbytext(self, text):
        d = self.d
        try:
            ele = d(text=text)
            eletext = self.geteleinfo_text(ele)
            if eletext == '':
                self.outPutMyLog('没有定位到控件。')
            else:
                self.outPutMyLog('定位到text为[%s]的控件。' % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog('self.isstartappd的值为：%s' % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog('出错原因：定位控件失败.具体原因：%s' % e)
            self.delaytime(3)

    def findbytext_and_input(self, text, inputtext):
        ele = self.findbytext(text)
        self.ele_input(ele, inputtext)

    def findbytext_and_click(self, text, outpretoastmessage=None):
        ele = self.findbytext(text)
        toastmessage = self.ele_click_and_return_toastmessage(ele,
            outpretoastmessage)
        return toastmessage
    <mask token>

    def findbytext_and_return_text(self, text):
        ele = self.findbytext(text)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    def findbytext_and_return_selectedstatus(self, text):
        ele = self.findbytext(text)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    def ele_input(self, ele, inputtext):
        ele.clear_text()
        ele.send_keys(inputtext)
        self.outPutMyLog('输入:%s。' % inputtext)
        self.delaytime(1)

    def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None):
        ele.click()
        self.outPutMyLog('点击该控件。')
        if outpretoastmessage is None:
            self.delaytime(3)
            return None
        else:
            toastmessage = self.getToast()
            self.outPutMyLog('pretoastmessage:', outpretoastmessage)
            return toastmessage
    <mask token>

    def geteleinfo_text(self, ele):
        value = 'text'
        eleinfo_text = self.geteleinfo_value(ele, value)
        if eleinfo_text != '':
            self.outPutMyLog('该控件的text属性的值为：%s' % eleinfo_text)
        return eleinfo_text

    def geteleinfo_selected(self, ele):
        value = 'selected'
        eleinfo_selected = self.geteleinfo_value(ele, value)
        if eleinfo_selected != '':
            self.outPutMyLog('该控件的selected属性的值为：%s' % eleinfo_selected)
        return eleinfo_selected

    def geteleinfo_rect(self, ele):
        value = 'bounds'
        eleinfo_bounds = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的bounds属性的值为：%s' % str(eleinfo_bounds))
        eleinfo_rect = {}
        rect_x = eleinfo_bounds['left']
        rect_y = eleinfo_bounds['top']
        rect_width = int(eleinfo_bounds['right']) - int(rect_x)
        rect_height = int(eleinfo_bounds['bottom']) - int(rect_y)
        eleinfo_rect['x'] = rect_x
        eleinfo_rect['y'] = rect_y
        eleinfo_rect['width'] = rect_width
        eleinfo_rect['height'] = rect_height
        self.outPutMyLog('bounds转为rect的值为:')
        self.outPutMyLog(eleinfo_rect)
        return eleinfo_rect

    def get_zhidingquyu_image(self, rect_x, rect_y, rect_width, rect_height):
        screen_path = self.getScreenshotNormal()
        coderange = int(rect_x), int(rect_y), int(int(rect_x) + int(rect_width)
            ), int(int(rect_y) + int(rect_height))
        pageScreenshot = Image.open(screen_path)
        imageScreen = pageScreenshot.crop(coderange)
        currentny = self.getTimeStrNY()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        tStr = self.getTimeStr()
        path = '%s/eleimage_%s.png' % (firedir, tStr)
        imageScreen.save(path)
        return path

    def geteleinfo_rect_screen(self, ele):
        eleinfo_rect_dict = self.geteleinfo_rect(ele=ele)
        rect_x = eleinfo_rect_dict['x']
        rect_y = eleinfo_rect_dict['y']
        rect_width = eleinfo_rect_dict['width']
        rect_height = eleinfo_rect_dict['height']
        ele_image_path = self.get_zhidingquyu_image(rect_x, rect_y,
            rect_width, rect_height)
        print('获取到元素的截图：')
        print(ele_image_path)
        return ele_image_path

    def geteleinfo_value(self, ele, value):
        eleinfo = ele.info
        print('元素的信息：')
        print(eleinfo)
        eleinfo_value = eleinfo[value]
        return eleinfo_value

    def getToast(self):
        toastmessage = self.d.toast.get_message(5.0, default='')
        self.outPutMyLog('toastmessage:', toastmessage)
        return toastmessage

    def getdeviceinfo(self):
        deviceinfo = self.d.device_info
        self.outPutMyLog('deviceinfo:%s' % deviceinfo)
        self.outPutMyLog('deviceinfo类型:%s' % type(deviceinfo))
        return deviceinfo

    def createwatcher(self):
        d = self.d
        d.watcher('crash').when(text='很抱歉，“QRindo MCH”已停止运行。').click(text='确定')
        d.watcher('crash').triggered
        self.outPutMyLog('d.watcher:', d.watcher)

    def getTimeStr(self):
        tStr = self.timeStr.getTimeStr()
        return tStr

    def getTimeStrNY(self):
        tStrNY = self.timeStr.getTimeStrNY()
        return tStrNY

    def getScreenshotError(self):
        d = self.d
        self.outPutMyLog('调用截取图片函数')
        currentny = self.getTimeStrNY()
        tStr = self.getTimeStr()
        firedir = '%s/media/report/%s/screenshots/' % (os.path.dirname(os.
            path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            currentny)
        self.createdir(filedir=firedir)
        path = '%s/screenpicture_%s.png' % (firedir, tStr)
        pathaboutmysql = (
            'media\\report\\%s\\screenshots\\screenpicture_%s.png' % (
            currentny, tStr))
        d.screenshot(path)
        self.outPutMyLog('*****')
        self.outPutMyLog(pathaboutmysql)
        self.outPutMyLog('*****')
        return path
    <mask token>

    def createdir(self, filedir):
        filelist = filedir.split('/')
        long = len(filelist)
        zuhefiledir = filelist[0]
        for i in range(1, long):
            zuhefiledir = zuhefiledir + '/' + filelist[i]
            if os.path.exists(zuhefiledir):
                self.outPutMyLog('已经存在目录：%s' % zuhefiledir)
            else:
                os.mkdir(zuhefiledir)
                self.outPutMyLog('已经创建目录：%s' % zuhefiledir)


<mask token>

# _*_ coding:utf-8 _*_

__author__ = 'bobby'
__date__ = '2018/10/9 15:55'
import os
import uiautomator2 as u2
import time
from AndroidUITest.util.gettimestr import GetTimeStr  # 导入获取时间串函数
from AndroidUITest.util.my_log import MyLog
from PIL import Image   #导入Image
from PIL import ImageEnhance  #导入ImageEnhance


class BaseFrame(object):
    def __init__(self, outdevice=None, apppacakagename=None, isusb=None):
        print(outdevice)
        try:
            if outdevice is None:
                # self.d = u2.connect('192.168.199.168')
                # self.d = u2.connect_usb('192.168.1.100:5555')
                self.d = u2.connect()  # 没有输入设备号则默认不要设备号
            else:
                self.d = u2.connect_usb(outdevice)

        except Exception as e:
            print(e)
            self.install_uiaumator2_yilai()  # 先安装

            # 然后再运行
            if outdevice is None:
                # self.d = u2.connect('192.168.199.168')
                # self.d = u2.connect_usb('192.168.1.100:5555')
                self.d = u2.connect()  # 没有输入设备号则默认不要设备号
            else:
                self.d = u2.connect_usb(outdevice)

        # if apppacakagename == None:
        #     self.packagename = "com.ahdi.qrindo.wallet"
        # else:
        #     self.packagename =apppacakagename
        self.timeStr = GetTimeStr()  # 实例化
        self.isstartapp = False

    # 打印日志
    def outPutMyLog(self, context):
        mylog = MyLog(context)
        mylog.runMyLog()

    # 打印错误日志
    def outPutErrorMyLog(self, context):
        mylog = MyLog(context)
        mylog.runErrorLog()

    # 执行shell命令

    def adbshell(self, order):
        d = self.d
        d.shell(order)
        # self.outPutMyLog('输入shell命令:',order,'。\n')

    # python -m weditor
    def start_weditor(self):
        # order = "python -m uiautomator2 init"
        order = "python -m weditor"
        self.adbshell(order=order)

    # 执行cmd命令
    def exec_order(self, orderName):
        check = os.popen(orderName)
        c = check.read()
        print("执行命令：%s" % orderName)
        print("执行命令后返回的内容：%s" % c)
        check.close()
        return c

    # 安装uiautomator2依赖脚本
    def install_uiaumator2_yilai(self):
        # 获取当前文件路径
        current_path = os.path.dirname(os.path.abspath(__file__))
        print(current_path)

        # 安装app-uiautomator.apk
        app_uiautomator_apk_path = str(
            current_path) + "\\" + "uiautomator2yilaibao" + "\\" + "app-uiautomator.apk"
        print(app_uiautomator_apk_path)
        install_app_uiautomator_apk_order = "adb install %s" % str(
            app_uiautomator_apk_path)
        self.exec_order(orderName=install_app_uiautomator_apk_order)
        self.delaytime(5)

        # 安装app-uiautomator-test.apk
        app_uiautomator_test_apk_path = str(
            current_path) + "\\" + "uiautomator2yilaibao" + "\\" + "app-uiautomator-test.apk"
        install_app_uiautomator_test_apk_order = "adb install %s" % str(
            app_uiautomator_test_apk_path)
        self.exec_order(orderName=install_app_uiautomator_test_apk_order)

        self.delaytime(5)

        # 将atx-agent推送到手机的/data/local/tmp/目录中
        atx_agent_path = str(current_path) + "\\" + \
            "uiautomator2yilaibao" + "\\" + "atx-agent"
        push_atx_agent_order = "adb push %s /data/local/tmp" % str(
            atx_agent_path)
        self.exec_order(orderName=push_atx_agent_order)
        self.delaytime(5)

        # 设置手机端atx-agent权限为755，支持所有用户组可读可执行
        set_atx_agent_order = "adb shell chmod 755 /data/local/tmp/atx-agent"
        self.exec_order(orderName=set_atx_agent_order)
        self.delaytime(5)

        # 启动atx-agent并切换后台运行，默认**端口是7912
        start_atx_agent_order = "adb shell /data/local/tmp/atx-agent server -d"
        self.exec_order(orderName=start_atx_agent_order)
        self.delaytime(5)

    # 启动app

    def startapp(self, packagename, activityname=None):
        self.stopapp(packagename=packagename)  # 先关闭应用
        self.d.app_start(packagename, activityname)
        self.outPutMyLog('启动包名为[%s]的应用---------' % packagename)
        # self.outPutMyLog('设备信息：', d.device_info)
        self.delaytime(3)

    # 关闭app
    def stopapp(self, packagename):
        self.d.app_stop(packagename)
        self.outPutMyLog('关闭包名为[%s]的应用---------' % packagename)

    # 延时
    def delaytime(self, dalaytime):
        dalaytime = int(dalaytime)
        time.sleep(dalaytime)
        self.outPutMyLog('等待%d秒...' % dalaytime)

    # 点击返回按钮
    def clickback(self):
        self.d.press("back")
        self.outPutMyLog('点击返回按键')
        self.delaytime(3)

    # 点击Home按钮
    def clickhome(self):
        self.d.press("home")
        self.outPutMyLog('点击Home按键')
        self.delaytime(3)

    # 元素是否存在
    def ele_is_exist(self, findstyle, styleparame):
        try:
            if findstyle == "resourceId":
                ele = self.d(resourceId=styleparame)
                ele.info
                # self.outPutMyLog("ele.info:%s" % ele.info)
                # self.outPutMyLog("找到元素")
            elif findstyle == "text":
                ele = self.d(text=styleparame)
                ele.info
                # self.outPutMyLog("ele.info:%s"% ele.info)
                # self.outPutMyLog("找到元素")
        except Exception as e:
            # self.outPutMyLog("报错：%s"% e)
            return False
        else:
            return True

    # 查找元素
    def findelement(self, findstyle, styleparame):
        try:
            if findstyle == "resourceId":
                ele = self.d(resourceId=styleparame)
            elif findstyle == "text":
                ele = self.d(text=styleparame)
            elif findstyle == "xpath":
                ele = self.d.xpath(styleparame)

            elerect = self.geteleinfo_rect_screen(ele)  #调用元素属性，如果没有成功，则说明元素不存在


        except Exception as e:
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog("出错原因：定位控件失败.具体原因：%s" % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)

    # 查找到元素并且输入内容

    def findelement_and_input(self, findstyle, styleparame, inputtext):
        ele = self.findelement(findstyle, styleparame)
        self.ele_input(ele, inputtext)

    # 查找到元素并且点击该元素
    def findelement_and_click(
            self,
            findstyle,
            styleparame,
            outpretoastmessage=None):
        ele = self.findelement(findstyle, styleparame)
        toastmessage = self.ele_click_and_return_toastmessage(
            ele, outpretoastmessage)
        return toastmessage

    # 查找到元素并且返回enabled属性的状态
    def findelement_and_return_enabledstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    # 查找到元素并且text内容
    def findelement_and_return_text(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    # 查找到元素并且返回selected属性状态
    def findelement_and_return_selectedstatus(self, findstyle, styleparame):
        ele = self.findelement(findstyle, styleparame)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    # 通过ID查找到元素
    def findbyresourceId(self, resourceId):
        d = self.d
        try:
            ele = d(resourceId=resourceId)
            eletext = self.geteleinfo_text(ele)
            if eletext == "":
                self.outPutMyLog("定位到resourceId为[%s]的控件。" % resourceId)
            else:
                self.outPutMyLog("定位到text为[%s]的控件。" % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog("出错原因：定位控件失败.具体原因：%s" % e)
            self.delaytime(3)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)

    # 通过ID查找到元素并且输入内容
    def findbyresourceId_and_input(self, resourceId, inputtext):
        ele = self.findbyresourceId(resourceId)
        self.ele_input(ele, inputtext)

    # 通过ID查找到元素并且点击该元素
    def findbyresourceId_and_click(self, resourceId, outpretoastmessage=None):
        ele = self.findbyresourceId(resourceId)
        toastmessage = self.ele_click_and_return_toastmessage(
            ele, outpretoastmessage)
        return toastmessage

    # 通过ID查找到元素并且返回enabled属性的状态
    def findbyresourceId_and_return_enabledstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    # 通过ID查找到元素并且text内容
    def findbyresourceId_and_return_text(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    # 通过ID查找到元素并且返回selected属性状态
    def findbyresourceId_and_return_selectedstatus(self, resourceId):
        ele = self.findbyresourceId(resourceId)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    # 通过text查找到元素
    def findbytext(self, text):
        d = self.d
        try:
            ele = d(text=text)
            eletext = self.geteleinfo_text(ele)
            if eletext == "":
                self.outPutMyLog("没有定位到控件。")
            else:
                self.outPutMyLog("定位到text为[%s]的控件。" % eletext)
            return ele
        except Exception as e:
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.isstartapp = True
            self.outPutMyLog("self.isstartappd的值为：%s" % self.isstartapp)
            self.getScreenshotError()
            self.outPutErrorMyLog("出错原因：定位控件失败.具体原因：%s" % e)
            self.delaytime(3)

    # 通过text查找到元素并且输入内容

    def findbytext_and_input(self, text, inputtext):
        ele = self.findbytext(text)
        self.ele_input(ele, inputtext)

    # 通过text查找到元素并且点击内容
    def findbytext_and_click(self, text, outpretoastmessage=None):
        ele = self.findbytext(text)
        toastmessage = self.ele_click_and_return_toastmessage(
            ele, outpretoastmessage)
        return toastmessage

    # 通过text查找到元素并且返回enabled状态
    def findbytext_and_return_enabledstatus(self, text):
        ele = self.findbytext(text)
        eleinfo_enabled = self.geteleinfo_enabled(ele)
        return eleinfo_enabled

    # 通过text查找到元素并且返回enabled状态
    def findbytext_and_return_text(self, text):
        ele = self.findbytext(text)
        eleinfo_text = self.geteleinfo_text(ele)
        return eleinfo_text

    # 通过text查找到元素并且返回selected状态
    def findbytext_and_return_selectedstatus(self, text):
        ele = self.findbytext(text)
        eleinfo_selected = self.geteleinfo_selected(ele)
        return eleinfo_selected

    # 元素中输入内容
    def ele_input(self, ele, inputtext):
        ele.clear_text()
        ele.send_keys(inputtext)
        self.outPutMyLog("输入:%s。" % inputtext)
        self.delaytime(1)

    # 点击元素并返回toast提示信息
    def ele_click_and_return_toastmessage(self, ele, outpretoastmessage=None):
        ele.click()
        self.outPutMyLog("点击该控件。")
        if outpretoastmessage is None:
            self.delaytime(3)
            return None
        else:
            toastmessage = self.getToast()
            self.outPutMyLog("pretoastmessage:", outpretoastmessage)
            return toastmessage

    # 得到元素enabled属性值
    def geteleinfo_enabled(self, ele):
        value = 'enabled'
        eleinfo_enabled = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的enabled属性的值为：%s' % eleinfo_enabled)
        return eleinfo_enabled

    # 得到元素text属性值
    def geteleinfo_text(self, ele):
        value = 'text'
        eleinfo_text = self.geteleinfo_value(ele, value)
        if eleinfo_text != '':
            self.outPutMyLog('该控件的text属性的值为：%s' % eleinfo_text)
        return eleinfo_text

    # 得到元素selected属性值
    def geteleinfo_selected(self, ele):
        value = 'selected'
        eleinfo_selected = self.geteleinfo_value(ele, value)
        if eleinfo_selected != '':
            self.outPutMyLog('该控件的selected属性的值为：%s' % eleinfo_selected)
        return eleinfo_selected

    # 得到元素rect属性值
    def geteleinfo_rect(self, ele):
        value = 'bounds'
        eleinfo_bounds = self.geteleinfo_value(ele, value)
        self.outPutMyLog('该控件的bounds属性的值为：%s' % str(eleinfo_bounds))

        eleinfo_rect = {}
        rect_x = eleinfo_bounds["left"]
        rect_y = eleinfo_bounds["top"]
        rect_width = int(eleinfo_bounds["right"])-int(rect_x)
        rect_height = int(eleinfo_bounds["bottom"])-int(rect_y)
        # {"x": 692, "y": 838, "width": 216, "height": 278}

        eleinfo_rect["x"] = rect_x
        eleinfo_rect["y"] = rect_y
        eleinfo_rect["width"] = rect_width
        eleinfo_rect["height"] = rect_height

        self.outPutMyLog('bounds转为rect的值为:' )
        self.outPutMyLog(eleinfo_rect)

        return eleinfo_rect

    def get_zhidingquyu_image(self,rect_x,rect_y,rect_width,rect_height):
        #页面全图
        screen_path = self.getScreenshotNormal()

        coderange = (int(rect_x),int(rect_y),int(int(rect_x)+int(rect_width)),int(int(rect_y)+int(rect_height))) #写成我们需要截取的位置坐标
        pageScreenshot = Image.open(screen_path)   #打开截图
        imageScreen = pageScreenshot.crop(coderange)   #使用Image的crop函数，从截图中再次截取我们需要的区域,即验证码区域

        currentny = self.getTimeStrNY()  # 获取当前时间的年月

        firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),currentny)
        self.createdir(filedir=firedir)
        tStr = self.getTimeStr()
        path = '%s/eleimage_%s.png' % (firedir,tStr)

        imageScreen.save(path)   #保存区域图片
        return path

    #获取到元素控件的截图
    def geteleinfo_rect_screen(self, ele):
        eleinfo_rect_dict = self.geteleinfo_rect(ele=ele)
        rect_x = eleinfo_rect_dict["x"]
        rect_y = eleinfo_rect_dict["y"]
        rect_width = eleinfo_rect_dict["width"]
        rect_height = eleinfo_rect_dict["height"]
        ele_image_path = self.get_zhidingquyu_image(rect_x,rect_y,rect_width,rect_height)
        print("获取到元素的截图：")
        print(ele_image_path)
        return ele_image_path



    # 得到元素属性值
    def geteleinfo_value(self, ele, value):
        eleinfo = ele.info
        print("元素的信息：")
        print(eleinfo)
        # self.outPutMyLog('eleinfo:',eleinfo)
        eleinfo_value = eleinfo[value]
        return eleinfo_value


    # 得到toast提示信息
    def getToast(self):
        toastmessage = self.d.toast.get_message(5.0, default="")
        self.outPutMyLog("toastmessage:", toastmessage)
        return toastmessage

    # 得到设备信息
    def getdeviceinfo(self):
        deviceinfo = self.d.device_info
        self.outPutMyLog("deviceinfo:%s" % deviceinfo)
        self.outPutMyLog("deviceinfo类型:%s" % type(deviceinfo))
        return deviceinfo

    # 建立crash监听
    def createwatcher(self):
        d = self.d
        d.watcher('crash').when(text='很抱歉，“QRindo MCH”已停止运行。').click(text="确定")
        d.watcher("crash").triggered
        self.outPutMyLog('d.watcher:', d.watcher)

    # 获取时间串

    def getTimeStr(self):
        tStr = self.timeStr.getTimeStr()
        return tStr

    def getTimeStrNY(self):
        tStrNY = self.timeStr.getTimeStrNY()
        return tStrNY

    # 出错时，获取页面截图
    def getScreenshotError(self):
        d = self.d
        self.outPutMyLog("调用截取图片函数")
        currentny = self.getTimeStrNY()  # 获取当前时间的年月
        tStr = self.getTimeStr()
        # path = "../screenshots/screenpicture_%s.png"% tStr
        firedir = r'%s/media/report/%s/screenshots/' % (os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), currentny)
        self.createdir(filedir=firedir)
        path = '%s/screenpicture_%s.png' % (firedir, tStr)
        pathaboutmysql = r'media\report\%s\screenshots\screenpicture_%s.png' % (
            currentny, tStr)
        # path = '%s/screenshots/screenpicture_%s.png'%(str(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),tStr)
        d.screenshot(path)
        self.outPutMyLog("*****")
        # self.outPutMyLog(path)
        self.outPutMyLog(pathaboutmysql)
        self.outPutMyLog("*****")
        return path

    # 正常，获取页面截图
    def getScreenshotNormal(self):
        d = self.d
        self.outPutMyLog("调用截取图片函数")
        tStr = self.getTimeStr()
        filedir = "%s/screenshots/" % str(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        self.createdir(filedir=filedir)
        path = '%s/screenpicture_%s.png' % (filedir, tStr)
        d.screenshot(path)
        self.outPutMyLog("*****")
        self.outPutMyLog(path)
        self.outPutMyLog("*****")
        return path

    # 自动创建目录
    def createdir(self, filedir):
        filelist = filedir.split("/")
        # print(filelist)
        long = len(filelist)
        # print(long)
        zuhefiledir = filelist[0]
        for i in range(1, long):
            zuhefiledir = zuhefiledir + "/" + filelist[i]
            if os.path.exists(zuhefiledir):
                self.outPutMyLog("已经存在目录：%s" % zuhefiledir)
            else:
                os.mkdir(zuhefiledir)
                self.outPutMyLog("已经创建目录：%s" % zuhefiledir)


if __name__ == "__main__":
    outdevice = "TWP6R20821001328"
    apppacakagename = "com.android.contacts"
    activityname = ".activities.PeopleActivity"
    # "com.android.settings/.HWSettings"
    #
    bf = BaseFrame(outdevice=outdevice)
    # bf.install_uiaumator2_yilai()
    # # # bf.findbytext_and_click(outpretoastmessage='',text="Show QR")
    dv = bf.getdeviceinfo()
    bf.startapp(packagename=apppacakagename,activityname=activityname)
    resourceId = "com.android.contacts:id/text_no_contacts"
    findstyle = "resourceId"
    styleparame = "com.android.contacts:id/text_no_contacts"
    bf.findelement(findstyle, styleparame)


    # # bf.start_weditor()
    # # bf.startapp(packagename=apppacakagename,activityname=activityname)
    # bf.install_uiaumator2_yilai()
    # # print(dv["model"])
    # # print(dv["version"])
    # # print(dv["display"]["width"])
    # # print(dv["display"]["height"])
    # bf.startapp()
    # pass
    # import uiautomator2 as u2
    # d = u2.connect_usb("XPL5T20618001379")
    # print(d.info)
