您可以使用下面Script Assertion
对于 SOAP 请求测试步骤本身,这将避免额外的Groovy Script
测试步骤。
这假设名称属性测试步骤名称 is Properties
。如果其名称不同,则在脚本中进行相应更改。
脚本断言
//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']
//check if the response is not empy
assert context.response, 'Response is empty or null'
//Parse the xml
def parsedXml = new XmlSlurper().parseText(context.response)
//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry -> map[entry.ErrorCode.text()] = entry.Description.text(); map }
log.info "Error details from response : ${errorDetails}"
//loop thru xml error codes and verify against the properties of Properties Test Step
errorDetails.each { key, value ->
assert step.properties[key]?.value == value, "Unable to match value of the property ${key} "
}
编辑:根据OP的评论
Groovy 脚本测试步骤:
//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']
//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)
//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry -> map[entry.ErrorCode.text()] = entry.Description.text(); map }
log.info "Error details from response : ${errorDetails}"
//loop thru xml error codes and verify against the properties of Properties Test Step
errorDetails.each { key, value ->
assert step.properties[key]?.value == value, "Unable to match value of the property ${key} "
}
EDIT2:根据附加要求,添加以下内容Groovy 脚本
//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']
//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)
//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry -> map[entry.ErrorCode.text()] = entry.Description.text(); map }
log.info "Error details from response : ${errorDetails}"
def failureMessage = new StringBuffer()
//Loop thru properties of Property step and check against the response
step.properties.keySet().each { key ->
if (errorDetails.containsKey(key)) {
step.properties[key]?.value == errorDetails[key] ?: failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
} else {
failureMessage.append("Response does not have error code ${key}")
}
}
if (failureMessage.toString()) {
throw new Error(failureMessage.toString())
}