def __init__(self): super().__init__() # dependyaml文件与sql执行结果yaml文件 self.yamlKey = OperationYaml('dependKeyInfo.yaml') self.yamlsqlExecute = OperationYaml('sqlExecuteInfo.yaml') self.opera_excle = OperationExcle() # 创建实例对象dependkey与dependFiled与yaml配置里面的key self.dependKey = '' self.dependField = '' self.dependKeyYaml = '' self.sqlExecuteResult = '' # '''定义header类型''' self.No_auth_headerFlag = self.yaml['No_auth_headerFlag'] #无需token认证 self.crm_headerFlag = self.yaml['crm_headerFlag'] # crm self.fwh_headerFlag = self.yaml['fwh_headerFlag'] # 服务号微信端 self.fwh_admin_headerFlag = self.yaml['fwh_admin_headerFlag'] # 服务号后台 # 定义content_type self.content_type_json = self.yaml['content_type_json'] self.content_type_form_urlencoded = self.yaml[ 'content_type_form_urlencoded'] self.content_type_text = self.yaml['content_type_text'] self.content_type_form_data = self.yaml['content_type_form_data'] self.content_type_form_data_boundary = self.yaml[ 'content_type_form_data_boundary'] self.content_type_xml = self.yaml['content_type_xml'] #存放sql self.sql = None self.writelist = []
def __init__(self, case_id): super().__init__() # 实例依赖字段yaml文件 self.yamlField = OperationYaml('dependFieldInfo.yaml') # 数据依赖字段实例 self.dependFieldYaml = '' self.case_id = case_id self.data = GetData() self.depend_key_dict = {} #存放依赖key数据
def __init__(self, path, file_path): super().__init__() self.send_mail = SendEmail() #邮件发送 self.gld_mongo = Mongo_gld() self.gldexp_mongo = Mongo_gldexp() self.gld_mysql = Mysql_gld() self.gldexp_mysql = Mysql_gldexp() # 默认读取config,此文件只读不写 self.yaml = OperationYaml(path=path, file_path=file_path)
def __init__(self): self.requestDataDispose = operationRequestData() #请求数据处理 self.mylog = MyLog.get_log() #日志 self.log = self.mylog.get_logger() #获取日志创建对象 self.run_method = RunMethod() #通过请求封装 self.com_assert = CommonAssert() #通用预期与实际结果判断 self.send_mail = SendEmail() #邮件发送 self.json = OperationJson() # 默认读取config,此文件只读不写 self.yaml = OperationYaml().readDataForKey('config')
def __init__(self): config = OperationYaml().read_data() self.dataBaseConfig = config['config']
def setup(self): yaml = OperationYaml(file_path='devices_caps_ApiDemos.yaml') caps = yaml.readforKey_onetier('caps') self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(10) # 添加隐式等待
def __init__(self): Config = OperationYaml().read_data() self.config = Config['config']
def get_data(file_path='Commands.yaml'): yaml = OperationYaml(file_path=file_path) commands = yaml.read_data() return commands
def __init__(self): config = OperationYaml(file_path='Config.yaml').read_data() self.dataBaseConfig = config['config']
class GetData(ToolALL): def __init__(self): super().__init__() # dependyaml文件与sql执行结果yaml文件 self.yamlKey = OperationYaml('dependKeyInfo.yaml') self.yamlsqlExecute = OperationYaml('sqlExecuteInfo.yaml') self.opera_excle = OperationExcle() # 创建实例对象dependkey与dependFiled与yaml配置里面的key self.dependKey = '' self.dependField = '' self.dependKeyYaml = '' self.sqlExecuteResult = '' # '''定义header类型''' self.No_auth_headerFlag = self.yaml['No_auth_headerFlag'] #无需token认证 self.crm_headerFlag = self.yaml['crm_headerFlag'] # crm self.fwh_headerFlag = self.yaml['fwh_headerFlag'] # 服务号微信端 self.fwh_admin_headerFlag = self.yaml['fwh_admin_headerFlag'] # 服务号后台 # 定义content_type self.content_type_json = self.yaml['content_type_json'] self.content_type_form_urlencoded = self.yaml[ 'content_type_form_urlencoded'] self.content_type_text = self.yaml['content_type_text'] self.content_type_form_data = self.yaml['content_type_form_data'] self.content_type_form_data_boundary = self.yaml[ 'content_type_form_data_boundary'] self.content_type_xml = self.yaml['content_type_xml'] #存放sql self.sql = None self.writelist = [] # 获取caseID def get_caseId(self, row): col = int(data_config.get_case_id()) caseId = self.opera_excle.get_cell_value(row, col) if caseId == 'ID': return None else: return caseId # 获取excle行数,就是用例数 def get_case_line(self): line_num = self.opera_excle.get_lines() return line_num # 获取是否执行 @args_None def get_is_run(self, row: object) -> object: flag = None col = int(data_config.get_run()) run_model = self.opera_excle.get_cell_value(row, col) if run_model == 'yes': flag = True elif run_model == '是否执行': pass else: flag = False return flag @args_None # 获取header def getHeader(self, row): col = int(data_config.get_header()) header = self.opera_excle.get_cell_value(row, col) return header @args_None # 确定header类型 def getHeaderType(self, row): header = self.getHeader(row) header_OutputCase = self.requestDataDispose.strOutputCase(header) flag = None if self.No_auth_headerFlag in header_OutputCase: flag = self.No_auth_headerFlag return flag elif self.crm_headerFlag in header_OutputCase: flag = self.crm_headerFlag return flag elif self.fwh_headerFlag in header_OutputCase: flag = self.fwh_headerFlag return flag elif self.fwh_admin_headerFlag in header_OutputCase: flag = self.fwh_admin_headerFlag return flag else: flag = None return flag # 获取content_type的值 def getContentType(self, row): col = int(data_config.get_content_type()) content_type = self.opera_excle.get_cell_value(row, col) return content_type def ContentTypeData(self, row): content_type = self.getContentType(row) content_type_OutputCase = self.requestDataDispose.strOutputCase( content_type) header_content_type = None # 请求数据格式为json时使用 if [i for i in self.content_type_json if i in content_type_OutputCase]: header_content_type = self.yaml['headers_json'] # 请求数据格式为浏览器原生时使用 elif [ i for i in self.content_type_form_urlencoded if i in content_type_OutputCase ]: header_content_type = self.yaml['headers_form_urlencoded'] # 请求数据格式为text时使用,响应一般返回html elif [ i for i in self.content_type_text if i in content_type_OutputCase ]: header_content_type = self.yaml['headers_text'] # 用于文件上传时与文件上传时有附加参数时使用,header为空 elif [ i for i in self.content_type_form_data_boundary if i in content_type_OutputCase ]: header_content_type = self.yaml['headers_form_data_boundary'] # 用于form-data表单提交时使用 elif [ i for i in self.content_type_form_data if i in content_type_OutputCase ]: header_content_type = self.yaml['headers_form_data'] # 请求数据格式为xml时使用 elif [ i for i in self.content_type_xml if i in content_type_OutputCase ]: header_content_type = self.yaml['headers_xml'] else: print("暂不支持的content_type") header_content_type = None self.log.info(self.mylog.out_varname(header_content_type)) return header_content_type @args_None # 输出header def headerData(self, row): header_flag = self.getHeaderType(row) header_content_type = self.ContentTypeData(row) if isinstance(header_content_type, dict): if header_flag == self.No_auth_headerFlag: # 不需要token时得header notoken_headers = data_config.get_header_no_auth() header_content_type.update(notoken_headers) return header_content_type elif header_flag == self.crm_headerFlag: # CRM header crm_headers = data_config.get_crm_token() header_content_type.update(crm_headers) return header_content_type elif header_flag == self.fwh_headerFlag: # 服务号header fwhc_headers = data_config.get_fwh_token() header_content_type.update(fwhc_headers) return header_content_type elif header_flag == self.fwh_admin_headerFlag: # 服务号后台header fwh_admin_header = data_config.get_fwhadmin_cookie() header_content_type.update(fwh_admin_header) return header_content_type else: self.log.error('error:header填写错误') return None return None # 获取请求url名称 @args_None def get_request_name(self, row): col = int(data_config.get_request_name()) request_name = self.opera_excle.get_cell_value(row, col) if request_name: # print('请求url:' + request_name) return request_name else: return None # 获取请求方式 @args_None def get_request_method(self, row): col = int(data_config.get_request_method()) request_method = self.opera_excle.get_cell_value(row, col) return request_method # 获取URL @args_None def get_url(self, row): col = int(data_config.get_url()) url = self.opera_excle.get_cell_value(row, col) headerFlag = self.getHeaderType(row) if 'http://' in url[:8] or 'https://' in url[:8]: url = url elif headerFlag == self.crm_headerFlag: url = self.yaml['crm_test_api'] + url # print(1,url) elif headerFlag == self.fwh_headerFlag: url = self.yaml['fwh_test_api'] + url # print(2, url) elif headerFlag == self.fwh_admin_headerFlag: url = self.yaml['fwh_admin_test_api'] + url # print(3, url) else: url = url # print(4, url) return url # 获取请求数据 @args_None def get_request_data(self, row): col = int(data_config.get_data()) request_data = self.opera_excle.get_cell_value(row, col) return request_data # 更改请求数据类型,并输出最终得请求数据 @args_None def requestData(self, row): '''根据每一行的内容中的header值来判断是哪个系统的接口并使用特定的方法完成对请求数据的处理''' header_flag = self.getHeaderType(row) request_data = self.get_request_data(row) try: if header_flag in [ self.crm_headerFlag, self.fwh_admin_headerFlag, self.fwh_headerFlag ]: # 处理crm接口处理 if request_data: # 请求参数不为空 _request_data = self.requestDataDispose.requestToDict( request_data) return _request_data else: return None else: # 其余情况当做json格式处理 json_data = demjson.encode(request_data) return json_data except IndexError as indexError: self.log.error(self.mylog.out_varname(indexError)) # print(indexError) return None def get_joinFiles(self, row): col = int(data_config.get_joinFiles()) joinFiles = self.opera_excle.get_cell_value(row, col) # ''' 替换双引号为单引号,避免由于引号问题出现断言失败''' return joinFiles # 获取关联得文件信息 def joinFilesData(self, row): joinFiles = self.get_joinFiles(row) # print(eval(joinFiles)) if not joinFiles: return None if joinFiles[0] in ('(', '[', '{') and joinFiles[-1] in (')', ']', '}'): try: joinFileData = self.requestDataDispose.out_join_files( tuple_in=eval(joinFiles)) # print(joinFileData) if joinFileData: return joinFileData else: return None except BaseException as e: print(e) return None else: return None # 处理请求 def request_info(self, row): response = None request_name = self.get_request_name(row) self.log.info(self.mylog.out_varname(request_name)) url = self.get_url(row) self.log.info(self.mylog.out_varname(url)) method = self.get_request_method(row) self.log.info(self.mylog.out_varname(method)) request_data = self.requestData(row) self.log.info(self.mylog.out_varname(request_data)) header = self.headerData(row) self.log.info(self.mylog.out_varname(header)) # 获取关联得文件信息 files = self.joinFilesData(row) self.log.info(self.mylog.out_varname(files)) if files: response = self.run_method.run_main(method=method, url=url, data=request_data, headers=header, files=files, res_format='json') else: response = self.run_method.run_main(method=method, url=url, data=request_data, headers=header, res_format='json') return response # 获取预期结果 @args_None def get_expect_data(self, row): col = int(data_config.get_expect()) expect = self.opera_excle.get_cell_value(row, col) # ''' 替换双引号为单引号,避免由于引号问题出现断言失败''' return expect # # 根据sql获取预期结果 @args_None def get_sql_expect_data(self, row, col=None): # '''根据header确认数据库配置''' #''' 根据传入的列以确定数据读取、写入的位置''' if col is None: col = int(data_config.get_expect()) else: col = col sql = self.opera_excle.get_cell_value(row, col) self.sql = sql if sql: header_flag = self.getHeaderType(row) if header_flag == self.crm_headerFlag: # CRM header try: sql_except = self.tencent_cloud.sql_operation_limit(sql) except TypeError as error: sql_except = None self.log.error(self.mylog.out_varname(error)) elif header_flag==self.fwh_headerFlag or \ header_flag==self.fwh_admin_headerFlag: # 服务号 、服务号后台sql配置 try: sql_except = self.tencent_cloud.sql_operation_limit(sql) except TypeError as error: sql_except = None self.log.error(self.mylog.out_varname(error)) else: try: sql_except = self.tencent_cloud.sql_operation_limit(sql) except TypeError as error: sql_except = None self.log.error(self.mylog.out_varname(error)) return sql_except else: return False # '''根据传入的sql_data确定是否需要执行sql操作''' def get_sqlFlag(self, row, sql_data): select_str = 'select' insert_str = 'INSERT' update_str = 'UPDATE' delete_str = 'DELETE' # '''大写sql''' sql_falg_upper = [ i.upper() for i in [select_str, insert_str, update_str, delete_str] ] # '''小写sql''' sql_falg_lower = [i.lower() for i in sql_falg_upper] if sql_data: if (sql_data[:len(select_str)].upper() in sql_falg_upper) or \ (sql_data[:len(select_str)].lower() in sql_falg_lower): return True else: return False else: return False #确定最终获取预期结果的方式 @args_None def expectData(self, row): expect = self.get_expect_data(row) expect_falg = self.get_sqlFlag(row, expect) # print(self.mylog.out_varname(expect_falg)) if expect_falg: expect = self.get_sql_expect_data(row) # print(self.mylog.out_varname(expect)) return expect # 写入实际结果 def write_result(self, row, value): col = int(data_config.get_result()) write_row_col_value = '{}:{}:{}'.format(row, col, value) self.log.info(self.mylog.out_varname(write_row_col_value)) self.opera_excle.write_value(row, col, value) # # #保存 # self.opera_excle.save_workbook('DataCase_ALL_result.xlsx') self.opera_excle.save_workbook() # 获取实际结果 @args_None def get_result(self, row): col = int(data_config.get_result()) result = self.opera_excle.get_cell_value(row, col) self.log.info(self.mylog.out_varname(result)) return result # 判断是否有case的依赖 @args_None def is_depend(self, row): col = int(data_config.get_case_dapend()) max_line = self.get_case_line() depend_case_id = self.opera_excle.get_cell_value(row, col) if depend_case_id: try: depend_case_id = int(depend_case_id) if depend_case_id >= max_line: return None else: return depend_case_id except TypeError as typeerror: # print(typeerror) self.log.error(self.mylog.out_varname(typeerror)) return None else: return None # 获取依赖数据的key关键字 @args_None def depentKey_kw(self, row): # '''写入数据后重新获取的值未更新: # 原因:self.data在实例化的时候就已经生成,所以要使写入的值立马生效有两个方式: # 1.读取之前在实例化对象 # 2.读取数据的方式改成不用实例,直接用实例方法 col = int(data_config.get_key_depend()) excleDepentKey = self.opera_excle.get_cell_value(row, col) self.dependKeyYaml = '{}{}'.format(self.yaml['dependKey'], row) if not excleDepentKey: # '''通过yaml文件获取depend''' if self.dependKeyYaml in self.opera_excle.write_list: return self.dependKeyYaml else: return False else: return excleDepentKey # 根据依赖key关键字返回依赖key数据 def get_depent_key(self, row): dependCaseFlag = self.is_depend(row) if dependCaseFlag: self.write_dependKey(row) depend_key = self.depentKey_kw(row) depend_key_data = self.yamlKey.readforKey_onetier(key=depend_key) if depend_key_data: return depend_key_data else: return False else: return False # 写入数据依赖key:可能不准确,按特定规则生成的,需手动确认 def write_dependKey(self, row): str_in = self.get_request_data(row) # 获取配置中的dependKey完成dependKey字符拼接 self.dependKey = '{}{}'.format(self.yaml['dependKey'], row) self.dependField = '{}{}'.format(self.yaml['dependField'], row) headerFlag = self.getHeaderType(row) join_str = '' #依赖key生成固定拼接字符串 if headerFlag == self.fwh_admin_headerFlag: join_str = self.yaml['fwhadmin_dependKey_joinstr'] # 特殊处理服务号获取用户信息接口 elif (self.yaml['fwh_test_api'] + '/index/user/info' in self.get_url(self.get_caseId(row))): join_str = self.yaml['fwh_user_dependKey_joinstr'] else: join_str = self.yaml['rule_dependKey_joinstr'] if str_in: dependKeyInfo = self.requestDataDispose.denpendKeyGenerate( str_in, join_str=join_str) if dependKeyInfo: dependKey_dict = {} # 临时存放dependKey dependKey_dict[self.dependKey] = dependKeyInfo # '''这句代码用于处理yaml写入失败(浮点对象异常的问题) value = eval(demjson.encode(dependKey_dict)) # '''写入数据至yaml''' # 将数据写进实例 self.opera_excle.writeDatasObject(self.dependKey) self.opera_excle.writeDatasObject(self.dependField) self.writelist = self.opera_excle.write_list # print(self.opera_excle.write_list) # print(self.writelist) self.yamlKey.write_yaml(value) return True else: return False else: return False #通过数据依赖字段将所需字段的值写入请求参数 def writeDependFiledToRequestData(self, row, source_data=None): try: questdata_col = int(data_config.get_data()) dependKey_col = int(data_config.get_key_depend()) dependField_col = int(data_config.get_field_depend()) str_in = self.get_request_data(row) requestDataDepend = self.requestDataDispose.requestDataDepend( source_data, str_in) # print(self.mylog.out_varname(requestDataDepend)) self.log.info(self.mylog.out_varname(requestDataDepend)) if requestDataDepend: requestDataDepend = str(requestDataDepend) # print('requestDataDepend',requestDataDepend) self.opera_excle.writeDatasObject(requestDataDepend) self.opera_excle.write_value(row, dependKey_col, self.dependKey) self.opera_excle.write_value(row, dependField_col, self.dependField) self.opera_excle.write_value(row, questdata_col, requestDataDepend) # 写完数据保存操作 # self.opera_excle.save_workbook('DataCase_ALL1.xlsx') return True else: return False except BaseException as error: self.log.error(self.mylog.out_varname(error)) print(error) return False # 获取sql语句 def get_sqlStatementData(self, row): col = int(data_config.get_sql_statement()) # '''调用sql_expect_data赋值self.sql,并返回sql执行结果''' data = self.get_sql_expect_data(row, col) if data: sql_flag = self.get_sqlFlag(row, self.sql) if sql_flag: return data else: return False else: return False # 获取sql执行结果 def get_sqlExecuteResult(self, row): sql_value = self.get_sqlStatementData(row) # print(sql_value) if sql_value: self.write_sqlExecuteResult(row, sql_value) col = int(data_config.get_sql_execute_result()) sql_value = self.opera_excle.get_cell_value(row, col) self.sqlExecuteResult = self.yamlsqlExecute.readforKey_onetier( sql_value) if self.sqlExecuteResult: return self.sqlExecuteResult else: return False else: return False # 将sql执行结果写入yaml与excle def write_sqlExecuteResult(self, row, sql_value): col = int(data_config.get_sql_execute_result()) self.log.info(self.mylog.out_varname(sql_value)) if sql_value: kw_str = 'FROM' re_str = '%s\s+(\w+)\s+' % (kw_str.lower()) if self.sql: table_str = re.search(re_str, self.sql) if not table_str: re_str = '%s\s?(\w+)\s' % (kw_str.upper()) table_str = re.search(re_str, self.sql) table_str = table_str.group() # 匹配出表名,并作为依赖key的一部分 table_name = table_str[len(kw_str):].strip() # 获取sql执行关键字并组装 sqlExecuteResult = '{}{}_{}'.format( self.yaml['sqlExecuteResult'], row, table_name) self.log.info(self.mylog.out_varname(sqlExecuteResult)) SqlExecute_dict = {} # '''这句代码用于处理yaml写入失败(浮点对象异常的问题) value = eval(demjson.encode(sql_value)) SqlExecute_dict[sqlExecuteResult] = value # '''写入数据至excle与yaml''' # self.opera_excle.writeDatasObject(self.sqlExecuteResult) self.yamlsqlExecute.write_yaml(SqlExecute_dict) self.opera_excle.write_value(row, col, sqlExecuteResult) # 保存 self.opera_excle.save_workbook() return True else: return False else: return False def write_sqlExecuteResultToRequestData(self, row): source_data = self.get_sqlExecuteResult(row) # print('source_data',source_data) self.log.info(self.mylog.out_varname(source_data)) if source_data: # 将获取出来的sql执行结果写入请求数据 result = self.writeDependFiledToRequestData( row, source_data=source_data) # print(self.mylog.out_varname(result)) if result: return True else: return False else: return False
def __init__(self): config = OperationYaml().read_data() self.fwhConfig = config['config'] self.log = logs()
class DependentData(GetData): def __init__(self, case_id): super().__init__() # 实例依赖字段yaml文件 self.yamlField = OperationYaml('dependFieldInfo.yaml') # 数据依赖字段实例 self.dependFieldYaml = '' self.case_id = case_id self.data = GetData() self.depend_key_dict = {} #存放依赖key数据 #执行依赖测试,并返回结果 def run_dependent(self): row_num = self.opera_excle.get_row_num(self.case_id) depend_response = self.data.request_info(row_num) self.log.info(self.mylog.out_varname(depend_response)) return depend_response #根据依赖key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self, row): self.depend_data_dict = {} #执行前清空dict yamlDepentKey = self.data.get_depent_key(row) # print(yamlDepentKey) depend_Field_dict = {} #数据依赖字段 # response_data为依赖测试的执行结果 response_data = self.run_dependent() # print(response_data) try: # ''' 判断depend_data使用eval是否发生异常,如果异常当做单字符处理, # 如果没异常判断是否是list且是否为空,满足条件循环处理,否则不处理''' if isinstance(response_data, str): response_data = eval(response_data) if isinstance(yamlDepentKey, list) and yamlDepentKey: for i in yamlDepentKey: # print(i) # print(self.depend_key_dict) self.depend_data_parse(i, response_data) else: return None except SyntaxError as syntaxerror: print(syntaxerror) self.log.error(self.mylog.out_varname(syntaxerror)) self.depend_data_parse(yamlDepentKey, response_data) excleDepentKey = self.dependFiel_kw(row) depend_Field_dict[excleDepentKey] = self.depend_key_dict # print(self.depend_key_dict) return depend_Field_dict def depend_data_parse(self, depend_key, response_data): '''处理依赖''' if depend_key: # 定义要获取的key # 处理依赖时,只取响应中的第一个值 __dict = self.data.requestDataDispose.depend_data_parse( depend_key, response_data, index='one') # 合并字典 # 确保字典不能为空与类型必须时字典 if __dict and isinstance(__dict, dict): self.depend_key_dict.update(__dict) # 返回数据依赖字段 def dependFiel_kw(self, row): col = int(data_config.get_field_depend()) depend_field = self.opera_excle.get_cell_value(row, col) self.dependFieldYaml = '{}{}'.format(self.yaml['dependField'], row) # print('self.data.writelist',self.data.writelist) if not depend_field: # 判断field关键字是否存在存实例中 if self.dependFieldYaml in self.data.writelist: return self.dependFieldYaml else: return False else: return depend_field # 获取数据依赖字段 def get_depend_field(self, row): self.write_dependField(row) depend_field = self.dependFiel_kw(row) depend_field_data = self.yamlField.readforKey_onetier(key=depend_field) # print(depend_field) # print(depend_field_data) if depend_field_data: return depend_field_data else: return False # 写入数据依赖字段信息至yaml def write_dependField(self, row): try: dependFieldYaml = self.get_data_for_key(row) if dependFieldYaml: # '''这句代码用于处理yaml写入失败(浮点对象异常的问题) value = eval(demjson.encode(dependFieldYaml)) # print(value) self.yamlField.write_yaml(value) return True else: return False except BaseException as error: print(error) self.log.error(self.mylog.out_varname(error)) return False # 将依赖处理完毕的请求数据写入excle def writeDependRequestDataToExcle(self, row): source_data = self.get_depend_field(row) # print(self.mylog.out_varname(source_data)) falg = None if source_data: falg = self.data.writeDependFiledToRequestData( row, source_data=source_data) if falg: return True else: return False else: return False