diff options
Diffstat (limited to 'keyword/common/customlibrary/Custometest/LogResponseVAL.py')
| -rw-r--r-- | keyword/common/customlibrary/Custometest/LogResponseVAL.py | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/keyword/common/customlibrary/Custometest/LogResponseVAL.py b/keyword/common/customlibrary/Custometest/LogResponseVAL.py new file mode 100644 index 0000000..dbf6474 --- /dev/null +++ b/keyword/common/customlibrary/Custometest/LogResponseVAL.py @@ -0,0 +1,203 @@ +import re +import time + +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(" ") + #获取json串中所有的key,返回列表 + responsekeys = getKeys(response) + # 判断目的条件的Key在数据中是否存在 + if target[0] in responsekeys: + if len(target) != 1: + #targetkey 判断的字段 + targetkey = target[0] + # 判断条件 + 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] + torf=is_valid_date(responsevalue) + if torf == True: + timeArray = time.strptime(responsevalue, "%Y-%m-%d %H:%M:%S") + timeStamp = str(int(time.mktime(timeArray))) + p = conditional(conditions, timeStamp, targetkey, sum, targetvalue) + if p != "": + strlist.append(p) + else: + p = conditional(conditions, responsevalue, targetkey, sum, targetvalue) + if p != "": + strlist.append(p) + elif len(target) == 2: + p = conditional(conditions, responsevalue, targetkey, sum) + if p != "": + strlist.append(p) + else: + str2 = "返回数据第" + str(sum) + "组数据中不存在该字段:" + target[0] + print(str2) + strlist.append(str2) + sum += 1 + else: + str3 = "返回数据中无数据" + strlist.append(str3) + Assertresults(strlist) + return strlist + +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 + "的值与和条件不符" + + if conditions == "!=": + if value == value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + print(str1) + + if conditions == ">": + if int(value2) <= int(value): + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "<": + if int(value2) >= int(value): + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == ">=": + if int(value2) < int(value): + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "<=": + if int(value2) > int(value): + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "in": + value = value.split(",") + if value2 not in value: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "notin": + value = value.split(",") + if value2 in value: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "like": + left = value[0] + right = value[-1] + if left == "%" and right == "%": + value = value[1:len(value) - 1] + if value not in value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + elif left == "%" and right != "%": + v = len(value) + _value = value[1:] + _value2 = value2[-(v - 1):] + print(_value, _value2) + if _value != _value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + elif left != "%" and right == "%": + v = len(value) + _value = value[0:-1] + _value2 = value2[0:v - 1] + if _value != _value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "notlike": + left = value[0] + right = value[-1] + if left == "%" and right == "%": + value = value[1:len(value) - 1] + if value in value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + elif left == "%" and right != "%": + v = len(value) + _value = value[1:] + _value2 = value2[-(v - 1):] + if _value == _value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + elif left != "%" and right == "%": + v = len(value) + _value = value[0:-1] + _value2 = value2[0:v - 1] + if _value == _value2: + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + if conditions == "notEmpty": + if value2 == "": + str1 = "返回数据第" + str(sum) + "组数据中," + targetkey + "的值与和条件不符。" + + 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 + + +def is_valid_date(strdate): + '''判断是否是一个有效的日期字符串''' + a = re.findall(":", strdate) + b = re.findall("-", strdate) + if len(a) ==2 and len(b) == 2: + return True + else: + return False + |
