Exemple #1
0
def batchrun(request):
    result = {}
    if request.method == 'POST':
        req = json.loads(request.body)["params"]
        idlist = req['idList']
        environment = req["environment"]
        if len(idlist) == 0:
            result = {"code": -1, "info": "执行列表为空"}
            return JsonResponse(result)
        reportflag = req["reportflag"]
        exeuser = request.session.get('username')
        starttime = time.strftime('%Y-%m-%d %H:%M:%S',
                                  time.localtime(time.time()))
        if reportflag == True:
            reflag = "Y"
        else:
            reflag = "N"
        for id in idlist:
            try:
                query = apiInfoTable.objects.get(apiID=id)
            except Exception as e:
                result = {"code": -2, "info": "用例不存在," + str(e)}
                return JsonResponse(result)
            if query.method == "" or query.url == "":
                result = {"code": -1, "info": "method或url不能为空"}
                return JsonResponse(result)
        # batchrun_list = [{"sname": "批量执行", "list": idlist, "cookices": cookices}]
        try:
            batchrun_list = getbatchrunList(idlist, exeuser, environment)
            # print("batchrun_list: ", batchrun_list)
        except Exception as e:
            result = {"code": -1, "info": "获取执行列表失败"}
            return JsonResponse(result)
        batchResult = batchstart.start_main(batchrun_list, environment, reflag,
                                            exeuser)
        # print batchResult
        if reportflag == True:
            report_localName = batchResult["reportPath"]
            report_runName = req["pmName"]  # +"_" + batchResult["reportname"]
            successNum = batchResult["sNum"]
            failNum = batchResult["fNum"]
            errorNum = batchResult["eNum"]
            totalNum = successNum + failNum + errorNum
            endtime = time.strftime('%Y-%m-%d %H:%M:%S',
                                    time.localtime(time.time()))
            result_infos = {
                "report_runName": report_runName,
                "environment": environment,
                "startTime": starttime,
                "endTime": endtime,
                "totalNum": totalNum,
                "successNum": successNum,
                "failNum": failNum,
                "errorNum": errorNum,
                "executor": exeuser,
                "report_localName": report_localName,
            }
            try:
                s = reports.objects.create(**result_infos)
                s.save()
            except BaseException as e:
                print(" SQL Error: %s" % e)
                result = {'code': -1, 'info': 'sql error'}
                return JsonResponse(result)
            result = {"code": 0, "info": "执行结束,结果请查看报告"}
        else:
            result = {"code": 0, "info": "执行结束,结果:" + str(batchResult)}
    return JsonResponse(result)
def runCase(request):
    result = {'code': -3, 'info': '接口调用错误!'}
    if request.method == 'POST':
        req = json.loads(request.body)["params"]
        id = req['id']
        environment = req['environment']
        runResultName = req['runResultName']
        # reportflag = "Y"
        reportflag = req['reportflag']
        exeuser = request.session.get('username')
        starttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        paramList = str(id).split(',')
        caseID = paramList[0]
        cookieID = paramList[1]
        list = []
        caseName = caseList.objects.filter(id=caseID).values("caseName")[0]['caseName']
        includeAPI = caseList.objects.filter(id=caseID).values("includeAPI")[0]['includeAPI']
        APIID = str(includeAPI).split(',')
        if (APIID[0] != ''):
            for x in APIID:
                list.append(x)
                # [{"sname":"登录","list":[1,5,10],"cookices":{}}]
            if (cookieID == '不使用Cookie'):
                batchrunJson = {
                    "sname": str(caseName),
                    "list": list,
                }
            else:
                cookie = userCookies.objects.filter(id=cookieID).values("cookies")[0]['cookies']
                cookices = json.loads(cookie)
                batchrunJson = {
                    "sname": str(caseName),
                    "list": list,
                    "cookices": cookices,
                }
            batchrun_list = []
            batchrun_list.append(batchrunJson)
            batchResult = batchstart.start_main(batchrun_list, environment, reportflag, exeuser)
            if (reportflag == 'Y'):
                report_localName = batchResult["reportPath"]
                report_runName = runResultName
                successNum = batchResult["sNum"]
                failNum = batchResult["fNum"]
                errorNum = batchResult["eNum"]
                totalNum = successNum + failNum + errorNum
                endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                result_infos = {
                    "report_runName": report_runName,
                    "environment": environment,
                    "startTime": starttime,
                    "endTime": endtime,
                    "totalNum": totalNum,
                    "successNum": successNum,
                    "failNum": failNum,
                    "errorNum": errorNum,
                    "executor": exeuser,
                    "report_localName": report_localName,
                }
                try:
                    s = reports.objects.create(**result_infos)
                    s.save()
                except BaseException as e:
                    print(" SQL Error: %s" % e)
                    result = {'code': -2, 'info': 'sql error'}
                    return JsonResponse(result)
                inter = caseList.objects.get(id=caseID)
                inter.lastRunTime = starttime
                inter.reportLocation = report_localName
                if (totalNum == successNum):
                    inter.runResult = str(environment) + "环境运行成功"
                else:
                    inter.runResult = str(environment) + "环境运行失败"
                inter.save()
                result = {"code": 0, "info": "执行结束,结果请查看报告"}
            else:
                successNum = batchResult["sNum"]
                failNum = batchResult["fNum"]
                errorNum = batchResult["eNum"]
                inter = caseList.objects.get(id=caseID)
                inter.lastRunTime = starttime
                if (failNum == 0) and (errorNum == 0):
                    inter.runResult = str(environment) + "环境运行成功"
                else:
                    inter.runResult = str(environment) + "环境运行失败"
                inter.save()
                info = u"执行结束,结果:<br>成功:" + str(successNum) + ";失败:" + str(failNum) + ";出错:" + str(
                    errorNum)
                result = {
                    "code": 0,
                    "info": info
                }
        else:
            code = -1
            info = '用例下不存在接口!'
            result = {
                'code': code,
                'info': info
            }
    return JsonResponse(result, safe=False)
Exemple #3
0
def caseBatchRun(request):
    result = {'code': -1, 'info': '接口调用错误!'}
    if request.method == 'POST':
        req = json.loads(request.body)["params"]
        id = req['idCookie']
        environment = req['environment']
        runResultName = req['runResultName']
        # reportflag = "Y"
        reportflag = req['reportflag']
        exeuser = request.session.get('username')
        starttime = time.strftime('%Y-%m-%d %H:%M:%S',
                                  time.localtime(time.time()))
        variableList = req["pubvariable"]
        heanderList = req["pubHeader"]
        variables = {}
        publicHeader = {}
        print heanderList
        print variableList
        for i in heanderList:
            publicHeader[str(i["name"])] = i["value"]
        for j in variableList:
            print j
            variables[str(j["name"])] = until.toType(
                j["type"], j["value"]).toreturnType()

        batchrun_list = []
        info = ""
        for case in id:
            paramList = str(case).split(',')
            caseID = paramList[0]
            cookieID = paramList[1]
            list = []
            caseName = caseList.objects.filter(
                id=caseID).values("caseName")[0]['caseName']
            includeAPI = caseList.objects.filter(
                id=caseID).values("includeAPI")[0]['includeAPI']
            APIID = str(includeAPI).split(',')
            if (APIID[0] != ''):
                for x in APIID:
                    list.append(x)
                    # [{"sname":"登录","list":[1,5,10],"cookices":{},"variables": {}, "publicHeader", {}}]
                if (cookieID == '不使用Cookie'):
                    batchrunJson = {
                        "sname": str(caseName),
                        "list": list,
                    }
                else:
                    cookie = userCookies.objects.filter(
                        id=cookieID).values("cookies")[0]['cookies']
                    cookices = json.loads(cookie)
                    batchrunJson = {
                        "sname": str(caseName),
                        "list": list,
                        "cookices": cookices,
                    }
                batchrunJson["variables"] = variables
                batchrunJson["publicHeader"] = publicHeader
                batchrun_list.append(batchrunJson)
        batchResult = batchstart.start_main(batchrun_list, environment,
                                            reportflag, exeuser)
        if (reportflag == 'Y'):
            report_localName = batchResult["reportPath"]
            report_runName = runResultName
            successNum = batchResult["sNum"]
            failNum = batchResult["fNum"]
            errorNum = batchResult["eNum"]
            totalNum = successNum + failNum + errorNum
            endtime = time.strftime('%Y-%m-%d %H:%M:%S',
                                    time.localtime(time.time()))
            result_infos = {
                "report_runName": report_runName,
                "environment": environment,
                "startTime": starttime,
                "endTime": endtime,
                "totalNum": totalNum,
                "successNum": successNum,
                "failNum": failNum,
                "errorNum": errorNum,
                "executor": exeuser,
                "report_localName": report_localName,
            }
            try:
                s = reports.objects.create(**result_infos)
                s.save()
            except BaseException as e:
                print(" SQL Error: %s" % e)
                result = {'code': -2, 'info': 'sql error'}
                return JsonResponse(result)
            for case in id:
                paramList = str(case).split(',')
                caseID = paramList[0]
                inter = caseList.objects.get(id=caseID)
                inter.lastRunTime = starttime
                inter.reportLocation = report_localName
                includeAPI = caseList.objects.filter(
                    id=caseID).values("includeAPI")[0]['includeAPI']
                APIID = str(includeAPI).split(',')
                Success = True
                if (APIID[0] != ''):
                    for x in APIID:
                        flag = int(
                            apiInfoTable.objects.filter(apiID=x).values(
                                "lastRunResult")[0]['lastRunResult'])
                        if (flag == -1):
                            Success = False
                if (Success):
                    inter.runResult = str(environment) + "环境运行成功"
                else:
                    inter.runResult = str(environment) + "环境运行失败"
                inter.save()
                result = {"code": 0, "info": "执行结束,结果请查看报告"}
        else:
            for case in id:
                paramList = str(case).split(',')
                caseID = paramList[0]
                successNum = batchResult["sNum"]
                failNum = batchResult["fNum"]
                errorNum = batchResult["eNum"]
                inter = caseList.objects.get(id=caseID)
                inter.lastRunTime = starttime
                if (failNum == 0) and (errorNum == 0):
                    inter.runResult = str(environment) + "环境运行成功"
                else:
                    inter.runResult = str(environment) + "环境运行失败"
                inter.save()
                info = u"执行结束,结果:<br>成功:" + str(successNum) + ";失败:" + str(
                    failNum) + ";出错:" + str(errorNum)
            result = {"code": 0, "info": info}
    return JsonResponse(result, safe=False)