Groovy 使用存储在 属性 步骤中的值来验证节点值列表
Groovy to validate the list of node values with the values stored in a property step
Properties Screenshot响应片段
<tns:TAResponse
xmlns:tns="http://webconnectivity.co.uk/">
<tns:SessionToken>84hjfutryh47849dkdhg9493493=</tns:SessionToken>
<tns:PartyId>1234</tns:PartyId>
<tns:ResponseType></tns:ResponseType>
<tns:MessageUUId>12341F17-ABC9-3E99-1D12-B8289POO2107</tns:MessageUUId>
<tns:Matches>
<tns:IntegrationServiceMatch>
<tns:InternalReference>2066856</tns:InternalReference>
<tns:ErrorsAndWarnings>
<tns:IntegrationServiceErrorCode>
<tns:ErrorCode>W000026</tns:ErrorCode>
<tns:Description>Settlement Date not within tolerance</tns:Description>
</tns:IntegrationServiceErrorCode>
<tns:IntegrationServiceErrorCode>
<tns:ErrorCode>E000033</tns:ErrorCode>
<tns:Description>Number on message does not match</tns:Description>
</tns:IntegrationServiceErrorCode>
<tns:IntegrationServiceErrorCode>
<tns:ErrorCode>E000001</tns:ErrorCode>
<tns:Description>NO likely matches</tns:Description>
</tns:IntegrationServiceErrorCode>
</tns:ErrorsAndWarnings>
</tns:IntegrationServiceMatch>
</tns:Matches>
</tns:TAResponse>
我在 属性 步骤中存储了所有这些错误代码和描述。我需要验证响应中的这些错误代码和描述是否与 属性 步骤匹配。 (顺序不是交易)
有人可以指导我完成 groovy 吗?
以前我只需要从响应中验证一个错误代码和描述。所以我使用下面的脚本进行了验证。
def content = new XmlSlurper().parse(file)
def respType = content.Body.TAResponse.ResponseType.text()
def errAndWarn = content.Body.TAResponse.Matches.IntegrationServiceMatch
assert errAndWarn.size() == 1
for (def i=0;i<errAndWarn.size();i++)
{
assert errAndWarn[i].ErrorsAndWarnings.IntegrationServiceErrorCode.ErrorCode == "E000033"
assert errAndWarn[i].ErrorsAndWarnings.IntegrationServiceErrorCode.Description == "Number on message does not match"
}
你应该可以做到:
content.Body
.TAResponse
.Matches
.IntegrationServiceMatch
.ErrorsAndWarnings
.IntegrationServiceErrorCode.each { node ->
println "Error code is ${node.ErrorCode.text()} and Description is ${node.Description.text()}"
}
您可以将下面的 Script Assertion
用于 SOAP 请求测试步骤本身,这将避免额外的 Groovy Script
测试步骤。
这假设 Properties 测试步骤名称 的名称是 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 Script
//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())
}
Properties Screenshot响应片段
<tns:TAResponse
xmlns:tns="http://webconnectivity.co.uk/">
<tns:SessionToken>84hjfutryh47849dkdhg9493493=</tns:SessionToken>
<tns:PartyId>1234</tns:PartyId>
<tns:ResponseType></tns:ResponseType>
<tns:MessageUUId>12341F17-ABC9-3E99-1D12-B8289POO2107</tns:MessageUUId>
<tns:Matches>
<tns:IntegrationServiceMatch>
<tns:InternalReference>2066856</tns:InternalReference>
<tns:ErrorsAndWarnings>
<tns:IntegrationServiceErrorCode>
<tns:ErrorCode>W000026</tns:ErrorCode>
<tns:Description>Settlement Date not within tolerance</tns:Description>
</tns:IntegrationServiceErrorCode>
<tns:IntegrationServiceErrorCode>
<tns:ErrorCode>E000033</tns:ErrorCode>
<tns:Description>Number on message does not match</tns:Description>
</tns:IntegrationServiceErrorCode>
<tns:IntegrationServiceErrorCode>
<tns:ErrorCode>E000001</tns:ErrorCode>
<tns:Description>NO likely matches</tns:Description>
</tns:IntegrationServiceErrorCode>
</tns:ErrorsAndWarnings>
</tns:IntegrationServiceMatch>
</tns:Matches>
</tns:TAResponse>
我在 属性 步骤中存储了所有这些错误代码和描述。我需要验证响应中的这些错误代码和描述是否与 属性 步骤匹配。 (顺序不是交易) 有人可以指导我完成 groovy 吗? 以前我只需要从响应中验证一个错误代码和描述。所以我使用下面的脚本进行了验证。
def content = new XmlSlurper().parse(file)
def respType = content.Body.TAResponse.ResponseType.text()
def errAndWarn = content.Body.TAResponse.Matches.IntegrationServiceMatch
assert errAndWarn.size() == 1
for (def i=0;i<errAndWarn.size();i++)
{
assert errAndWarn[i].ErrorsAndWarnings.IntegrationServiceErrorCode.ErrorCode == "E000033"
assert errAndWarn[i].ErrorsAndWarnings.IntegrationServiceErrorCode.Description == "Number on message does not match"
}
你应该可以做到:
content.Body
.TAResponse
.Matches
.IntegrationServiceMatch
.ErrorsAndWarnings
.IntegrationServiceErrorCode.each { node ->
println "Error code is ${node.ErrorCode.text()} and Description is ${node.Description.text()}"
}
您可以将下面的 Script Assertion
用于 SOAP 请求测试步骤本身,这将避免额外的 Groovy Script
测试步骤。
这假设 Properties 测试步骤名称 的名称是 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 Script
//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())
}