Groovy 使用属性步骤中存储的值验证节点值列表

2024-02-09

属性截图 https://i.stack.imgur.com/iEYRw.jpg响应片段

    <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"
}

您可以使用下面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())
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Groovy 使用属性步骤中存储的值验证节点值列表 的相关文章

随机推荐