def save_result(self, response_to_check): if '输出' in self.expected_result.keys(): # 需要提取服务器返回内容 output = self.expected_result['输出'] if type(output) == type({}): for key in output.keys(): if key.lower() == 'dic': # 如果为字典,则用键值提取 for var_name, extrator in output[key].items(): logger.info('使用键值提取') value_get = self.extrator( 'dic', extrator, response_to_check) # 获取键对应值 logger.info('获取到的变量的值为:%s' % value_get) self.outputs_dict[var_name] = value_get logger.info( '使用“键值提取”提取的自定义变量-值(key-value对)为:%s-%s' % (var_name, self.outputs_dict[var_name])) elif key.lower() == 're': for var_name, extrator in output[key].items(): logger.info('使用正则表达式提取') value_get = self.extrator('re', extrator, response_to_check) index = 1 tmp_var_name = var_name for item in value_get: logger.info('获取到的变量的值为:%s' % value_get) var_name = var_name + '_' + str(index) if var_name in self.outputs_dict.keys( ): # 已有存在值,替换已经存在的值 self.outputs_dict[var_name] = item continue else: self.outputs_dict[var_name] = item index = index + 1 logger.info( '使用“正则表达式提取”提取的自定义变量-值(key-value对)为:%s-%s' % (var_name, self.outputs_dict[var_name])) var_name = tmp_var_name elif key.lower() == 'xpath': for var_name, extrator in output[key].items(): logger.info('使用xpath提取') value_get = self.extrator('xpath', extrator, response_to_check) logger.info('获取到的变量的值为:%s' % value_get) self.outputs_dict[var_name] = value_get logger.info( '使用“xpath提取”提取的自定义变量-值(key-value对)为:%s-%s' % (var_name, self.outputs_dict[var_name])) logger.debug('提取的变量-值(key-value对)为:%s' % self.outputs_dict) else: logger.warn('未检测到从服务器返回中提取内容的要求,请检查是否正确填写预期结果')
def save_onesql_query_result(self, sql_record): if self.expected_result and '输出' in self.expected_result.keys( ): # 需要提取服务器返回内容 output = self.expected_result['输出'] counter = 0 while counter < len(sql_record): for var_name, var_number in output.items(): var_number = int(var_number) #以防错误输入了字符串编号 temp_var_number = var_number - 1 if temp_var_number == counter: CaseStep.outputs_dict[var_name] = sql_record[ counter] # 已有存在值,替换已经存在的值 logger.debug('提取的输出结果(key-value对)为:%s-%s' % (var_name, sql_record[counter])) counter = counter + 1 logger.debug('提取的输出结果(key-value对)为:%s' % CaseStep.outputs_dict) else: logger.warn('未检测到从数据库服务器返回中提取内容的要求,请检查是否正确填写预期结果')
def save_result(self, response_to_check): if '输出' in self.expected_result.keys(): # 需要提取服务器返回内容 output = self.expected_result['输出'] if type(output) == type({}): for var_name, extrator in output.items(): if type(extrator) == type({}): # 如果为字典,获取键值 value_get = self.extrator(extrator, response_to_check) # 获取键对应值 logger.info('获取到的变量的值为:%s' % value_get) key_value = {var_name: value_get} if self.outputs_list.count( key_value): # 已有存在值,替换已经存在的值 index = self.outputs_list.index(key_value) + 1 self.outputs_list.index(index, key_value) self.outputs_list.remove(key_value) else: # 不存在值,添加到列表 self.outputs_list.append(key_value) else: # 正则表达式式提取 value_get = self.extrator(extrator, response_to_check) index = 1 for item in value_get: logger.info('获取到的变量的值为:%s' % value_get) var_name = var_name + '_' + str(index) index = index + 1 key_value = {var_name: item} if self.outputs_list.count( key_value): # 已有存在值,替换已经存在的值 index = self.outputs_list.index(key_value) + 1 self.outputs_list.insert(index, key_value) self.outputs_list.remove(key_value) else: # 不存在值,添加到列表 self.outputs_list.append(key_value) logger.info('提取的输出结果(key-value对)为:%s' % self.outputs_list) else: logger.warn('未检测到从服务器返回中提取内容的要求,请检查是否正确填写预期结果')
def save_onesql_query_result(self, sql_record): if '输出' in self.expected_result.keys(): # 需要提取服务器返回内容 output = self.expected_result['输出'] counter = 0 while counter < len(sql_record): for var_name, var_number in output.items(): var_number = int(var_number) #以防错误输入了字符串编号 temp_var_number = var_number - 1 if temp_var_number == counter: key_value = {var_name: sql_record[counter]} if self.outputs_list.count( key_value): # 已有存在值,替换已经存在的值 index = CaseStep.outputs_list.index(key_value) + 1 CaseStep.outputs_list.insert(index, key_value) CaseStep.outputs_list.remove(key_value) else: # 不存在值,添加到列表 CaseStep.outputs_list.append( key_value) # 存储变量到列表[{var_name:var_value}] counter = counter + 1 logger.info('提取的输出结果(key-value对)为:%s' % CaseStep.outputs_list) else: logger.warn('未检测到从数据库服务器返回中提取内容的要求,请检查是否正确填写预期结果')
def assert_sql_result(self, response_to_check): if self.expected_result != '': if re.findall('匹配规则', str(self.expected_result)) == []: logger.info('用户没有设置匹配规则,返回程序') return ['Pass', ''] traceback_template = '''Traceback (most recent call last): File "%(filename)s", line %(lineno)s, in %(name)s %(type)s: %(message)s\n''' if self.expected_result['匹配规则'] == '相等': # 遍历条件列表, 形如 "条件":[{"模式":"[CaseStep.device_no]=true", "消息":“登录失败,success不为true”},{"模式":"[CaseStep.serial_no]=\ "成功\"","消息":“message不为成功”}, # {"模式":"[CaseStep.serial_no]= "[CaseStep.branch_id]"},"消息":“code不为4001”},{"模式":"\"[CaseStep.branch_id]\"= "\"[CaseStep.branch_id]\""},"消息":“code不为4002”}], for item in self.expected_result['条件']: member = item['模式'] logger.info('要检测的模式为:%s' % member) value_list = member.split('=') if len(value_list) < 2: logger.warn('该条件模式[%s]填写错误,跳过对该模式的检测' % member) break msg = item['消息'] try: assert value_list[0] == value_list[1], msg except AssertionError: exc_type, exc_value, exc_traceback = sys.exc_info() traceback_details = { 'filename': exc_traceback.tb_frame.f_code.co_filename, 'lineno': exc_traceback.tb_lineno, 'name': exc_traceback.tb_frame.f_code.co_name, 'type': exc_type.__name__, 'message': exc_value } del (exc_type, exc_value, exc_traceback) traceback_exc = traceback_template % traceback_details return ['Fail', traceback_exc] return ['Pass', ''] elif self.expected_result['匹配规则'] == '不相等': for item in self.expected_result['条件']: member = item['模式'] member = member.replace('!=', '!=') # 防止用户输入中文的! logger.info('要检测的模式为:%s' % member) value_list = member.split('!=') if len(value_list) < 2: logger.warn('该条件模式[%s]填写错误,跳过对该模式的检测' % member) break msg = item['消息'] try: assert value_list[0] != value_list[1], msg except AssertionError: exc_type, exc_value, exc_traceback = sys.exc_info() traceback_details = { 'filename': exc_traceback.tb_frame.f_code.co_filename, 'lineno': exc_traceback.tb_lineno, 'name': exc_traceback.tb_frame.f_code.co_name, 'type': exc_type.__name__, 'message': exc_value } del (exc_type, exc_value, exc_traceback) traceback_exc = traceback_template % traceback_details return ['Fail', traceback_exc] return ['Pass', ''] else: logger.warn('匹配规则填写错误["匹配规则":"%s"]' % self.expected_result['匹配规则']) return [ 'Fail', '匹配规则填写错误["匹配规则":"%s"],匹配规则可选择值 相等|不相等' % self.expected_result['匹配规则'] ]