Exemplo n.º 1
0
def Init_GlobalParameter(self,pid):
    # 初始化日志
    log_name = 'init_gp_' + random_str(6) + '.log'
    log_batch = 'Init_GlobalParameter'
    self.log_init_gp = Log.get_log(log_name, log_batch, 'init_gp')
    self.logger_init_gp = self.log_init_gp.get_logger()
    self.logger_init_gp.info("-------初始化全局参数开始!-------")
    all_gparam = GlobalParameterInfo.objects.filter(Q(belong_project_id=pid) & Q(param_type='3'))
    for obj in all_gparam:
        # 获取并打印用例类型param_type='3'的参数名称和参数内容
        gparam_name = obj.name
        gparam_content = eval(obj.param_content)
        # print("gparam_name:", gparam_name)
        # print("gparam_content:", obj.param_content)
        # print("gparam_content:", type(gparam_content))
        self.logger_init_gp.info("gparam_name:" + gparam_name)
        self.logger_init_gp.info("gparam_content:" + str(gparam_content))
        # 获取全局参数的关联的related_case_id
        related_case_id = obj.related_case.id
        api_name = obj.related_case.api_name
        # print("api_name:", api_name)
        all_apicase = ApiCaseInfo.objects.get(id=int(related_case_id))
        api_request = json.loads(all_apicase.api_request)
        method_type = api_request["url"]["method_type"]
        # print("method_type", method_type)

        # 初始化前后置操作
        pre_operation = []
        after_operation = []
        run_after_operation = []
        send_url = ''
        ds = {}
        headers = {}
        Cookies = {}
        request = {}
        res_dict = {}
        # 第一步:获取预置操作id,并且执行预制操作
        if all_apicase.pre_operation:
            all_pre_operation = eval(all_apicase.pre_operation)
            print("all_pre_operation:", all_pre_operation)
            # 调用前后置操作
            time.sleep(1)
            self.logger_init_gp.error("调用前后置操作:", *all_pre_operation)
            try:
                (res_dict, detail_opt) = run_opt(*all_pre_operation)
            except Exception as err:
                print("调用前后置操作失败:", *all_pre_operation)
                self.logger_init_gp.error("调用前后置操作失败:", *all_pre_operation)
            self.logger_init_gp.info("/n" + str(detail_opt) + "/n")
            print("前置操作返回参数res_dict值:", res_dict)
            self.logger_init_gp.error("前置操作返回参数res_dict值:", **res_dict)

            # print("前置操作返回参数res_dict类型:", type(res_dict))

        # 第二步:获取数据池第一行数据和请求相关数据,eg:header/cookie/request
        # 初始化全局参数,只取数据池第一行数据
        if api_request["ds"]:
            ds = api_request["ds"][0]
            if 'checkbox' in ds:
                ds.pop('checkbox')
            if 'ID' in ds:
                ds.pop('ID')
            # del ds['checkbox']
            # del ds['ID']
        # 获取接口请求url
        if api_request["url"]["host"]:
            send_host = api_request["url"]["host"]
            send_host = GlobalParameterInfo.objects.get(id=int(send_host)).value
            send_url = send_host + api_request["url"]["api_path"]
            # print("send_url:", send_url)
            self.logger_init_gp.info("send_url:" + send_url)

        else:
            print("请求url不合法!")
            self.logger_init_gp.error("请求url不合法!")
            continue

        # 获取接口请求head
        if api_request["head"]:
            head = api_request["head"]
            headers = config_disc(str(head), str(res_dict), str(ds))
            self.logger_init_gp.info("headers:" + str(headers))
            # print("headers:", headers)

        # 获取接口请求cookies
        if api_request["Cookies"]:
            Cookie = api_request["Cookies"]
            Cookies = config_disc(str(Cookie),  str(res_dict), str(ds))
            self.logger_init_gp.info("Cookies:" + str(Cookies))
            # print("Cookies:", Cookies)

        if api_request["request"]:
            # print("api_request:", api_request["request"])
            api_request = api_request["request"]
            request = config_disc(str(api_request), str(res_dict), str(ds))
            self.logger_init_gp.info("request:" + str(request))

            # print("request:", request)
        else:
            print("请求body不合法!")
            self.logger_init_gp.error("请求body不合法!")
            continue

        # 第三步:发送请求
        try:
            return_json = send_request(api_name, method_type, send_url, str(headers), str(Cookies), str(request))
        except Exception as err:
            # print("Time out!")
            self.logger_init_gp.error("Time out!")
            continue
        # 获取响应消息体
        reps_data = json.loads(show_return_data(return_json))
        self.logger_init_gp.info("\n请求返回值:" + '\n' + str(reps_data))
        # 获取响应消息头
        reps_headers = show_return_headers(return_json)
        self.logger_init_gp.info("reps_headers", reps_headers)

        # 获取响应消息cookies
        reps_cookies = show_return_cookies(return_json)
        self.logger_init_gp.info("reps_cookies", **reps_cookies)

        reps_josn = json.dumps(reps_data, ensure_ascii=False, sort_keys=True, indent=4)
        print("\n请求返回值:" + '\n' + reps_josn)
        # configHttp.logger.info("response:" + reps_josn)

        # 获取全局参数中gparam_content字典中的值,并使用jsonpath获取对应的返回值后,回填到参数对应的value字段中
        all_gparam_info = GlobalParameterInfo.objects.get(id=obj.id)
        print(all_gparam_info)

        gparme_header = gparam_content['param_header']
        print("gparme_header:", gparme_header)
        gparme_cookie = gparam_content['param_cookie']
        print("gparme_cookie:", gparme_cookie)
        gparme_path = gparam_content['param_path']
        print("gparme_path:", gparme_path)
        gparme_value = ''
        if gparme_path != '' and jsonpath.jsonpath(reps_data, gparme_path):
            # 应该jsonpath取出来的是个list所以取list的第一个元素,即[0]
            if jsonpath.jsonpath(reps_data, gparme_path)[0]:
                gparme_value = jsonpath.jsonpath(reps_data, gparme_path)[0]
                print("gparme_value:",gparme_value)

            else:
                gparme_value = ''
            all_gparam_info.value = gparme_value
            all_gparam_info.save()
        elif gparme_cookie != '':
            gparme_value = reps_cookies[gparme_cookie]
            all_gparam_info.value = gparme_value
            all_gparam_info.save()
        elif gparme_header != '':
            gparme_value = reps_headers[gparme_header]
            all_gparam_info.value = gparme_value
            all_gparam_info.save()
        else:
            all_gparam_info.value = gparme_value
            all_gparam_info.save()

        # 第四步:获取执行后操作,并且执行
        if all_apicase.run_after_operation:
            all_run_after_operation = eval(all_apicase.run_after_operation)
            print("all_run_after_operation:", all_run_after_operation)
            self.logger_init_gp.info("调用执行后操作:", *all_run_after_operation)
            # 调用后置操作
            print("调用执行后操作操作", str(all_run_after_operation))
            try:
                (res_dict_run_after, detail_opt_run_after) = run_opt(*all_run_after_operation)
            except Exception as err:
                self.logger_init_gp.error("调用执行后操作失败!")
            self.logger_init_gp.info("/n" + str(detail_opt_run_after) + "/n")

        # 第五步:获取后置操作,并且执行
        if all_apicase.after_operation:
            all_after_operation = eval(all_apicase.after_operation)
            # print("all_pre_operation:", all_after_operation)
            self.logger_init_gp.info("调用后置操作:", *all_after_operation)
            # 调用后置操作
            print("调用后置操作", str(all_after_operation))
            try:
                (res_dict_after, detail_opt_after) = run_opt(*all_after_operation)
            except Exception as err:
                self.logger_init_gp.error("调用后置操作失败!")
            self.logger_init_gp.info("/n" + str(detail_opt_after) + "/n")

    self.logger_init_gp.info("-------初始化全局参数结束!-------")

    Init_GP_detail = self.log_init_gp.get_logContent()
    print("Init_GP_detail", Init_GP_detail)
    self.log_init_gp.remove_handler()
    return Init_GP_detail
Exemplo n.º 2
0
def run_opt(self, *opt_id):
    # 初始化日志
    log_name = 'run_opt_' + random_str(6) + '.log'
    log_batch = 'run_opt'
    self.log_opt = Log.get_log(log_name, log_batch, 'run_opt')
    self.logger_opt = self.log_opt.get_logger()
    self.logger_opt.info("-------执行前后置操作开始!-------")
    self.logger_opt.info("前后置操作id:" + str(opt_id))
    # opt_id: ['5', '6']
    # 定义返回值res_dict
    global res_dict
    res_dict = {}
    for i in opt_id:
        # 这里过滤掉删除状态的操作,all_opts.type=1 表示用例类型的前后置操作
        all_opts = OperationInfo.objects.get(Q(id=int(i)) & Q(status='1'))
        # 等待类型的操作,调用time
        if all_opts.type == '2':
            times = int(all_opts.operation)
            time.sleep(times)
            print("Time sleep:", times, "秒")
            self.logger_opt.info("Time sleep:" + str(times) + "秒")

            # configHttp.logger.info("Time sleep:" + str(times) + "秒")
        # 用例类型的操作,调用调用requests,发送请求
        if all_opts.type == '0':
            # 获取请求提取内容字典
            # opt_operation = map(eval, all_opts.operation)
            opt_operation = json.loads(all_opts.operation)
            # print("opt_operation:", opt_operation)
            # print("opt_operation_type:", type(opt_operation))
            # 获取操作关联的用例类
            all_apicase = ApiCaseInfo.objects.get(id=int(all_opts.related_case.id))
            # api_method = all_apicase.api_method
            api_id = all_apicase.id
            api_name = all_apicase.api_name
            self.logger_opt.info("操作管理用例ID和名称" + str(api_id) + "_" + str(api_name))
            api_request = json.loads(all_apicase.api_request)
            method_type = api_request["url"]["method_type"]
            # 第二步:获取数据池第一行数据和请求相关数据,eg:header/cookie/request
            ds = {}
            headers = {}
            Cookies = {}
            request = {}
            # 初始化全局参数,只取数据池第一行数据
            if api_request["ds"]:
                ds = api_request["ds"][0]
                if 'checkbox' in ds:
                    ds.pop('checkbox')
                if 'ID' in ds:
                    ds.pop('ID')
            # 获取接口请求url
            if api_request["url"]["host"]:
                send_host = api_request["url"]["host"]
                send_host = GlobalParameterInfo.objects.get(id=int(send_host)).value
                send_url = send_host + api_request["url"]["api_path"]
                self.logger_opt.info("send_url:" + send_url)
            else:
                print("请求url不合法!")
                self.logger_opt.info("请求url不合法!")
                continue
            # 获取接口请求head
            if api_request["head"]:
                head = api_request["head"]
                # print("head:", head)
                headers = config_disc(str(headers),str(res_dict),str(ds))
                print("headers:", headers)
                self.logger_opt.info("headers:" + str(headers))
            # 获取接口请求cookies
            if api_request["Cookies"]:
                Cookie = api_request["Cookies"]
                Cookies = config_disc(str(Cookie),str(res_dict),str(ds))
                print("Cookies:", Cookies)
                self.logger_opt.info("Cookies:" + str(Cookies))
            # 获取接口请求消息体request
            if api_request["request"]:
                # print(api_request["request"])
                request_data = api_request["request"]
                request = config_disc(str(request_data),str(res_dict),str(ds))
                print("request:", request)
                self.logger_opt.info("request:" + str(request))
            else:
                print("请求body不合法!")
                self.logger_opt.info("请求body不合法!")
                continue
            # 第三步:发送请求
            try:
                return_json = send_request(api_name, method_type, send_url, str(headers), str(Cookies), str(request))
            except Exception as err:
                self.logger_opt.info("Time out!")
                continue
            # 获取响应消息体
            reps_data = json.loads(show_return_data(return_json))
            # 获取响应消息头
            reps_headers = show_return_headers(return_json)
            # 获取响应消息cookies
            reps_cookies = show_return_cookies(return_json)
            # print("url:", return_json.url)
            # self.logger_opt.info("url:" + str(return_json.url))
            print(str(return_json.request))
            print("reps_headers:", reps_headers)
            self.logger_opt.info("reps_headers:" + str(reps_headers))
            print("reps_cookies:", reps_cookies)
            self.logger_opt.info("reps_cookies:" + str(reps_cookies))
            reps_josn = json.dumps(reps_data, ensure_ascii=False, sort_keys=True, indent=4)
            print("\n请求返回值:" + '\n' + reps_josn)
            self.logger_opt.info("\n请求返回值:" + '\n' + str(reps_josn))
            # 根据接口返回,取前后置操作的operation,给数据池/参数取值
            self.logger_opt.info("根据接口返回,取前后置操作的operation,给数据池/参数取值")
            for operation in opt_operation:
                # print("operation:", operation)
                # print("operation_type:", type(operation))
                opt_res_name = operation['ParamName']
                if operation['JsonPath'] != '' and jsonpath.jsonpath(reps_data, operation['JsonPath'])[0]:
                    opt_res_value = jsonpath.jsonpath(reps_data, operation['JsonPath'])[0]
                elif operation['CookieName'] != '' and reps_cookies[operation['CookieName']]:
                    opt_res_value = reps_cookies[operation['CookieName']]
                elif operation['HeaderName'] != '' and reps_headers[operation['HeaderName']]:
                    opt_res_value = reps_headers[operation['HeaderName']]
                else:
                    opt_res_value = ''
                res_dict.update({opt_res_name: opt_res_value})
    print("res_dict:", res_dict)
    self.logger_opt.info("res_dict:" + str(res_dict))
    self.logger_opt.info("-------执行前后置操作结束!-------")
    detail_opt = self.log_opt.get_logContent()
    self.log_opt.remove_handler()
    return (res_dict, str(detail_opt))