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

时间:2017-03-22 14:10:09

标签: groovy soapui

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>

我已将所有这些错误代码和描述存储在属性步骤中。我需要验证响应中的这些错误代码和描述是否与属性步骤匹配。 (顺序不是交易) 有人可以指导我通过常规吗? 以前我只需要验证响应中的一个错误代码和desc。所以我使用下面的脚本验证。

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

2 个答案:

答案 0 :(得分:1)

您可以在Script Assertion下面使用SOAP请求测试步骤,这将避免额外的Groovy Script测试步骤。

这假设属性测试步骤名称的名称为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())
} 

答案 1 :(得分:0)

你应该可以这样做:

content.Body
       .TAResponse
       .Matches
       .IntegrationServiceMatch
       .ErrorsAndWarnings
       .IntegrationServiceErrorCode.each { node ->
    println "Error code is ${node.ErrorCode.text()} and Description is ${node.Description.text()}"
}