def storeResultVariable(command): """ {"desc":"保存结果locator到name","alias":"保存结果","valueDesc":{"locator":"json路径","name":"参数名"},"pattern":"保存结果(?P<locator>.*)为参数(?P<name>.*)"} """ valueMap = command["value"] resultMap = command["result"] paramName = valueMap["name"].strip() locator = valueMap["locator"].strip() paramValue = "" try: if not locator or len(locator) == 0: value = resultMap["result"] else: value = json.get_value(resultMap["result"], locator) if isinstance(value, (dict, list)): paramValue = value elif isinstance(value, (str, int, float)): paramValue = str(value) elif value is None: paramValue = "null" else: paramValue = value except Exception as e: logging.error(traceback.format_exc()) command["error"] = str(e) raise ExecutionFailException(command["error"]) storedVariableMap = {} if "storedVariable" in resultMap.keys(): storedVariableMap = resultMap["storedVariable"] storedVariableMap[paramName] = paramValue resultMap["storedVariable"] = storedVariableMap resultMap["message"] = "保存结果值" + str(paramValue) + "到变量" + paramName
def replaceResult(value,result): start="\$\{" end="}" it = re.finditer(r"%s(.*?)%s" % (start,end),value) for match in it: for group in match.groups(): paramValue=str(json.get_value(result,group)) value=re.sub(r"%s%s%s" % (start,group,end), paramValue, value) return value
def assertResultFormat(command): """ {"desc":"验证结果符合预期json格式","alias":"验证结果格式","valueDesc":{"expected":"预期格式","locator":"json提取表达式"},"pattern":"验证结果(?P<locator>.*)符合(?P<expected>.*)"} """ valueMap = command["value"] resultMap = command["result"] expected = valueMap["expected"] locator = valueMap["locator"].strip() check = valueMap["check"] if "check" in valueMap.keys() else False if not locator or len(locator) == 0: try: json.assertMatch(resultMap["result"], expected) resultMap["message"] = "实际值符合预期" + str(expected) except Exception as e: command["error"] = str(e) if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) else: actual = "" errorList = [] try: actual = json.get_value(resultMap["result"], locator) except Exception as err: command["error"] = err if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) try: json.assertMatch(actual, expected, locator) resultMap["message"] = locator + "实际值符合预期" + str(expected) except Exception as e: errorList.append(str(e)) if len(errorList) > 0: if len(errorList) > 1: command["error"] = json.dumps(errorList) else: command["error"] = errorList[0] if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"])
def assertResultEquals(command): """ {"desc":"验证结果相等","alias":"验证结果相等","valueDesc":{"expected":"预期值","locator":"json提取表达式"},"pattern":"验证结果(?P<locator>.*)是(?P<expected>.*)"} """ valueMap = command["value"] resultMap = command["result"] expected = str(valueMap["expected"]).strip() locator = valueMap["locator"].strip() check = valueMap["check"] if "check" in valueMap.keys() else False if not locator or len(locator) == 0: if not equals(expected, resultMap["result"]): command["error"] = "实际结果是(" + resultMap["result"] + ")与预期不一致" if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = "实际返回值符合预期结果" + expected else: actual = "" errorList = [] try: actual = json.get_value(resultMap["result"], locator) except Exception as err: command["error"] = err if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) if not equals(expected, actual): errorList.append("locator为%s的结果值为%s,不符合预期值%s" % (locator, actual, expected)) if len(errorList) > 0: if len(errorList) > 1: command["error"] = json.dumps(errorList) else: command["error"] = errorList[0] if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = locator + "的实际值符合预期结果" + expected
def assertResultMatch(command): """ {"desc":"验证结果符合正则表达式","alias":"验证结果符合正则","valueDesc":{"regexp":"表达式","locator":"json提取表达式"},"pattern":"验证结果(?P<locator>.*)匹配(?P<regexp>.*)"} """ valueMap = command["value"] resultMap = command["result"] regexp = str(valueMap["regexp"]).strip() locator = valueMap["locator"].strip() check = valueMap["check"] if "check" in valueMap.keys() else False if not locator or len(locator) == 0: if not re.fullmatch(regexp, resultMap["result"]): command["error"] = "实际结果是(" + resultMap["result"] + ")与预期不一致" if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = "实际返回值符合预期正则" + regexp else: actual = "" errorList = [] try: actual = json.get_value(resultMap["result"], locator) except Exception as err: command["error"] = err if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) if not re.fullmatch(regexp, str(actual)): errorList.append("locator为%s的结果值为%s,不符合预期正则%s" % (locator, actual, regexp)) if len(errorList) > 0: if len(errorList) > 1: command["error"] = json.dumps(errorList) else: command["error"] = errorList[0] if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = locator + "的实际值符合预期正则" + regexp
def assertResultContains(command): """ {"desc":"验证结果包含","alias":"验证结果包含","valueDesc":{"expected":"预期值","locator":"json提取表达式"},"pattern":"验证结果(?P<locator>.*)包含(?P<expected>.*)"} """ valueMap = command["value"] resultMap = command["result"] expected = str(valueMap["expected"]).strip() locator = valueMap["locator"].strip() check = valueMap["check"] if "check" in valueMap.keys() else False if not locator or len(locator) == 0: if not expected in resultMap["result"]: command["error"] = "实际结果不包含预期值(" + expected + ")" if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = "实际结果包含预期值(" + expected + ")" else: actual = "" errorList = [] try: actual = json.get_value(resultMap["result"], locator) except Exception as err: command["error"] = err if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) if not expected in actual: errorList.append("locator为%s的结果值为%s,不包含预期值%s" % (locator, actual, expected)) if len(errorList) > 0: if len(errorList) > 1: command["error"] = json.dumps(errorList) else: command["error"] = errorList[0] if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = locator + "的实际值包含预期值" + expected
def assertResultBlank(command): """ {"desc":"验证结果为空","alias":"验证结果为空","valueDesc":{"locator":"json提取表达式"},"pattern":"验证结果(?P<locator>.*)为空"} """ valueMap = command["value"] resultMap = command["result"] locator = valueMap["locator"].strip() check = valueMap["check"] if "check" in valueMap.keys() else False if not locator or len(locator) == 0: if len(resultMap["result"]) > 0: command["error"] = "实际结果不为空" if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = "实际结果为空" else: actual = "" errorList = [] try: actual = json.get_value(resultMap["result"], locator) except Exception as err: logging.error(traceback.format_exc()) command["error"] = err if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) if actual: errorList.append("locator为%s的实际值为%s,不为空" % (locator, actual)) if len(errorList) > 0: if len(errorList) > 1: command["error"] = json.dumps(errorList) else: command["error"] = errorList[0] if check: raise ExecutionCheckException(command["error"]) else: raise ExecutionFailException(command["error"]) resultMap["message"] = "locator为%s的实际值为空" % (locator)
def replace(value,paramMap,start="\$\{",end="}"): it = re.finditer(r"%s(.*?)%s" % (start,end),value) try: for match in it: for group in match.groups(): groups=group.split(",") replaceValue=groups[1] if len(groups)>1 else None index0=groups[0].find('.') index1=groups[0].find('[') index=-1 if index0<0 and index1<0 else index0 if index0>=0 and index1<0 else index1 if index0<0 and index1>=0 else min(index0,index1) if index<0: if groups[0] in paramMap: replaceValue=str(paramMap[groups[0]]) else: paramName=groups[0][0:index] paramLocator=groups[0][index:] paramLocator=paramLocator[1:] if paramLocator[0]=='.' else paramLocator if paramName in paramMap: paramValue=json.get_value(paramMap[paramName],paramLocator) if isinstance(paramValue,(dict,list)): replaceValue=paramValue elif isinstance(paramValue,(str,int,float)): replaceValue=str(paramValue) elif paramValue is None: replaceValue="null" else: replaceValue=paramValue # replaceValue=str(paramValue) if not isinstance(paramValue,(dict,list)) else paramValue if replaceValue is not None: group=group.replace("[","\[").replace("]","\]") value=re.sub(r"%s%s%s" % (start,group,end), replaceValue, value) # value=value.replace("%s%s%s" % (start,group,end),replaceValue) return value except Exception as e: logging.error(traceback.format_exc())