def init_global_queue():
    db = DBTool()
    db.initGlobalDBConf()
    #======加入未执行的接口调试 数据表tb_http_interface_debug
    # init_debug_queue_interface(db)
    # init_debug_queue_case(db)
    init_task_queue(db)
    init_task_cancel_queue(db)
    db.release()
Exemple #2
0
def cancelTaskSuite(dataDict):
    try:
        redisCache = RedisTool()
        redisCache.initRedisConf()

        db = DBTool()
        db.initGlobalDBConf()

        # taskExecStatus = json.loads(redisCache.get_data("%s_taskSuite_%s_task_%s" % (dataDict[Do.TYPE_PROTOCOL],dataDict[Do.KEY_TASK_SUITE_EXECUTE_ID], dataDict[Do.KEY_TASK_EXECUTE_ID])))
        # taskExecStatus["execStatus"] = ExecStatus.CANCELED
        # redisCache.set_data("%s_taskSuite_%s_task_%s" % (dataDict[Do.TYPE_PROTOCOL],dataDict[Do.KEY_TASK_SUITE_EXECUTE_ID], dataDict[Do.KEY_TASK_EXECUTE_ID]),json.dumps(taskExecStatus),60*60*12)
        redisCache.del_data("%s_taskSuite_%s_task_%s" % (dataDict[Do.TYPE_PROTOCOL],dataDict[Do.KEY_TASK_SUITE_EXECUTE_ID], dataDict[Do.KEY_TASK_EXECUTE_ID]))

    finally:
    #将任务集设置为已取消
        if dataDict[Do.TYPE_PROTOCOL] == Protocol.HTTP_PROTOCOL:
            db.execute_sql("UPDATE tb_task_suite_execute SET execStatus = '%s' where id = %s" % (ExecStatus.CANCELED,dataDict[Do.KEY_TASK_SUITE_EXECUTE_ID]))
        elif dataDict[Do.TYPE_PROTOCOL] == Protocol.DUBBO_PROTOCOL:
            db.execute_sql("UPDATE tb2_dubbo_task_suite_execute SET execStatus = '%s' where id = %s" % (ExecStatus.CANCELED, dataDict[Do.KEY_TASK_SUITE_EXECUTE_ID]))
        db.release()
Exemple #3
0
def init_global_queue(taskQueueList,taskCancelQueueList):
    # taskQueue.put([])
    # taskCancelQueue.put([])
    # serviceListQueue.put([])
    redis = RedisTool()
    redis.initRedisConf()
    db = DBTool()
    db.initGlobalDBConf()
    #启动时将所有执行服务器设置为离线
    db.execute_sql("UPDATE tb_run_server_conf SET STATUS = 0 ")
    #======加入未执行的接口调试 数据表tb_http_interface_debug
    # init_debug_queue_interface(db)
    # init_debug_queue_case(db)
    init_task_queue(db,redis,taskQueueList)
    init_task_cancel_queue(db,redis,taskCancelQueueList)
    init_dubbo_task_queue(db,taskQueueList)
    init_dubbo_task_cancel_queue(db,taskCancelQueueList)
    init_task_suite_queue(db,"HTTP")
    init_task_suite_queue(db,"DUBBO")
    db.release()
class UITestTask(object):

    reportFileName = "report.html"
    logFileName = "report.log"
    pickleFileName = "unquitdriverlist.pkl"

    def __init__(self):
        #基本信息
        self.id = 0
        self.taskExecuteId = 0

        self.taskId = ""
        self.title = ""
        self.taskdesc = ""
        self.businessLineId = ""
        self.businessLineName = ""
        self.moduleId = ""
        self.moduleName = ""
        self.sourceGroup = ""

        self.fileName = ""
        self.fileAddBy = ""
        self.sheetName = ""
        self.httpConfKey = ""
        self.reportDir = ""

        self.packageId = ""
        self.serverId = ""
        #执行信息
        self.execStatus = 1  # 1 新建排队中未执行 2执行中 3执行完成 4执行异常
        self.execCommand = ""
        self.execStartTime = ""
        self.execEndTime = ""
        self.execTakeTime = ""
        self.execProgressString = ""
        #执行结果信息
        self.testResult = ""
        self.testResultMessage = ""

        self.execComments = ""
        self.isSendEmail = 0
        self.emailList = ""

        #通用信息
        self.state = 1
        self.addBy = ""
        self.modBy = ""
        self.addTime = ""
        self.modTime = ""

        self.globalDB = DBTool()

    def generateByTaskExecuteId(self):
        self.globalDB.initGlobalDBConf()
        self.taskExecuteId = int(self.taskExecuteId)
        sqlTasExec = "select * from tb_ui_test_execute where id=%d" % int(
            self.taskExecuteId)
        resTaskExec = self.globalDB.execute_sql(sqlTasExec)
        if resTaskExec:
            tmpResTaskInfo = resTaskExec[0]
            self.id = tmpResTaskInfo['id']

            self.taskId = tmpResTaskInfo['taskId']
            self.title = tmpResTaskInfo['title']
            self.taskdesc = tmpResTaskInfo['taskdesc']
            self.businessLineId = tmpResTaskInfo['businessLineId']
            self.moduleId = tmpResTaskInfo['moduleId']
            self.sourceGroup = tmpResTaskInfo['sourceGroup']

            self.fileName = tmpResTaskInfo['fileName']
            self.fileAddBy = tmpResTaskInfo["fileAddBy"]
            self.sheetName = tmpResTaskInfo['sheetName']
            self.httpConfKey = tmpResTaskInfo['httpConfKey']
            self.reportDir = tmpResTaskInfo['reportDir'].strip()
            self.addBy = tmpResTaskInfo['addBy']

            self.packageId = tmpResTaskInfo['packageId'].strip()
            self.serverId = tmpResTaskInfo["serverId"]

            self.execComments = tmpResTaskInfo['execComments']
            self.isSendEmail = int(tmpResTaskInfo['isSendEmail'])
            self.emailList = tmpResTaskInfo['emailList']

            self.testResult = tmpResTaskInfo['testResult']
            self.execStatus = tmpResTaskInfo['execStatus']

            self.generateBusinessLineAndModuleName()
        else:
            self.taskExecuteId = -1
        self.globalDB.release()

    def generateBusinessLineAndModuleName(self):
        self.globalDB.connect()
        businessLineDict = self.globalDB.execute_sql(
            "select * from tb_business_line where id=%d" %
            (int(self.businessLineId)))
        moduleDict = self.globalDB.execute_sql(
            "select * from tb_modules where id=%d" % (int(self.moduleId)))
        self.globalDB.release()
        self.businessLineName = businessLineDict[0]['bussinessLineName']
        self.moduleName = moduleDict[0]['moduleName']

    def getPackageInfo(self):
        self.globalDB.connect()
        packageInfo = self.globalDB.execute_sql(
            "select * from tb_ui_package where packageId='%s'" %
            self.packageId)
        if packageInfo:
            return packageInfo[0]
        else:
            return False

    def bindTaskMobileServer(self, taskExecuteId, serverId):
        self.globalDB.connect()
        self.globalDB.execute_sql(
            "UPDATE tb_ui_test_execute SET serverId = %s WHERE id = %s" %
            (taskExecuteId, serverId))

    def getDevice(self, serverType):
        self.globalDB.connect()
        if serverType.lower() == "android":
            typeKey = 1
        elif serverType.lower() == "ios":
            typeKey = 2
        mobileServer = self.globalDB.execute_sql(
            "select * from tb_ui_mobile_server where state = 1 and status = 0 and serverType = %s"
            % typeKey)
        if mobileServer:
            self.serverId = mobileServer[0]["id"]
            self.globalDB.execute_sql(
                "UPDATE tb_ui_mobile_server SET STATUS = 1 WHERE id = %s" %
                mobileServer[0]["id"])
            self.globalDB.execute_sql(
                "UPDATE tb_ui_test_execute SET serverId = %s WHERE id = %s" %
                (mobileServer[0]["id"], self.taskExecuteId))
            return mobileServer[0]
        else:
            return False

    def resetAppiumServer(self):
        self.globalDB.connect()
        if self.serverId != 0:
            self.globalDB.execute_sql(
                "update tb_ui_mobile_server set status=0 where id=%d" %
                (int(self.serverId)))

    def execute(self):
        if self.taskExecuteId == -1:
            self.execStatus = 4
            self.setExecStatus()
            return

        self.execStatus = 2
        if self.reportDir.strip() == "":
            self.reportDir = "%s_%s" % (self.taskExecuteId,
                                        get_current_time_YYYYMMDD_HHMMSS())
        self.execStartTime = get_current_time()
        self.testResult = ResultConst.NOTRUN
        self.saveToDB()
        taskExecStartTimestamp = time.time()
        reportBaseDir = "%s/static/ui_test/reports/%s/" % (
            EnvConfig.WEB_ROOT, self.reportDir)  # 必须有/结尾
        excelBaseDir = "%s/ui_file_uploads/%s" % (EnvConfig.WEB_ROOT,
                                                  self.fileAddBy)

        appParamString = ""
        if self.packageId != "":
            #设置定制的appParamString
            pacInfo = self.getPackageInfo()
            if pacInfo:
                if pacInfo['packageType'] == 1:
                    #android
                    appUrl = "%s/static/ui_test/app_uploads/%s/%s" % (
                        EnvConfig.WEB_URI, pacInfo['addBy'],
                        pacInfo['appFileName'])
                    appParamString = "--app=%s--appPackage=%s--appActivity=%s" % (
                        appUrl, pacInfo['appPackage'], pacInfo['appActivity'])
                elif pacInfo['packageType'] == 2:
                    # iPhone
                    appUrl = "%s/static/ui_test/app_uploads/%s/%s" % (
                        EnvConfig.WEB_URI, pacInfo['addBy'],
                        pacInfo['appFileName'])
                    appParamString = "--app=%s--bundleId=%s" % (
                        appUrl, pacInfo['bundleId'])
                elif pacInfo['packageType'] == 3:
                    # iPad
                    appUrl = "%s/static/ui_test/app_uploads/%s/%s" % (
                        EnvConfig.WEB_URI, pacInfo['addBy'],
                        pacInfo['appFileName'])
                    appParamString = "--app=%s--bundleId=%s" % (
                        appUrl, pacInfo['bundleId'])

        execAnfReportParamInfoStr = "--filepath=%s/%s--sheetName=%s--httpConfKey=%s--reportFolder=%s" % (
            excelBaseDir, self.fileName, self.sheetName, self.httpConfKey,
            reportBaseDir)

        dbParamInfoStr = "--dbMode=true--dbHost=%s--dbPort=%s--dbUsername=%s--dbPassword=%s--dbName=%s--dbTaskExecuteId=%d" % (
            DBConf.dbHost, DBConf.dbPort, DBConf.dbUsername, DBConf.dbPassword,
            DBConf.dbName, int(self.taskExecuteId))
        driverConfigConfStr = "--driverConfigPath=%s/RobotUiTest/DriverConfig.conf" % EnvConfig.PLATFORM_ROOT
        #如果不是app
        if self.sourceGroup == webTypeKey:
            logging.debug("web任务!")
            if isReleaseEnv:
                driverConfigConfStr = "%s--ip=%s--port=%s" % (
                    driverConfigConfStr, driverConfDict["hub"]["ip"],
                    driverConfDict["hub"]["port"])
            logging.debug("driverConfigConfStr %s" % driverConfigConfStr)
        if self.sourceGroup == androidTypeKey:
            logging.debug("android任务!")
            androidServer = self.getDevice("Android")
            if androidServer:
                logging.debug("发现Android设备 %s " % androidServer["id"])
                self.bindTaskMobileServer(self.taskExecuteId,
                                          androidServer["id"])
                appParamString = "%s--ip=%s--port=%s--udid=%s--deviceName=%s" % (
                    appParamString, androidServer["serverIp"],
                    androidServer["serverPort"], androidServer["udid"],
                    androidServer["deviceName"])
            else:
                #这就是没有获取到模拟器
                logging.error("没有发现Android设备 ")
                self.testResultMessage = "没获取到模拟器"
                self.execStatus = 4
                self.setExecStatus()
                self.globalDB.release()
        if self.sourceGroup == iosTypeKey:
            logging.debug("ios任务!")
            iosServer = self.getDevice("IOS")
            if iosServer:
                logging.debug("发现ios设备 %s " % iosServer["id"])
                self.bindTaskMobileServer(self.taskExecuteId, iosServer["id"])
                appParamString = "%s--ip=%s--port=%s--udid=%s--deviceName=%s--wdaLocalPort=%s" % (
                    appParamString, iosServer["serverIp"],
                    iosServer["serverPort"], iosServer["udid"],
                    iosServer["deviceName"], iosServer["wdaLocalPort"])
            else:
                #这就是没有获取到模拟器
                logging.error("没获取到ios设备 ")
                self.testResultMessage = "没获取到模拟器"
                self.execStatus = 4
                self.setExecStatus()
                self.globalDB.release()
        # if self.sourceGroup == iosTypeKey:
        #     appParamString = "%s--ip=%s--port=%s--udid=%s" % (appParamString,"10.10.20.229","9966","127.0.0.1:21503")
        pythoneExecuteString = "python3 %s/test_run/runUITest.py --print=0%s%s%s%s" % (
            EnvConfig.UI_FRAMEWORK_ROOT, execAnfReportParamInfoStr,
            dbParamInfoStr, driverConfigConfStr, appParamString)

        print(pythoneExecuteString)
        if isLinuxSystem():
            order = "%s > /dev/null 2>&1 " % pythoneExecuteString
        else:
            order = "start /b %s " % pythoneExecuteString
        logging.debug(order)
        self.execCommand = "python3 runUITest.py %s%s" % (
            execAnfReportParamInfoStr, appParamString)  #去掉数据库等敏感信息
        self.saveToDB()
        try:
            os.popen(order)
        except Exception as e:
            logging.error("发生执行异常")
            self.globalDB.release()
        #开始循环判断执行状态,如果状态是结束,那么结束。a d
        try:
            self.globalDB.initGlobalDBConf()
            while True:
                res = self.globalDB.execute_sql(
                    "select * from tb_ui_test_execute where id=%d" %
                    (int(self.taskExecuteId)))
                if res:
                    resUITaskDict = res[0]
                    self.execStatus = resUITaskDict['execStatus']
                    if self.execStatus == 2:
                        #如果在执行中,等待一会。
                        time.sleep(1)
                        continue
                    else:
                        #如果不是执行中,那就是框架反写到数据库中执行状态执行结束了。
                        self.testResult = resUITaskDict['testResult']
                        self.testResultMessage = resUITaskDict[
                            'testResultMessage']
                        #判断如果使用了模拟器把模拟器设为可用
                        break
                else:
                    #如果没有找到对应的执行状态,报异常。
                    self.execStatus = 4
                    self.testResult = ResultConst.EXCEPTION
                    self.testResultMessage = "执行异常,没有找到任务执行数据!"
                    self.saveToDB()
        finally:
            self.execEndTime = get_current_time()
            taskExecEndTimestamp = time.time()
            self.execTakeTime = str(taskExecEndTimestamp -
                                    taskExecStartTimestamp)
            self.saveExecEndTimeAndTaskTakeTimeToDB()
            if self.isSendEmail > 0:
                self.sendEmailToExecutor()
            if self.serverId != 0:
                self.resetAppiumServer()
            self.globalDB.release()

    def setExecStatus(self):
        self.globalDB.initGlobalDBConf()
        self.taskExecuteId = int(self.taskExecuteId)
        sqlTasExec = "update tb_ui_test_execute set execStatus=%d where id=%d" % (
            self.execStatus, int(self.taskExecuteId))
        resTaskExec = self.globalDB.execute_update_sql(sqlTasExec)
        self.globalDB.release()

    def saveReportDirToDB(self):
        self.globalDB.initGlobalDBConf()
        self.taskExecuteId = int(self.taskExecuteId)
        sqlTasExec = "update tb_ui_test_execute set reportDir='%s' where id=%d" % (
            self.reportDir, int(self.taskExecuteId))
        resTaskExec = self.globalDB.execute_update_sql(sqlTasExec)
        self.globalDB.release()

    def saveProgressToDB(self):
        self.globalDB.initGlobalDBConf()
        self.taskExecuteId = int(self.taskExecuteId)
        sqlTasExec = "update tb_ui_test_execute set execProgressString='%s' where id=%d" % (
            self.execProgressString, int(self.taskExecuteId))
        resTaskExec = self.globalDB.execute_update_sql(sqlTasExec)
        self.globalDB.release()

    def saveExecEndTimeAndTaskTakeTimeToDB(self):
        self.globalDB.initGlobalDBConf()
        self.taskExecuteId = int(self.taskExecuteId)
        saveAttrString = "execStatus=%d," % self.execStatus
        if self.execEndTime != "":
            saveAttrString += "execEndTime = '%s'," % self.execEndTime
        if self.execTakeTime != "":
            saveAttrString += "execTakeTime = '%s'," % self.execTakeTime
        try:
            if saveAttrString != "":
                saveAttrString = saveAttrString[:-1]
                sqlTasExec = "update tb_ui_test_execute set %s where id=%d" % (
                    saveAttrString, int(self.taskExecuteId))
                res = self.globalDB.execute_update_sql(sqlTasExec)
                return res
            else:
                return False
        finally:
            self.globalDB.release()

    def cancelTask(self):
        try:
            logging.debug("当前执行状态:%s" % self.execStatus)
            if self.execStatus == 2 or self.execStatus == 10:
                if self.serverId != 0:
                    self.resetAppiumServer()
                reportBaseDir = "%s/static/ui_test/reports/%s/" % (
                    EnvConfig.WEB_ROOT, self.reportDir)  # 必须有/结尾
                reportFileRealPath = "%s%s" % (reportBaseDir,
                                               UITestTask.reportFileName)
                pickleFileRealPath = "%s%s" % (reportBaseDir,
                                               UITestTask.pickleFileName)

                #STEP1 杀进程
                if isLinuxSystem():
                    #linux下杀进程
                    getPidCommand = "ps -ef |grep %s |grep -v grep |grep -v /dev/null" % reportBaseDir
                    logging.debug(getPidCommand)
                    pidPopenRes = os.popen(getPidCommand)
                    pidOutput = pidPopenRes.read().strip()
                    logging.debug(pidOutput)
                    retResList = pidOutput.split(" ")
                    logging.debug(retResList)
                    pidTobeKilled = 0
                    for tmpPid in retResList:
                        if isInt(tmpPid.strip()):
                            pidTobeKilled = int(tmpPid.strip())
                            break
                    logging.debug("PidTobeKilledInLinux:%d" % pidTobeKilled)
                    if pidTobeKilled > 0:
                        killRes = os.popen("kill -9 %d" % pidTobeKilled)
                        killOutputStr = killRes.read()
                        logging.info("进程[%d]killed!%s" %
                                     (pidTobeKilled, killOutputStr))
                else:
                    #windows下杀进程
                    getPidCommand = """wmic process where caption="python3.exe" get handle,caption,commandline /value"""
                    pidRes = os.popen(getPidCommand)
                    outPidTaskString = pidRes.read()
                    handleStringList = outPidTaskString.split("Handle=")
                    for i in range(0, len(handleStringList)):
                        if reportBaseDir in handleStringList[i]:
                            realWindowsPid = get_sub_string(
                                handleStringList[i + 1], "", "\n").strip()
                            if isInt(realWindowsPid):
                                killRes = os.popen("taskkill /pid %s -f" %
                                                   realWindowsPid)
                                kilResOutput = killRes.read()
                                logging.info("进程[%s]killed!%s" %
                                             (realWindowsPid, kilResOutput))
                # step3 更新状态先更新状态再ui出driver,因为退出driver比较慢。
                self.execStatus = 11
                self.testResult = "CANCEL"
                self.saveToDB()
                #STEP2退出未推出的driver
                try:
                    # 反序列化退出所有有效的driver
                    if os.path.exists(pickleFileRealPath) and os.path.isfile(
                            pickleFileRealPath):
                        logging.info("任务【%s】取消,发现pickle文件!" %
                                     self.taskExecuteId)
                        # 先判断有没有pkl文件
                        #存在pkl,进行退出
                        with open(pickleFileRealPath, 'rb') as f:
                            aa = pickle.load(f)
                        for tmpDriver in aa:
                            try:
                                tmpDriver.quit()
                                logging.info("WebDriver quited!")
                            except Exception as e:
                                logging.info(
                                    "WebDriver unquited! Exception occured when quit!"
                                )
                                logging.debug(traceback.format_exc())

                        os.remove(pickleFileRealPath)
                    else:
                        logging.info("任务【%s】取消,没有发现pickle文件!" %
                                     self.taskExecuteId)
                except Exception as e:
                    logging.error(str(e))

            return True
        except Exception as e:
            return False
        finally:
            self.globalDB.release()

    def saveToDB(self):
        self.globalDB.initGlobalDBConf()
        self.taskExecuteId = int(self.taskExecuteId)
        saveAttrString = "execStatus=%d," % self.execStatus
        if self.reportDir != "":
            saveAttrString += "reportDir = '%s'," % self.reportDir
        if self.execStartTime != "":
            saveAttrString += "execStartTime = '%s'," % self.execStartTime
        if self.execEndTime != "":
            saveAttrString += "execEndTime = '%s'," % self.execEndTime
        if self.execTakeTime != "":
            saveAttrString += "execTakeTime = '%s'," % self.execTakeTime
        if self.execProgressString != "":
            saveAttrString += "execProgressString = '%s'," % self.execProgressString
        if self.testResult != "":
            saveAttrString += "testResult = '%s'," % self.testResult
        if self.testResultMessage != "":
            saveAttrString += "testResultMessage = '%s'," % self.testResultMessage
        if self.execCommand != "":
            saveAttrString += "execCommand = '%s'," % self.execCommand
        try:
            if saveAttrString != "":
                saveAttrString = saveAttrString[:-1]
                sqlTasExec = "update tb_ui_test_execute set %s where id=%d" % (
                    saveAttrString, int(self.taskExecuteId))
                res = self.globalDB.execute_update_sql(sqlTasExec)
                return res
            else:
                return False
        finally:
            self.globalDB.release()

    def sendEmailToExecutor(self):
        self.globalDB.initGlobalDBConf()
        res = self.globalDB.execute_sql(
            "select email from tb_user where loginName='%s' " % self.addBy)
        self.globalDB.release()
        resEmailList = self.emailList
        print("self.emailList", self.emailList)
        if res:
            # emailList = "%s" % res[0]['email']  # ;分号间隔
            emailList = ""  # ;分号间隔

            if resEmailList.strip() != "":
                resEmList = resEmailList.split(",")
                for tmpMail in resEmList:
                    emailList = emailList + ";" + tmpMail
                # emailList = emailList + ";[email protected]"

            subject = "[%s]UI任务[%s:%s]测试报告" % (self.testResult, self.taskId,
                                               self.title)

            # 开始生成emailText的html###############################################################################
            """
(一)测试报告概况:
任务ID:HTTP_TASK_62
执行环境:预发布灰度
执行结果:FAIL
执行耗时:36秒
任务名称:SFA-CPQ团队-自动化测试【staging灰度环境】
任务描述:(1)【产品】PC端和APP端 (2)【价格表】&【价格表明细】PC端和APP端(3)【合同】PC端和APP端
包含ywx:['SFA']
包含mk:['产品', '合同', '价格表']
备注:在jenkins上执行自动化测试任务

所有统计:

(1)用例统计:用例总数10个,通过7,失败3,通过率70%。【说明:因为任务中的用例都会被执行,通过率 = 通过数量 / 总数量】
(2)接口统计:接口总数500个,通过400个,失败50个,未执行50个,通过率80%。【说明:接口总数包含独立接口和用例接口之和。因为用例的靠前的接口可能失败,靠后的接口不会被执行,通过率定义方式大家可以多提建议。通过率 =  通过数量 / 总数量】
(二)失败的所有接口汇总:【说明:包含独立接口和用例接口】
(1)mk:【产品】
接口[/json/crm_product/save.action]:已执行4次,通过0,失败4,通过率0.00%。
(2)mk:【合同】
接口[/mobile/common/search.action]:已执行2次,通过1,失败1,通过率50.00%。
接口[/json/crm_common_detail/lock-data.action]:已执行1次,通过0,失败1,通过率0.00%。
 详情见附件。
            """
            bgColor = "#f2dede"
            if self.testResult == "PASS":
                bgColor = "#dff0d8"
            emailText = "<html><body style='background-color:%s;display:block;'>" % bgColor

            # 1、测试报告概况
            emailText = emailText + (
                """
            <h1>(一)UI测试报告概况</h1>
            <h2>任务基本信息</h2>
            <table border="1px" cellpadding="3px" width="80%%">
            <tr><th width="20%%"  align="left">任务ID</th><td width="80%%">%s</td></tr>
            <tr><th align="left">执行环境</th><td>%s</td></tr>
            <tr><th align="left">执行时间</th><td>%s</td></tr>
            <tr><th align="left">执行结果</th><td>%s</td></tr>
            <tr><th align="left">执行耗时</th><td>%s秒</td></tr>
            <tr><th align="left">任务名称</th><td>%s</td></tr>
            <tr><th align="left">任务描述</th><td>%s</td></tr>
            <tr><th align="left">包含%s</th><td>%s</td></tr>
            <tr><th align="left">包含%s</th><td>%s</td></tr>
            <tr><th align="left">备注</th><td>%s</td></tr>
            </table>""" %
                (self.taskId, self.httpConfKey, self.execStartTime,
                 self.testResult, self.execTakeTime, self.title, self.taskdesc,
                 CommonConf.groupLevel1, self.businessLineName,
                 CommonConf.groupLevel2, self.moduleName,
                 (self.execComments.strip() == "" and "无"
                  or self.execComments.strip())))
            if isDictJson(self.testResultMessage):
                resDict = json.loads(self.testResultMessage)
                if resDict['caseTotalCount'] == 0:
                    caseRate = 0.00
                else:
                    caseRate = float(resDict['casePassCount']) / float(
                        resDict['caseTotalCount'])

                if resDict['caseStepTotalCount'] == 0:
                    stepRate = 0.00
                else:
                    stepRate = float(resDict['caseStepPassCount']) / float(
                        resDict['caseStepTotalCount'])

                # 所有统计信息开始################################################################
                emailText = emailText + "<h2>结果统计信息</h2>"
                emailText = emailText + """
    <table border="1px" cellpadding="3px" width="80%%">  
        <thead>
            <tr style="background-color:#fcf8e3;"> 
                <th width="5%%"></th>
                <th width="10%%">总计</th>
                <th width="10%%">PASS</th>
                <th width="10%%">FAIL</th>
                <th width="10%%">WARNING</th>
                <th width="10%%">通过率</th>
            </tr>
        </thead>
        <tbody>
            <tr style="background-color:%s;" id="total_summay"><td>用例</td>
                <td><bdi id="summay_totalCount">%s</bdi></td>
                <td><bdi id="summay_passCount">%s</bdi>
                <td><bdi id="summay_failCount">%s</bdi></td>
                <td><bdi id="summay_failCount">%s</bdi></td>
                <td><bdi id="summay_passPercent">%.2f%%</bdi></td></tr>
            <tr style="background-color:%s;" id="total_summay"><td>步骤</td> 
                <td><bdi id="summay_totalCount">%s</bdi></td>
                <td><bdi id="summay_passCount">%s</bdi></td>
                <td><bdi id="summay_failCount">%s</bdi></td>
                <td><bdi id="summay_failCount">%s</bdi></td>
                <td><bdi id="summay_passPercent">%.2f%%</bdi></td></tr>
        </tbody>
    </table>""" % (bgColor, resDict['caseTotalCount'],
                   resDict['casePassCount'], resDict['caseFailCount'],
                   resDict['caseWarningCount'], caseRate * 100, bgColor,
                   resDict['caseStepTotalCount'], resDict['caseStepPassCount'],
                   resDict['caseStepFailCount'],
                   resDict['caseStepWarningCount'], stepRate * 100)

            else:
                # task 执行结果 结果没有返回正常的json
                emailText = emailText + self.testResultMessage + "<br>"

            # self.isSendEmail  是否发送是否带附件PassFailErrorException
            isSendMailStr = str(self.isSendEmail)
            # 不够6位后面补1
            addOneCount = 6 - len(isSendMailStr)
            for i in range(0, addOneCount):
                isSendMailStr = isSendMailStr + "1"
            if int(isSendMailStr[1]) == 1:
                emailText = emailText + "<br>详情见附件。"
            else:
                tmpurl = "%s/static/ui_test/reports/%s/report.html" % (
                    EnvConfig.WEB_URI, self.reportDir)
                emailText = emailText + (
                    "<br>详情见:&nbsp;&nbsp;<a href='%s'>%s</a>" %
                    (tmpurl, tmpurl))

            emailText = emailText + "</body></html>"

            # 结束生成emailText的html###############################################################################
            # 开始发送邮件
            def subSendMail():
                if int(isSendMailStr[1]) == 1:
                    # 发送附件
                    testReportFilePath = "%s/static/ui_test/reports/%s/report.html" % (
                        EnvConfig.WEB_ROOT, self.reportDir)
                    UsualTool.send_mail(emailList,
                                        subject,
                                        emailText,
                                        testReportFilePath,
                                        subType="html")
                else:
                    # 不发送附件
                    UsualTool.send_mail(emailList,
                                        subject,
                                        emailText,
                                        subType="html")

            if self.testResult == ResultConst.PASS and int(
                    isSendMailStr[2]) == 1:
                subSendMail()
            elif self.testResult == ResultConst.FAIL and int(
                    isSendMailStr[3]) == 1:
                subSendMail()
            elif self.testResult == ResultConst.ERROR and int(
                    isSendMailStr[4]) == 1:
                subSendMail()
            elif self.testResult == ResultConst.EXCEPTION and int(
                    isSendMailStr[5]) == 1:
                subSendMail()
        else:
            emailList = EmailConfig.sender  # ;分号间隔
            subject = "[%s]任务[%s:%s]测试报告,没有找到收件人,执行者为%s" % (
                self.testResult, self.taskId, self.title, self.addBy)
            emailText = "[%s]任务[%s:%s]测试报告,没有找到收件人,执行者为%s" % (
                self.testResult, self.taskId, self.title, self.addBy)
            UsualTool.send_mail(emailList, subject, emailText, subType="html")
Exemple #5
0
class CommonAttr(object):
    def __init__(self):
        self.interfaceId = ""  # 接口用例就是直接显示id,case步骤是显示 caseId-stepNum
        self.version = ""
        self.protocol = ""
        self.objType = ObjTypeConst.UNKNOWN

        self.traceId = ""

        self.globalDB = DBTool()
        self.serviceDB = DBTool()
        self.serviceDBDict = {}
        self.serviceRedis = RedisTool()
        self.serviceRedisDict = {}
        self.serviceMongoDBDict = {}

        self.highPriorityVARSStr = ""  # 通过Str生成dict
        self.highPriorityVARSDict = {
        }  # 优先变量,当存在优先变量时,GVAR和VAR先调用优先变量,优先级最高,当调用$GVAR[key]或者$VAR[key]时,先看着里面是否有,如果存在将忽略db中的GVAR和变量列表中的变量。
        self.highPriorityVARSFinalStr = ""

        self.httpConfKey = ""
        self.confHttpLayer = ConfHttpLayer()

        self.varsPool = {}  # 里面有 varkey PathVarkey 以及python模式相关的变量,没有声明路径的
        self.calledVarsKeyList = []  # {"key":"来源"}  这个只在本接口或者步骤内生效。

        self.current_session = requests.session()
        self.kw_LOGIN_ParamList = []
        self.context_data_list = []
        self.context_data_dict = {}

        self.version = "CurrentVersion"
        self.caseLevel = testLevelConst.RUNALL

        self.response = requests.models.Response(
        )  # 最近HTTP请求返回的response对象,提供给各个方法使用
        self.interface_response = requests.models.Response(
        )  # 要测试的接口返回的response,供断言使用。
        self.dubboResponseString = ""  #返回的结果,也就是最近执行的dubbo接口返回的结果

        self.testResult = ResultConst.NOTRUN
        self.actualResult = ""
        self.assertResult = ""
        self.exitExecStatusList = [
            ResultConst.FAIL, ResultConst.EXCEPTION, ResultConst.ERROR
        ]
        self.isRequested = False

        self.pythonModeStrPrefix = "python"
        self.pythonModeExecuteLog = ""
        self.pythonLogVarsPre = ""
        self.pythonLogVarsPost = ""
        self.noneTag = "@NONE@"
        self.validResultList = [
            ResultConst.ERROR, ResultConst.PASS, ResultConst.FAIL,
            ResultConst.NOTRUN, ResultConst.EXCEPTION, ResultConst.NO_ASSERT
        ]

        self.varCallCountDict = {
            "GVAR": {},
            "VAR": {},
            "TEXT": {},
            "imports": {}
        }  #设置GVAR 和 VAR
        self.calledInterfaceRecurDict = {}

        self.isPythonModePre = False
        self.isPythonModePost = False

        self.retryList = []

    def generateResultWhenNotRequsted(self):
        if self.isRequested == False:
            if self.protocol == "HTTP":
                self.interface_response = self.assertResult
                self.response = self.interface_response
            elif self.protocol == "DUBBO":
                self.dubboResponseString = self.assertResult

    @catch_exception
    def initDB(self):
        """
        执行前初始化的一些数据库信息
        Returns:
            无。
        """
        # 初始化serviceDB
        if "DB" in self.confHttpLayer.confServiceLayer.confDict.keys():
            logging.debug("○○21★1===traceId[%s]★★★★★" % self.traceId)
            serviceDBDict = self.confHttpLayer.confServiceLayer.confDict['DB']
            # 加载被测各个单点系统的数据库
            for tmpDbKey in serviceDBDict.keys():
                logging.debug("○○21★★2===traceId[%s] %s★★★★★" %
                              (self.traceId, serviceDBDict[tmpDbKey]))
                logging.debug("○○21★★★3===traceId[%s] tmpDbKey[%s]★★★★★" %
                              (self.traceId, tmpDbKey))
                tmpHost = serviceDBDict[tmpDbKey]["host"]
                logging.debug("○○21★★★★4===traceId[%s] tmpDbKey[%s]★★★★★" %
                              (self.traceId, tmpDbKey))
                if tmpHost != "0.0.0.0" and tmpHost != "":
                    tmpPort = int(serviceDBDict[tmpDbKey]["port"])
                    tmpUsername = serviceDBDict[tmpDbKey]["username"]
                    tmpPassword = serviceDBDict[tmpDbKey]["password"]
                    tmpServiceDB = DBTool(host=tmpHost,
                                          port=tmpPort,
                                          username=tmpUsername,
                                          password=tmpPassword,
                                          isDictCursor=False)
                    self.serviceDBDict[tmpDbKey] = tmpServiceDB

    @catch_exception
    def initRedis(self):
        """
        执行前初始化的一些数据库信息
        Returns:
            无。
        """
        # 初始化平台db
        # 初始化serviceDB
        if "REDIS" in self.confHttpLayer.confServiceLayer.confDict.keys():
            serviceDBDict = self.confHttpLayer.confServiceLayer.confDict[
                'REDIS']
            # 加载被测各个单点系统的数据库
            for tmpDbKey in serviceDBDict.keys():
                tmpHost = serviceDBDict[tmpDbKey]["host"]
                if tmpHost != "0.0.0.0" and tmpHost != "":
                    tmpPort = int(serviceDBDict[tmpDbKey]["port"])
                    tmpPassword = serviceDBDict[tmpDbKey][
                        "password"] if "password" in serviceDBDict[
                            tmpDbKey].keys() else ""
                    tmpServiceDB = RedisTool(host=tmpHost,
                                             port=tmpPort,
                                             password=tmpPassword)
                    self.serviceRedisDict[tmpDbKey] = tmpServiceDB

    @catch_exception
    def initMongoDB(self):
        """
        执行前初始化的一些mongodb信息
        Returns:
            无。
        """
        # 初始化平台db
        # 初始化serviceDB

        if "MONGO" in self.confHttpLayer.confServiceLayer.confDict.keys():
            logging.debug("○○23★1 mongo===traceId[%s]★★★★★" % self.traceId)

            serviceDBDict = self.confHttpLayer.confServiceLayer.confDict[
                'MONGO']
            logging.debug("○○23★★2 mongo===traceId[%s]★★★★★" % self.traceId)

            # 加载被测各个单点系统的数据库
            for tmpDbKey in serviceDBDict.keys():
                logging.debug("○○23★★★3 mongo===traceId[%s] %s ★★★★★" %
                              (self.traceId, serviceDBDict[tmpDbKey]))
                if serviceDBDict[tmpDbKey][
                        'host'] != "0.0.0.0" and serviceDBDict[tmpDbKey][
                            'host'] != "":
                    self.serviceMongoDBDict[tmpDbKey] = MongoTool(
                        host=serviceDBDict[tmpDbKey]['host'],
                        port=int(serviceDBDict[tmpDbKey]['port']),
                        username=serviceDBDict[tmpDbKey]['username'] if
                        "username" in serviceDBDict[tmpDbKey].keys() else None,
                        password=serviceDBDict[tmpDbKey]['password'] if
                        "password" in serviceDBDict[tmpDbKey].keys() else None)

    @catch_exception
    def releaseDB(self):
        """
        执行完释放数据库连接
        Returns:
            无。
        """
        self.globalDB.release()
        for tmpServiceDbKey, tmpServiceDbValue in self.serviceDBDict.items():
            tmpServiceDbValue.release()

    @take_time
    @catch_exception
    def setResult(self, result=ResultConst.ERROR, errorMsg=""):
        """
        设置测试结果
        Args:
            result: 测试结果    PASS FAIL等
            errorMsg: 测试消息。

        Returns:
            无。
        """
        if result not in self.validResultList:
            self.testResult = ResultConst.ERROR
            self.assertResult = "%s: %s" % (ResultConst.ERROR,
                                            "不是合法的测试结果[%s]" % result)
            self.actualResult = "不是合法的测试结果[%s]" % result
            return

        self.assertResult = "%s: %s\n%s\n" % (result, errorMsg,
                                              self.assertResult)
        if TypeTool.is_str(self.actualResult) and self.actualResult == "":
            self.actualResult = errorMsg
        self.testResult = result

    def setPASS(self, errorMsg=""):
        self.setResult(ResultConst.PASS, errorMsg)

    def setFAIL(self, errorMsg=""):
        self.setResult(ResultConst.FAIL, errorMsg)

    def setERROR(self, errorMsg=""):
        self.setResult(ResultConst.ERROR, errorMsg)

    def setEXCEPTION(self, errorMsg=""):
        self.setResult(ResultConst.EXCEPTION, errorMsg)

    def updateCalledVarkeyList(self, updateKey):
        if updateKey in self.calledVarsKeyList:
            self.calledVarsKeyList.remove(updateKey)
        self.calledVarsKeyList.append(updateKey)

    def checkAllInfosAfterTest(self):
        self.testResult = (self.testResult == None or self.testResult
                           == "") and "没有测试结果" or self.testResult
        self.actualResult = (self.actualResult == None or self.actualResult
                             == "") and "没有实际返回结果" or self.actualResult
        self.assertResult = (self.assertResult == None or self.assertResult
                             == "") and "没有断言结果" or self.assertResult

        self.varsPre = (self.varsPre == None or self.varsPre.strip()
                        == "") and "无准备数据" or self.varsPre
        self.varsPost = (self.varsPost == None or self.varsPost.strip()
                         == "") and "无断言恢复数据" or self.varsPost

    @catch_exception
    def processData(self, dataString):
        """
        处理初始化和恢复的方法。
        Args:
            dataString: 要处理的字符串,比如
                        LOGIN();
                        REDIS();

        Returns:
            处理后的
        """
        dataStringList = splitStringToListByTag(dataString, ";")
        retString = ""
        for i in range(0, len(dataStringList)):
            if dataStringList[i].strip() == "":
                continue
            if dataStringList[i].strip()[0:1] == "#":
                retString += dataStringList[i].strip() + ";\n"
                continue
            tmpValue = dataStringList[i]
            tmpValue = BP.process_branch_IF(tmpValue, self)
            dataStringList[i] = self.processKPWithNoneTag(
                tmpValue)  # 变量替换,处理keyword
            retString += dataStringList[i].strip() + ";\n"
            if self.testResult in self.exitExecStatusList:
                if self.isRequested == False:
                    self.generateResultWhenNotRequsted()
                return retString[:-1]
        return retString[:-1]

    def setFinalAssertMsg(self):
        """
        设置最终断言结果。
        Returns:
            无
        """
        if self.testResult == ResultConst.PASS:
            self.assertResult = "[%s]: %s\n\n%s" % (
                ResultConst.PASS, "全部断言通过!", self.assertResult.strip())
        elif self.testResult == ResultConst.FAIL:
            self.assertResult = "[%s]: %s\n\n%s" % (ResultConst.FAIL, "断言失败!",
                                                    self.assertResult.strip())
        elif self.testResult == ResultConst.ERROR:
            self.assertResult = "[%s]: %s\n\n%s" % (
                ResultConst.ERROR, "出现ERROR!", self.assertResult.strip())
        elif self.testResult == ResultConst.NOTRUN:
            self.testResult = ResultConst.ERROR
            self.assertResult = "[%s]: %s\n\n%s" % (ResultConst.ERROR,
                                                    "执行结果是NOTRUN,请反馈给管理员!",
                                                    self.assertResult.strip())
        elif self.testResult == ResultConst.NO_ASSERT:
            self.testResult = ResultConst.ERROR
            self.assertResult = "[%s]: %s\n\n%s" % (
                ResultConst.ERROR, "执行结果是NO_ASSERT,没有进行断言,请加入断言!",
                self.assertResult.strip())
        else:
            self.assertResult = "[%s]: %s\n\n%s" % (
                ResultConst.EXCEPTION, "结果【%s】异常,请反馈给管理员!" % self.testResult,
                self.assertResult.strip())
            self.testResult = ResultConst.EXCEPTION

    ###变量 string json dict之间的转换函数
    @catch_exception
    def getVarsKeyListByVarsList(self, varsList):
        """
        通过varsList得到keylist
        Args:
            varsList: 处理过后的varslist,例如
                    ["k1=v1","k2=v2","k3=v3"]

        Returns:
            keyList,例如 ["k1","k2","k3"]
        """
        varsKeyList = []
        varsValueList = []
        for i in range(0, len(varsList)):
            tmpVarKeyValue = varsList[i].strip()
            if tmpVarKeyValue[0:1] == "#":
                #判断属于注释
                tmpkey = "[ANNOTATION]"
                tmpValue = tmpVarKeyValue
                varsKeyList.append(tmpkey.strip())
                varsValueList.append(tmpValue.strip())
            else:
                # 判断是否是关键字开头,如果是关键字开头,进入EXECUTE而不是变量拆分
                isExecuteKeywork = False
                for tmpKeyworkMethodName in kwKeyList:
                    if tmpVarKeyValue.startswith(tmpKeyworkMethodName + "("):
                        # 属于没有key的,只有执行作用,类似于执行前准备。
                        tmpkey = "[EXECUTE]"
                        tmpValue = tmpVarKeyValue
                        if tmpValue.strip() != "":
                            varsKeyList.append(tmpkey.strip())
                            varsValueList.append(tmpValue.strip())
                        isExecuteKeywork = True
                        break
                if isExecuteKeywork == False:
                    #如果内置关键字没有匹配到,看看自定义关键字有没有匹配。
                    selfKwList, selfKwDict = SelfKeywordProcesser.getSelfKeywordDict(
                    )
                    for tmpKeyworkMethodName in selfKwList:
                        if tmpVarKeyValue.startswith(tmpKeyworkMethodName +
                                                     "("):
                            # 属于没有key的,只有执行作用,类似于执行前准备。
                            tmpkey = "[EXECUTE]"
                            tmpValue = tmpVarKeyValue
                            if tmpValue.strip() != "":
                                varsKeyList.append(tmpkey.strip())
                                varsValueList.append(tmpValue.strip())
                            isExecuteKeywork = True
                            break

                # 不是所有类型关键字开头的,然后切割进行判断。
                if isExecuteKeywork == False:
                    tmpVarList = tmpVarKeyValue.split("=")
                    if len(tmpVarList) >= 2:
                        # 合法的key value并且不是#开头的注释语句,加入dict
                        tmpkey = tmpVarList[0].strip()
                        tmpValue = tmpVarList[1].strip()
                        for j in range(2, len(tmpVarList)):
                            tmpValue = tmpValue + "=" + tmpVarList[j]
                        # 结束key 和 value赋值
                        #判断key是否合法。
                        if not VerifyTool.IsVarMatch(tmpkey):
                            self.setERROR(
                                "变量key[%s]不合法,只允许大小写字母数字加-_,且只能字母开头!" % tmpkey)
                            return varsKeyList, varsValueList
                        varsKeyList.append(tmpkey.strip())
                        varsValueList.append(tmpValue.strip())
                    elif len(tmpVarList) == 1:
                        # 属于没有key的,只有执行作用,类似于执行前准备。
                        tmpkey = "[EXECUTE]"
                        tmpValue = tmpVarList[0].strip()
                        if tmpValue.strip() != "":
                            varsKeyList.append(tmpkey.strip())
                            varsValueList.append(tmpValue.strip())

        return varsKeyList, varsValueList

    @catch_exception
    def getVarsKeyListByVarsString(self, varsString):
        """
        得到通过varsString得到varkeyList
        Args:
            varsString: 比如
                    k1=v1;
                    k2=v2;
                    k3=v3;

        Returns:
            varskeyList,例如 ["k1","k2","k3"]
        """
        varsList = splitStringToListByTag(varsString, ";")
        return self.getVarsKeyListByVarsList(varsList)

    @catch_exception
    def processKPWithNoneTag(self, tmpValue):
        return KP.getProcessedValue(tmpValue, self).replace(self.noneTag, "")

    @catch_exception
    def get_processed_value(self, tmpValue):
        return self.processKPWithNoneTag(tmpValue)

    @catch_exception
    def generateVarsKeyListAndVarsPool(self, varsPreorPost):
        """
        生成变量列表和变量池 varsPool
        Args:
            varsPreorPost: 最初始的未经过任何处理的varsPreorPost,比如varsPre和varsPost。格式如下:
                        [CONF=conmmon]
                        k1=v1;
                        [ENDCONF]
                        [CONF=testenv1]
                        k2=v2;
                        [ENDCONF]

        Returns:
            生成和varsPool。
        """
        # # 从 [CONF=ServiceKey][ENDCONF]中截取对应的变量 如果没有截取到直接返回varsString
        varsString = varsPreorPost.strip()
        varsKeyList, varsValueList = self.getVarsKeyListByVarsString(
            varsString)
        for i in range(0, len(varsKeyList)):
            tmpKey = varsKeyList[i]
            tmpValue = varsValueList[i]
            # DONE 处理tmpValue,可以使用oldVarsDict中的任意变量,自己输入的;要带转义符
            if tmpKey == "[ANNOTATION]":
                # 是注释,不需要做任何操作。
                # 开始变量处理
                baseTmpKey = "[ANNOTATION]"
                while True:
                    # 循环处理,生成唯一的key
                    tmpKey = baseTmpKey + str(random.randint(
                        1, 9999999)) + genereateAnEnStr(8)  # 产生一个随机数key
                    if tmpKey in self.varsPool.keys():
                        # 如果存在,重新生成
                        tmpKey = baseTmpKey + str(random.randint(
                            1, 9999999)) + genereateAnEnStr(8)  # 产生一个随机数key
                    else:
                        # 不存在退出
                        break
                # 给全局变量池赋值
                self.varsPool[tmpKey] = tmpValue
                self.varsPool[self.getVarkeyPathKey(
                    tmpKey)] = "[%s]" % (self.interfaceId)
                self.updateCalledVarkeyList(tmpKey)
            elif tmpKey == "[EXECUTE]":
                # 只执行,不生成变量。
                pedValue = self.processKPWithNoneTag(tmpValue)
                baseTmpKey = "[EXECUTE]"
                while True:
                    # 循环处理,生成唯一的key
                    tmpKey = baseTmpKey + str(random.randint(
                        1, 9999999)) + genereateAnEnStr(8)  # 产生一个随机数key
                    if tmpKey in self.varsPool.keys():
                        # 如果存在,重新生成
                        tmpKey = baseTmpKey + str(random.randint(
                            1, 9999999)) + genereateAnEnStr(8)  # 产生一个随机数key
                    else:
                        # 不存在退出
                        break
                # 将处理完成的变量加入到oldVarsDict中
                self.varsPool[tmpKey] = "已执行:%s,执行结果:\n%s" % (
                    tmpValue, pedValue if len(pedValue) < 50 else
                    (pedValue[:48] + "..."))
                self.varsPool[self.getVarkeyPathKey(
                    tmpKey)] = "[%s]" % (self.interfaceId)
                self.updateCalledVarkeyList(tmpKey)
            else:
                # 正常变量
                self.setVar(tmpKey, self.processKPWithNoneTag(tmpValue))
                self.updateCalledVarkeyList(tmpKey)

            #如果出错了做什么,停止执行
            if self.testResult in self.exitExecStatusList or (
                    "IS_CONTINUE" in self.varsPool.keys()
                    and self.varsPool["IS_CONTINUE"] == False):
                if self.isRequested == False:
                    self.generateResultWhenNotRequsted()
                return

    @catch_exception
    def generateVarsStringByVarsPoolAndVarsKeyList(self):
        """
        根据varsPool生成当前的变量列表。
        Returns:
            当前变量列表
        """
        retStr = ""
        for tmpKey in self.calledVarsKeyList:
            if tmpKey.startswith("[ANNOTATION]") or tmpKey.startswith(
                    "[EXECUTE]"):
                #注释值显示后面部分不显示key:
                retStr += "%s ;(来源:%s)\n" % (
                    str(self.varsPool[tmpKey]).replace(";", "\\;"),
                    self.varsPool[self.getVarkeyPathKey(tmpKey)] if self.
                    getVarkeyPathKey(tmpKey) in self.varsPool.keys() else "无")
            else:
                #不是注释和执行类,正常变量的话
                try:
                    if TypeTool.is_list(
                            self.varsPool[tmpKey]) or TypeTool.is_dict(
                                self.varsPool[tmpKey]):
                        strToBerepalced = json.dumps(self.varsPool[tmpKey])
                    else:
                        strToBerepalced = str(self.varsPool[tmpKey])
                except:
                    strToBerepalced = str(self.varsPool[tmpKey])
                retStr += "%s = %s ;(来源:%s)\n" % (
                    tmpKey, strToBerepalced.replace(";", "\\;"),
                    self.varsPool[self.getVarkeyPathKey(tmpKey)] if self.
                    getVarkeyPathKey(tmpKey) in self.varsPool.keys() else "无")
        return retStr.strip()

    def initRequestHostAndResults(self):
        """
                执行前初始化的一些基本信息
                Returns:
                    无。
                """
        self.beforeExecuteTakeTime = 0
        self.afterExecuteTakeTime = 0
        self.executeTakeTime = 0
        self.totalTakeTime = 0
        self.testResult = ResultConst.NO_ASSERT
        self.actualResult = ""
        self.assertResult = ""

    def validate(self):
        pass

    def processVarsstring(self, varsstring):
        # 获取对应的String,判断是纯python模式还是普通模式
        # 纯python模式指的是执行python代码,python代码中可以调用一些函数来达到与平台互通的目的,使用更为高级
        # 普通模式是以平台语法为框架,可以调用python代码。
        varsPreEnvString = varsstring.strip()
        tagStr = varsPreEnvString.split("\n")[0].strip()
        if tagStr.startswith("#") and tagStr.replace("#", "").replace(
                ";", "").strip() == self.pythonModeStrPrefix:
            return True, BP.process_python_code(varsPreEnvString, self)
        else:
            self.generateVarsKeyListAndVarsPool(
                varsstring)  # DONE 处理完varsPre,生成对应的varsPool
            return False, ""

    def appendPythonModeLog(self, isPythonMode, pythonModeLog):
        if isPythonMode:
            return "\n\n############################################################\npython日志:\n%s" % pythonModeLog
        else:
            return ""

    @catch_exception
    def processBeforeExecute(self):
        logging.debug("○1===traceId[%s]★★★★★" % self.traceId)
        self.initRequestHostAndResults()  # 初始化uri,供全局使用
        logging.debug("○○2===traceId[%s]★★★★★" % self.traceId)

        if self.testResult in self.exitExecStatusList:
            self.assertResult = "处理initRequestHostAndResults时出现错误:\n%s" % self.assertResult
            return False

        self.globalDB.initGlobalDBConf()
        self.globalDB.setCursorDict(False)

        logging.debug("★★★★★★★★★★★★★★★★★★★★★ %s" % self.serviceDBDict)
        if self.serviceDBDict == {}:
            self.initDB()  # 初始化数据库,共全局使用
            logging.debug("○○21===traceId[%s]★★★★★" % self.traceId)

        if self.serviceRedisDict == {}:
            self.initRedis()  #初始化redis
            logging.debug("○○22===traceId[%s]★★★★★" % self.traceId)

        if self.serviceMongoDBDict == {}:
            self.initMongoDB()  #初始化mongodb
            logging.debug("○○23===traceId[%s]★★★★★" % self.traceId)

        logging.debug("○○○3===traceId[%s]★★★★★" % self.traceId)

        if self.testResult in self.exitExecStatusList:
            # y应该不会出现,出现了就是程序见鬼了
            self.assertResult = "初始化时出现错误或者异常。\n%s" % self.assertResult
            return False
        # DONE 1、验证基本数据
        logging.debug("EXECUTE_HTTP_STEP_1: 验证基本数据。")
        if self.validate() == False:
            self.assertResult = "执行前验证基本数据出现错误或者异常。\n%s" % self.assertResult
            return False
        # DONE pre 先对所有字段进行全局变量和组合文本替换 varsPre uri url method header parmas varsPost
        # 对所有字段进行全局文本处理。
        # 在函数中直接增加了错误信息,不需要继续增加assertResult信息
        if self.testResult in self.exitExecStatusList:
            return False
        # DONE 2、处理varsPool,varsPre  前置变量处理
        # DONE 3、处理变量中的关键字 关键字包括全局变量替换、组合文本替换等。
        logging.debug("EXECUTE_HTTP_STEP_2: 处理[准备]阶段。")
        dataInitStartTime = time.time()
        self.pythonModeExecuteLog = ""
        logging.debug("○○○○4===traceId[%s]★★★★★" % self.traceId)
        self.isPythonModePre, isContinue = self.processVarsstring(self.varsPre)
        logging.debug("○○○○○5===traceId[%s]★★★★★" % self.traceId)

        if self.isPythonModePre:
            self.pythonLogVarsPre = self.pythonModeExecuteLog  #TODO 这里是临时方案,这个到时候考虑放到某个地方展示。
            if isContinue == False:
                if self.generateVarsStringByVarsPoolAndVarsKeyList().strip(
                ) != "":
                    self.varsPre = self.generateVarsStringByVarsPoolAndVarsKeyList(
                    ).strip() + self.appendPythonModeLog(
                        self.isPythonModePre, self.pythonLogVarsPre
                    )  # 对生成的varsPool和varKey重新赋值给varsPre
                else:
                    self.varsPre = self.appendPythonModeLog(
                        self.isPythonModePre, self.pythonLogVarsPre).strip(
                        )  # 对生成的varsPool和varKey重新赋值给varsPre

                self.checkAllInfosAfterTest()
                return False
        logging.debug("○○○○○○6===traceId[%s]★★★★★" % self.traceId)

        dataInitEndTime = time.time()
        if self.generateVarsStringByVarsPoolAndVarsKeyList().strip() != "":
            self.varsPre = self.generateVarsStringByVarsPoolAndVarsKeyList(
            ).strip() + self.appendPythonModeLog(
                self.isPythonModePre,
                self.pythonLogVarsPre)  # 对生成的varsPool和varKey重新赋值给varsPre
        else:
            self.varsPre = self.appendPythonModeLog(
                self.isPythonModePre, self.pythonLogVarsPre).strip(
                )  # 对生成的varsPool和varKey重新赋值给varsPre

        self.beforeExecuteTakeTime = int(
            (dataInitEndTime - dataInitStartTime) * 1000)
        logging.debug("○○○○○○○7===traceId[%s]★★★★★" % self.traceId)

        if self.testResult in self.exitExecStatusList:
            self.checkAllInfosAfterTest()
            self.assertResult = "%s: 处理[准备]阶段时出现错误:\n%s" % (self.testResult,
                                                            self.assertResult)
            return False
        return True

    @catch_exception
    def processAfterExecute(self):
        #先处理执行过程中调用的数据
        if self.generateVarsStringByVarsPoolAndVarsKeyList().strip() != "":
            self.varsPre = self.generateVarsStringByVarsPoolAndVarsKeyList(
            ).strip() + self.appendPythonModeLog(
                self.isPythonModePre,
                self.pythonLogVarsPre)  # 对生成的varsPool和varKey重新赋值给varsPre
        else:
            self.varsPre = self.appendPythonModeLog(
                self.isPythonModePre, self.pythonLogVarsPre).strip(
                )  # 对生成的varsPool和varKey重新赋值给varsPre
        #生成之后开始处理后置
        self.calledVarsKeyList = []
        self.pythonModeExecuteLog = ""
        # DONE 7、处理varsPool varsPost处理后置变量
        # DONE 8、处理varsPool中的关键字
        logging.debug("EXECUTE_HTTP_STEP_6: 处理[断言恢复]阶段。")
        dataRecoverStartTime = time.time()
        self.isPythonModePost, isContinue = self.processVarsstring(
            self.varsPost)
        if self.isPythonModePost:
            self.pythonLogVarsPost = self.pythonModeExecuteLog  # TODO 这里是临时方案,这个到时候考虑放到某个地方展示。
            if isContinue == False:
                if self.generateVarsStringByVarsPoolAndVarsKeyList().strip(
                ) != "":
                    self.varsPost = self.generateVarsStringByVarsPoolAndVarsKeyList(
                    ) + self.appendPythonModeLog(
                        self.isPythonModePost, self.pythonLogVarsPost
                    )  # 对生成的varsPool和varKey重新赋值给varsPre
                else:
                    self.varsPost = self.appendPythonModeLog(
                        self.isPythonModePost, self.pythonLogVarsPost).strip(
                        )  # 对生成的varsPool和varKey重新赋值给varsPre

                self.checkAllInfosAfterTest()
                return False

        if self.generateVarsStringByVarsPoolAndVarsKeyList().strip() != "":
            self.varsPost = self.generateVarsStringByVarsPoolAndVarsKeyList(
            ) + self.appendPythonModeLog(
                self.isPythonModePost,
                self.pythonLogVarsPost)  # 对生成的varsPool和varKey重新赋值给varsPre
        else:
            self.varsPost = self.appendPythonModeLog(
                self.isPythonModePost, self.pythonLogVarsPost).strip(
                )  # 对生成的varsPool和varKey重新赋值给varsPre

        self.setFinalAssertMsg()
        # DONE 10、处理数据恢复中的变量替换、关键字处理
        logging.debug("EXECUTE_HTTP_STEP_8: 处理现场恢复。")
        dataRecoverEndTime = time.time()
        self.afterExecuteTakeTime = int(
            (dataRecoverEndTime - dataRecoverStartTime) * 1000)
        self.totalTakeTime = self.beforeExecuteTakeTime + self.executeTakeTime + self.afterExecuteTakeTime
        self.checkAllInfosAfterTest()
        return True

    def getVarkeyPathKey(self, varkey):
        return "PaTh-_-%s" % varkey

    def setVar(self, varkey, value):
        if not VerifyTool.IsVarMatch(varkey):
            #如果不是合法的varkey和value,讲不设置到变量池。
            self.setERROR("<ERROR:不合法的变量key[%s]>" % varkey)
            return
        if not VerifyTool.IsValidVarValue(value):
            self.setERROR(
                "<ERROR:不合法的变量value类型[%s],可赋值类型:int/float/str/dict/list>" %
                type(value))
            return
        pathVarkey = self.getVarkeyPathKey(varkey)
        historyPath = ""
        valueSub = str(value) if len(
            str(value)) < 20 else str(value)[:19] + "..."
        currentPath = "[%s:%s]" % (self.interfaceId, valueSub)
        if varkey in self.varsPool.keys():
            if varkey in ["VAR", "retStr"]:
                #EXEC_PYTHON的变量池,旧版。什么也不用做。
                return
            if varkey in [
                    "DEBUG_MODE", "IS_CONTINUE", "context", "ASSERT_MODE"
            ]:
                #不用设置Path,因为是pythonmode的三个常量,都是当前生效。
                self.varsPool[varkey] = value
                return
            if self.varsPool[varkey] == value:
                #不用设置,变量池中的跟当前的一致
                if pathVarkey not in self.varsPool.keys():
                    self.varsPool[pathVarkey] = currentPath
                return
            if pathVarkey in self.varsPool.keys():
                historyPath = self.varsPool[pathVarkey]

        hisPathList = historyPath.split("->")
        if historyPath != "":
            if len(hisPathList) >= 2:
                thisPath = "%s->%s->%s" % (hisPathList[-2], hisPathList[-1],
                                           currentPath)
            else:
                thisPath = "%s->%s" % (hisPathList[-1], currentPath)
        else:
            thisPath = currentPath
        self.varsPool[varkey] = value
        self.varsPool[pathVarkey] = thisPath

    def getRequestAddr(self, uriKey):
        with DBTool() as tmpdb:
            reqAddr = tmpdb.execute_sql(
                "select requestAddr from tb_env_uri_conf where httpConfKey='%s' and uriKey='%s'"
                % (self.confHttpLayer.key, uriKey))
        if reqAddr:
            return reqAddr[0]['requestAddr']
        else:
            return ""

    def get_python_mode_buildIn_functions(self):
        return getPythonThirdLib() + "\n" + getPythonModeBuildInFromScriptFile(
        )

    def processExecuteAttrAndRun(self, whetherSetResult=True):
        pass

    def transferAttrsFrom1Pto2P(self,
                                param1,
                                param2,
                                isTrans_calledInterfaceRecurDict=False):
        # 执行前全局属性传递,需要回传
        param2.current_session = param1.current_session
        param2.kw_LOGIN_ParamList = param1.kw_LOGIN_ParamList
        # 不需要回传
        param2.highPriorityVARSStr = param1.highPriorityVARSStr
        param2.highPriorityVARSDict = param1.highPriorityVARSDict
        # 传递整个变量池varsPool
        param2.varsPool = param1.varsPool
        param2.httpConfKey = param1.httpConfKey
        param2.confHttpLayer = param1.confHttpLayer

        param2.context_data_list = param1.context_data_list
        param2.context_data_dict = param1.context_data_dict

        param2.serviceDBDict = param1.serviceDBDict
        param2.serviceRedisDict = param1.serviceRedisDict
        param2.serviceMongoDBDict = param1.serviceMongoDBDict

        param2.serviceMongoDBDict = param1.serviceMongoDBDict
        param2.serviceMongoDBDict = param1.serviceMongoDBDict
        param2.serviceMongoDBDict = param1.serviceMongoDBDict

        if isTrans_calledInterfaceRecurDict:
            param2.calledInterfaceRecurDict = param1.calledInterfaceRecurDict

    def updateVarWhichNoPath(self):
        #更新没有Path的varkey,重要是一些python模式执行时没有设置varkeyPath的python变量
        varsPoolCurrentKeysList = deepcopy(list(self.varsPool.keys()))
        for tmpKey in varsPoolCurrentKeysList:
            if tmpKey in [
                    "VAR", "retStr", "DEBUG_MODE", "IS_CONTINUE", "context",
                    "ASSERT_MODE"
            ]:
                continue
            if not tmpKey.startswith("PaTh-_-") and VerifyTool.IsVarMatch(
                    tmpKey) and VerifyTool.IsValidVarValue(
                        self.varsPool[tmpKey]):
                self.setVar(tmpKey, self.varsPool[tmpKey])
                #这里没有想好,如果是python模式中的变量,但是没有被引用过,要不要暴露出来?
                self.updateCalledVarkeyList(tmpKey)