def __init__(self, case_id):
     self.case_id = case_id
     self.use_excel = UseExcel()
     self.run_method = SendGetPost()
     self.data = GetData()
     self.tool = CommonTool()
     self.use_mysql = UseMySql()
Exemplo n.º 2
0
 def __init__(self):
     self.run_method = SendGetPost()
     self.data = GetData()
     self.tool = CommonTool()
     self.send_mail = SendEmail()
     self.use_mysql = UseMySql()
     self.excel = UseExcel()
Exemplo n.º 3
0
class BaseFlow:
    def __init__(self):
        self.run_method = SendGetPost()
        self.data = GetData()
        self.tool = CommonTool()
        self.send_mail = SendEmail()
        self.use_mysql = UseMySql()
        self.excel = UseExcel()

    # def mobile(self, data):
    #     # data["mobile"] = self.tool.phone_code_generator()
    #     data["mobile"] = self.__mobile__
    #     return data

    def base_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_rows()

        log_file = "../log/interface-demo03.log"  # 每次执行用例前,将log日志文件清空数据
        with open(log_file, "w") as lf:
            lf.seek(
                0, 0
            )  # 加上lf.seek(0),把文件定位到position 0;若没有这句话,文件是定位到数据最后,truncate也是从最后这里删除。
            lf.truncate()

        if self.excel.sheet_name == 0:
            self.tool.write_mobile()  # 向get_mobile.json 中写入手机号码
            # self.__mobile__ = self.tool.phone_code_generator()

        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    # data = self.data.get_request_data(i)  # 获取excel 中的 data
                    data = self.data.get_request_excel_data(
                        i)  # 直接读取 excel 中的 data

                    # 获取依赖单元格
                    depend_case = self.data.is_depend(i)
                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        depend_response_data = self.depend_data.get_value_for_key(
                            i)  # 获取 依赖字段的 响应数据
                        # depend_key = self.data.get_depend_field(i)  # 获取 请求依赖的 key
                        # data[depend_key] = depend_response_data  # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数

                    if data.find("${mobile}") != -1:
                        # data = data.replace("${mobile}", self.__mobile__)
                        data = data.replace("${mobile}",
                                            self.tool.get_mobile())
                    elif data.find("${idCardNumber}") & data.find(
                            "${name}") != -1:
                        data = data.replace("${idCardNumber}",
                                            self.tool.getRandomIdCard())
                        name = "雨点" + self.tool.num_chinese(
                            self.tool.get_mobile()[-4:])
                        data = data.replace("${name}", name)
                    elif data.find("${businessLicenseNumber}") != -1:
                        data = data.replace("${businessLicenseNumber}",
                                            self.tool.license_no())
                    elif data.find("${write_borrowerCode}") != -1:
                        self.use_mysql.select_borrowercode()
                        use_json = UseJson("../data_config/borrowerCode.json")
                        borrowerCode = use_json.get_data("borrowerCode")
                        data = data.replace("${write_borrowerCode}",
                                            borrowerCode)
                    elif data.find("${borrowerCode}") != -1:
                        use_json = UseJson("../data_config/borrowerCode.json")
                        borrowerCode = use_json.get_data("borrowerCode")
                        data = data.replace("${borrowerCode}", borrowerCode)
                    elif data.find("${creditApplyCode}") != -1:
                        use_json = UseJson(
                            "../data_config/credit_apply_code.json")
                        code = use_json.get_data("code")
                        # code = use_json.read_data()
                        data = data.replace("${creditApplyCode}", code)
                        now = time.strftime("%Y-%m-%d 08:%M:%S")
                        data = data.replace("${nowdate}", now)
                    elif data.find("${nowdate}") != -1:
                        now = time.strftime("%Y-%m-%d 08:%M:%S")
                        data = data.replace("${nowdate}", now)

                    # data = self.data.get_request_excel_data_wrapper(i, self.mobile)  # 直接读取 excel 中的 data
                    # data = self.data.get_data_values(i)
                    # data = self.data.get_data_values_wrapper(i, self.mobile)
                    header = self.data.get_request_header(
                        i)  # 获取 excel 中的 header 关键字
                    # header_json = self.data.get_header_value(i)  # 获取 json 中的 header_key 对应的头文件数据
                    expect_data = self.data.get_expect_data(i)
                    expect = json.loads(expect_data)

                    # # 获取依赖单元格
                    # depend_case = self.data.is_depend(i)
                    # if depend_case != None:
                    #     self.depend_data = DependentData(depend_case)
                    #     depend_response_data = self.depend_data.get_value_for_key(i)  # 获取 依赖字段的 响应数据
                    #     # depend_key = self.data.get_depend_field(i)  # 获取 请求依赖的 key
                    #     # data[depend_key] = depend_response_data  # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数

                    if header == '{"Content-Type":"application/json"}':
                        header1 = eval(header)
                        # header1 = {"Content-Type": "application/json"}
                        # data = json.dumps(data)
                        # res = self.run_method.run_main(method, url, data, header=header_json, params=data)
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header1,
                                                       params=data)
                    elif header == '{"Content-Type": "application/json", "Product": "31G"}':
                        header1 = eval(header)
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header1,
                                                       params=data)
                    elif header == '{"Content-Type":"multipart/form-data"}':
                        # data = {"imageType": "ID_CARD_FRONT_IMAGE"}
                        data = eval(data)  # 把 str 转成 dict
                        with open("H:/wahh.jpg", "rb") as f:  # 打开上传文件
                            r = f.read()
                        files = {"file": ("wahh.jpg", r, "image/jpeg")}
                        use_json = UseJson("../data_config/config_header.json")
                        header_json = use_json.read_data()
                        # header_json = {  # "Content-Type": "multipart/form-data",  # 不要画蛇添足写这一句
                        #     "Cookie": "SESSION=NGExN2Y0MzUtNjVhNy00MDRkLWIyZjItMGFjZWVlMDFiZjM5"}
                        response = requests.post(url=url,
                                                 data=data,
                                                 files=files,
                                                 headers=header_json)  # 获取返回请求
                        res = response.text
                        # res = self.run_method.run_main(method, url, data, header=header_json, params=data)
                    elif header == 'get_cookie':
                        use_json = UseJson("../data_config/config_header.json")
                        cookie_value = use_json.get_data("Cookie")
                        # a = json.dumps(cookie1)
                        # cookie = re.findall(r'{(.*?)}', a)
                        # cookie_value = cookie1['Cookie']
                        header_json = {
                            "Content-Type": "application/json",
                            "Cookie": cookie_value
                        }
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == 'get_cookie,31G':
                        use_json = UseJson("../data_config/config_header.json")
                        cookie_value = use_json.get_data("Cookie")
                        # a = json.dumps(cookie1)
                        # cookie = re.findall(r'{(.*?)}', a)
                        # cookie_value = cookie1['Cookie']
                        header_json = {
                            "Content-Type": "application/json",
                            "Product": "31G",
                            "Cookie": cookie_value
                        }
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == "write_Cookie1":
                        # res = self.run_method.run_main(method, url, data, header=header_json, params=data)
                        use_header = UseHeader(res)
                        use_header.write_cookie()
                    elif header == "get_Cookie1":
                        use_json = UseJson("../data_config/cookie.json")
                        cookie = use_json.get_data("apsid")
                        cookies = {"apsid": cookie}
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data=data,
                                                       header=cookies,
                                                       params=data)
                    else:
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header,
                                                       params=data)

                    if self.tool.is_contain(expect, res):
                        self.data.write_actual(
                            i, json.dumps(res,
                                          ensure_ascii=False))  # 返回值为中文时,不乱码
                        self.data.write_result(i, "PASS")
                        pass_count.append(i)
                    else:
                        self.data.write_actual(
                            i, json.dumps(res, ensure_ascii=False))
                        self.data.write_result(i, "FAIL")
                        with open(log_file, "a", encoding="utf-8") as lf:
                            lf.write("\n第{}条用例实际结果与期望结果不一致:\n".format(i))
                            lf.write("期望结果:{}\n实际结果:{}\n".format(expect, res))
                        fail_count.append(i)
                else:
                    no_run_count.append(i)

            except Exception as e:
                self.data.write_result(i, str(e))
                with open(log_file, "a", encoding="utf-8") as lf:
                    lf.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)
Exemplo n.º 4
0
 def __init__(self):
     self.run_method = SendGetPost()
     self.data = GetData()
     self.tool = CommonTool()
     self.send_mail = SendEmail()
Exemplo n.º 5
0
class BaseFlow:
    def __init__(self):
        self.run_method = SendGetPost()
        self.data = GetData()
        self.tool = CommonTool()
        self.send_mail = SendEmail()

    def base_run(self):
        res = None
        pass_count = []
        fail_count = []
        no_run_count = []
        rows_count = self.data.get_case_rows()

        log_file = "../log/interface-demo02.log"  # 每次执行用例前,将log日志文件清空数据
        with open(log_file, "w") as lf:
            lf.seek(
                0, 0
            )  # 加上lf.seek(0),把文件定位到position 0;若没有这句话,文件是定位到数据最后,truncate也是从最后这里删除。
            lf.truncate()

        self.tool.write_mobile()  # 向get_mobile.json 中写入手机号码

        for i in range(1, rows_count):
            try:
                is_run = self.data.get_is_run(i)
                if is_run:
                    url = self.data.get_request_url(i)
                    method = self.data.get_request_method(i)
                    data = self.data.get_request_excel_data(
                        i)  # 直接读取 excel 中的 data
                    if data.find("${mobile}") != -1:
                        data = data.replace("${mobile}",
                                            self.tool.get_mobile())
                    elif data.find("${idCardNumber}") & data.find(
                            "${name}") != -1:
                        data = data.replace("${idCardNumber}",
                                            self.tool.getRandomIdCard())
                        name = "雨点" + self.tool.num_chinese(
                            self.tool.get_mobile()[-4:])
                        data = data.replace("${name}", name)
                    elif data.find("${businessLicenseNumber}") != -1:
                        data = data.replace("${businessLicenseNumber}",
                                            self.tool.license_no())

                    header = self.data.get_request_header(
                        i)  # 获取 excel 中的 header 关键字
                    expect_data = self.data.get_expect_data(i)
                    expect = json.loads(expect_data)

                    depend_case = self.data.is_depend(i)

                    if depend_case != None:
                        self.depend_data = DependentData(depend_case)
                        depend_response_data = self.depend_data.get_value_for_key(
                            i)  # 获取 依赖字段的 响应数据

                    if header == '{"Content-Type":"application/json"}':
                        header_json = eval(header)
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == '{"Content-Type":"multipart/form-data"}':
                        data = eval(data)  # 把 str 转成 dict
                        with open("H:/wahh.jpg", "rb") as f:  # 打开上传文件
                            r = f.read()
                        files = {"file": ("wahh.jpg", r, "image/jpeg")}
                        use_json = UseJson("../data_config/config_header.json")
                        header_json = use_json.read_data()
                        # header_json = {  # "Content-Type": "multipart/form-data",  # 不要画蛇添足写这一句
                        #     "Cookie": "SESSION=NGExN2Y0MzUtNjVhNy00MDRkLWIyZjItMGFjZWVlMDFiZjM5"}
                        response = requests.post(url=url,
                                                 data=data,
                                                 files=files,
                                                 headers=header_json)  # 获取返回请求
                        res = response.text
                    elif header == 'get_cookie':
                        use_json = UseJson("../data_config/config_header.json")
                        cookie_value = use_json.get_data("Cookie")
                        header_json = {
                            "Content-Type": "application/json",
                            "Cookie": cookie_value
                        }
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header=header_json,
                                                       params=data)
                    elif header == "write_Cookie1":
                        use_header = UseHeader(res)
                        use_header.write_cookie()
                    elif header == "get_Cookie1":
                        use_json = UseJson("../data_config/config_cookie.json")
                        cookie = use_json.get_data("apsid")
                        cookies = {"apsid": cookie}
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data=data,
                                                       header=cookies,
                                                       params=data)
                    else:
                        res = self.run_method.run_main(method,
                                                       url,
                                                       data,
                                                       header,
                                                       params=data)

                    if self.tool.is_contain(expect, res):
                        self.data.write_result(i, "pass")
                        pass_count.append(i)
                    else:
                        self.data.write_result(i, json.dumps(res))
                        with open(log_file, "a", encoding="utf-8") as lf:
                            lf.write("\n第%s条用例实际结果与期望结果不一致:\n" % i)
                            lf.write("期望结果:%s\n实际结果:%s\n" % (expect, res))
                        fail_count.append(i)
                else:
                    no_run_count.append(i)

            except Exception as e:
                self.data.write_result(i, str(e))
                with open(log_file, "a", encoding="utf-8") as lf:
                    lf.write("\n第%s条用例报错:\n" % i)
                initLogging(log_file, e)
                fail_count.append(i)
class DependentData:
    def __init__(self, case_id):
        self.case_id = case_id
        self.use_excel = UseExcel()
        self.run_method = SendGetPost()
        self.data = GetData()
        self.tool = CommonTool()

    def get_case_row_data(self):
        """通过 case_id 去获取依赖 case_id 的整行数据"""
        row_data = self.use_excel.get_row_data(self.case_id)
        return row_data

    def run_dependent(self):
        """执行依赖测试,获取结果"""
        row_num = self.use_excel.get_row_num(self.case_id)
        request_data = self.data.get_request_excel_data(
            row_num)  # 直接读取 excel 中的 data
        if request_data.find("${mobile}") != -1:
            request_data = request_data.replace("${mobile}",
                                                self.tool.get_mobile())

        header = self.data.get_request_header(row_num)
        header = eval(header)
        method = self.data.get_request_method(row_num)
        url = self.data.get_request_url(row_num)
        res = self.run_method.run_main(method,
                                       url,
                                       data=request_data,
                                       header=header,
                                       params=request_data)
        return res

    def get_value_for_key(self, row):
        """获取依赖字段的响应数据,通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value"""
        depend_data = self.data.get_depend_key(row)  # 获取依赖的返回数据 key
        response_data = self.run_dependent()  # 执行依赖 case 返回结果
        get_depend_data = [
            match.value for match in parse(depend_data).find(response_data)
        ][0]
        if depend_data == "accessToken":
            # get_depend_data = hashlib.md5(get_depend_data.encode(encoding='UTF-8')).hexdigest()  # md5加密
            get_depend_data = base64.b64encode(
                get_depend_data.encode('utf-8')).decode("utf-8")  # base64加密
            self.use_file(get_depend_data)
        return get_depend_data

    def md5(self):
        str = '387ff00e-1a54-49b3-87ca-a95ad3c9e08a'
        m = hashlib.md5()
        b = str.encode(encoding='utf-8')
        m.update(b)
        str_md5 = m.hexdigest()
        return str_md5

    def base64(self):
        a = "39397778-f129-41f4-ba28-68af53f5728c"
        jm = base64.b64encode(a.encode('utf-8'))
        b = str(jm)
        base = b[2:-1]
        return base

    def use_file(self, header_cookie):
        config_header = "../data_config/config_header.json"
        with open(config_header, "w") as ch:
            ch.write('{{"Cookie": "SESSION={}"}}'.format(header_cookie))
class DependentData:

    def __init__(self, case_id):
        self.case_id = case_id
        self.use_excel = UseExcel()
        self.run_method = SendGetPost()
        self.data = GetData()
        self.tool = CommonTool()
        self.use_mysql = UseMySql()


    def get_case_row_data(self):
        """通过 case_id 去获取依赖 case_id 的整行数据"""
        row_data = self.use_excel.get_row_data(self.case_id)
        return row_data

    def run_dependent(self):
        """执行依赖测试,获取结果"""
        row_num = self.use_excel.get_row_num(self.case_id)
        # request_data = self.data.get_data_values(row_num)
        request_data = self.data.get_request_excel_data(row_num)  # 直接读取 excel 中的 data
        if request_data.find("${mobile}") != -1:
            # request_data = request_data.replace("${mobile}", '18888881937')
            request_data = request_data.replace("${mobile}", self.tool.get_mobile())
        elif request_data.find("${borrowerCode}") != -1:
            use_json = UseJson("../data_config/borrowerCode.json")
            borrowerCode = use_json.get_data("borrowerCode")
            request_data = request_data.replace("${borrowerCode}", borrowerCode)
        elif request_data.find("${write_borrowerCode}") != -1:
            self.use_mysql.select_borrowercode()
            use_json = UseJson("../data_config/borrowerCode.json")
            borrowerCode = use_json.get_data("borrowerCode")
            request_data = request_data.replace("${write_borrowerCode}", borrowerCode)

        # request_data = eval(request_data)
        header = self.data.get_request_header(row_num)
        if header == 'get_cookie,31G':
            use_json = UseJson("../data_config/config_header.json")
            cookie_value = use_json.get_data("Cookie")
            header = {"Content-Type": "application/json", "Product": "31G", "Cookie": cookie_value}
        else:
            header = eval(header)
        method = self.data.get_request_method(row_num)
        url = self.data.get_request_url(row_num)
        res = self.run_method.run_main(method, url, data=request_data, header=header, params=request_data)
        return res

    def get_value_for_key(self, row):
        """获取依赖字段的响应数据,通过执行依赖测试case来获取响应数据,响应中某个字段数据作为依赖key的value"""
        depend_data = self.data.get_depend_key(row)  # 获取依赖的返回数据 key
        response_data = self.run_dependent()  # 执行依赖 case 返回结果
        get_depend_data = [match.value for match in parse(depend_data).find(response_data)][0]
        if depend_data == "accessToken":
            # get_depend_data = hashlib.md5(get_depend_data.encode(encoding='UTF-8')).hexdigest()  # md5加密
            get_depend_data = base64.b64encode(get_depend_data.encode('utf-8')).decode("utf-8")  # base64加密
            self.use_config_header(get_depend_data)
        elif depend_data == "code":
            self.use_credit_apply_code(get_depend_data)
        elif depend_data == "sessionId":
            get_depend_data = base64.b64encode(get_depend_data.encode('utf-8')).decode("utf-8")  # base64加密
            self.use_config_header(get_depend_data)

        return get_depend_data

    def md5(self):
        str = '387ff00e-1a54-49b3-87ca-a95ad3c9e08a'
        m = hashlib.md5()
        b = str.encode(encoding='utf-8')
        m.update(b)
        str_md5 = m.hexdigest()
        return str_md5

    def base64(self):
        a = "39397778-f129-41f4-ba28-68af53f5728c"
        jm = base64.b64encode(a.encode('utf-8'))
        b = str(jm)
        base = b[2:-1]
        return base

    def use_config_header(self, header_cookie):
        """将header_cookie写入config_header.json中"""
        config_header = "../data_config/config_header.json"
        with open(config_header, "w") as ch:
            ch.write('{{"Cookie": "SESSION={}"}}'.format(header_cookie))
            # ch.write('{"Cookie":' '"%s"}' % "aa")

    def use_credit_apply_code(self, code):
        credit_apply_code = "../data_config/credit_apply_code.json"
        with open(credit_apply_code, "w") as json:
            json.write('{{"code": "{}"}}'.format(code))