diff --git a/01-TestCase/tsg_ui/ui_settings/zResponsepagesCase.robot b/01-TestCase/tsg_ui/ui_settings/zResponsepagesCase.robot index 591d2e3..ad1741d 100644 --- a/01-TestCase/tsg_ui/ui_settings/zResponsepagesCase.robot +++ b/01-TestCase/tsg_ui/ui_settings/zResponsepagesCase.robot @@ -35,13 +35,13 @@ logtest ${EndTime1} Get Substring ${EndTime} \ -4 ${StartTime} add time to date ${EndTime} -01:00:00 ${StartTime1} Get Substring ${StartTime} \ -4 - ${filter} Set Variable common_log_id=238347398589845504 OR common_log_id=238347398522859520 + ${filter} Set Variable common_log_id=238734003578214400 ${responsebody} loglistverify ${Logurl} ${Schemaurl} ${token} ${StartTime1} ${EndTime1} ${logType} ${filter} - ${targetdict} create list common_log_id = 237434463277422592 + ${targetdict} create list common_log_id = 238734003578214400 log ${responsebody} ${strlist} FieldValidation ${responsebody} ${targetdict} log ${strlist} - Assertresults ${strlist} + # Assertresults ${strlist} diff --git a/04-CustomLibrary/Custometest/LogResponseVAL.py b/04-CustomLibrary/Custometest/LogResponseVAL.py index 72776a6..da23e1d 100644 --- a/04-CustomLibrary/Custometest/LogResponseVAL.py +++ b/04-CustomLibrary/Custometest/LogResponseVAL.py @@ -1,51 +1,90 @@ - - +import jsonpath +# 1.说明:本方法用于对日志接口返回数据中的字段和数据进行判断 +# 2.传入数据说明:responsedict - 接口返回数据的json数据 +# targetlist - 判断参数 ,可传入多个条件,在robotfromwork中条件以Tab分隔 。参数格式为: 字段(key)_判断条件_数据(value) ,一个条件内各参数以空格分隔 例:common_log_id = 238734003578214400 +# 判断条件包括:= 、 != 、> 、< 、>= 、<= 、in 、notin 、like、 notlike 、notEmpty 、 empty 。 +# (1)其中notin、notlike、notEmpty传入时中间无空格 +# (2)notEmpty、empty 不传数据(value) +# (3) in 、notin 多个字段以逗号分隔 例 : common_log_id notin 238734003578214400,238734003578214402,238734003578214403 def FieldValidation(responsedict, targetlist): responselist = responsedict["data"]["list"] strlist = [] if responselist: - #循环返回数据列表 + # 循环返回数据列表 sum = 1 for response in responselist: # 循环目地列表 for t in targetlist: - #将目的根据空格分割成列表 (“key值”,“判断条件”,“value值”) - target=t.split(" ") - print("target",target) - #判断目的条件的Key在数据中是否存在 - if target[0] in response: + # 将目的根据空格分割成列表 (“key值”,“判断条件”,“value值”) + target = t.split(" ") + #获取json串中所有的key,返回列表 + responsekeys = getKeys(response) + # 判断目的条件的Key在数据中是否存在 + if target[0] in responsekeys: + #targetkey 判断的字段 targetkey = target[0] - #判断条件 - conditions=target[1] - #目的Value - # 对应Key返回数据的Value - responsevalue = response[target[0]] - print(targetkey,conditions,responsevalue) - print(len(target)) - if len(target) == 3: - targetvalue=target[2] - p=conditional(conditions,responsevalue,targetkey,sum,targetvalue) - strlist.append(p) - elif len(target) == 2: - p=conditional(conditions, responsevalue, targetkey, sum) - strlist.append(p) + # 判断条件 + conditions = target[1] + # 返回数据中对应key的Value列表 + responsevaluelist = getjsonvalue(response,target[0]) + for responsevalue in responsevaluelist: + #判断value值是否为列表,转化为字符串 + if isinstance(responsevalue, list): + responsevalue=str(responsevalue) + if len(target) == 3: + targetvalue = target[2] + p = conditional(conditions, responsevalue, targetkey, sum, targetvalue) + strlist.append(p) + elif len(target) == 2: + p = conditional(conditions, responsevalue, targetkey, sum) + strlist.append(p) else: str2 = "返回数据第" + str(sum) + "组数据中不存在该字段:" + target[0] print(str2) strlist.append(str2) - sum+=1 + sum += 1 else: str3 = "返回数据中无数据" strlist.append(str3) - + Assertresults(strlist) return strlist -def conditional(conditions,value2,targetkey,sum,value=None): - str1="" +def getjsonvalue(json_data, key_name): + '''获取到json中任意key的值,结果为list格式''' + keyvalue = jsonpath.jsonpath(json_data, '$..{key_name}'.format(key_name=key_name)) + # key的值不为空字符串或者为empty(用例中空固定写为empty)返回对应值,否则返回empty + return keyvalue + +def getKeys(data): + # 获取json串中所有的key + keysAll_list = [] + def getkeys(data): # 遍历json所有key + if (type(data) == type({})): + keys = data.keys() + for key in keys: + value = data.get(key) + if (type(value) != type({}) and type(value) != type([])): + keysAll_list.append(key) + elif (type(value) == type({})): + keysAll_list.append(key) + getkeys(value) + elif (type(value) == type([])): + keysAll_list.append(key) + for para in value: + if (type(para) == type({}) or type(para) == type([])): + getkeys(para) + else: + keysAll_list.append(para) + getkeys(data) + return keysAll_list + +# 对传入的数据根据条件进行判断 +def conditional(conditions, value2, targetkey, sum, value=None): + str1 = "" if conditions == "=": if value != value2: - str1= "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符" + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符" if conditions == "!=": if value == value2: @@ -69,35 +108,35 @@ def conditional(conditions,value2,targetkey,sum,value=None): str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" if conditions == "in": - value=value.split(",") - if value2 not in value: + value = value.split(",") + if value2 not in value: str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" if conditions == "notin": - value=value.split(",") - if value2 in value: + value = value.split(",") + if value2 in value: str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" if conditions == "like": - left= value[0] - right=value[-1] + left = value[0] + right = value[-1] if left == "%" and right == "%": - value=value[1:len(value)-1] - if value not in value2: + value = value[1:len(value) - 1] + if value not in value2: str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" elif left == "%" and right != "%": - v=len(value) + v = len(value) _value = value[1:] - _value2 = value2[-(v-1):] - print(_value,_value2) + _value2 = value2[-(v - 1):] + print(_value, _value2) if _value != _value2: str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" elif left != "%" and right == "%": - v=len(value) + v = len(value) _value = value[0:-1] - _value2=value2[0:v-1] + _value2 = value2[0:v - 1] if _value != _value2: str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" @@ -112,7 +151,7 @@ def conditional(conditions,value2,targetkey,sum,value=None): elif left == "%" and right != "%": v = len(value) _value = value[1:] - _value2 = value2[-(v-1):] + _value2 = value2[-(v - 1):] if _value == _value2: str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" @@ -130,12 +169,10 @@ def conditional(conditions,value2,targetkey,sum,value=None): if conditions == "Empty": if value2 != "": str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" - return str1 def Assertresults(resultslist): print(resultslist) for i in resultslist: if i != "": - assert 1==2 - \ No newline at end of file + assert 1 == 2