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

Groovy使用存储在属性步骤中的值验证节点值列表,groovy,soapui,Groovy,Soapui,响应片段 <tns:TAResponse xmlns:tns="http://webconnectivity.co.uk/"> <tns:SessionToken>84hjfutryh47849dkdhg9493493=</tns:SessionToken> <tns:PartyId>1234</tns:PartyId> <tns:ResponseType></tns:Respo

响应片段

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

84hjfutryh47849dkdhg9493493=
1234
12341F17-ABC9-3E99-1D12-B82892107
2066856
W000026
结算日期不在允许范围内
E000033
消息上的数字不匹配
E000001
没有可能的匹配
我已将所有这些错误代码和描述存储在属性步骤中。我需要验证响应中的这些错误代码和描述是否与属性步骤匹配。(序列不是交易) 有人能带我穿过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"
}
def content=new XmlSlurper().parse(文件)
def respType=content.Body.TAResponse.ResponseType.text()
def ERNADWARN=content.Body.TAResponse.Matches.IntegrationServiceMatch
assert erran.size()==1

对于(def i=0;i您应该能够:

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

您可以对SOAP请求测试步骤本身使用下面的
脚本断言
,这将避免额外的
Groovy脚本
测试步骤

这假定属性测试步骤名称的名称为
属性
。如果其名称不同,则相应地在脚本中更改。
脚本断言

//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())
} 

你能发布所有的xml,而不仅仅是其中的一个无效片段吗?你需要检查响应中要找到的所有属性吗?还是一些?@Rao我需要检查所有属性。因为这是我预期的验证。好吧,删除
assert context.response
line并更改
def parsedXml=new XmlSlurper().parseText(context.response)
def parsedXml=new-XmlSlurper().parse(file)
。你应该很好。你应该仍然能够在脚本断言中进行验证。不管怎样,都可以编辑答案。请看一看。这非常有效:)你能使下面的语句成为可能吗。这将缩小我的验证范围。“我在属性步骤中有这些预期的3个错误代码和描述。如果响应中有更多的错误代码和描述(比如4个包括这3个),那么groovy应该不会失败。只想验证我预期的3是否出现在响应中。反之亦然(响应中只有2个,但预期中有3个)那么groovy在这一点上应该失败了“。可能您可以显示属性步骤的屏幕截图。我不清楚的是,您是否需要让属性测试步骤中的所有属性都出现在响应中?上述解决方案通过在属性步骤中查找断言响应中出现的任何内容都是有效的。请检查相关的屏幕截图。(响应片段附近的属性屏幕截图)。这里我只有2个错误代码和说明。我需要验证这些预期的错误代码和说明是否应该出现在响应中。在响应中可以是5或6或7个错误代码和desc。不必担心其他。我只需要验证我预期的错误代码是否出现在响应中。请让我知道这是否正确不清楚。谢谢你的回复@tim_yates