Groovy使用存储在属性步骤中的值验证节点值列表 [英] Groovy to validate the list of node values with the values stored in a property step

查看:102
本文介绍了Groovy使用存储在属性步骤中的值验证节点值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

属性截图响应代码片段

 < 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:匹配>
< tns:IntegrationServiceMatch>
< tns:InternalReference> 2066856< / tns:InternalReference>
< tns:IntegrationServiceErrorCode>
< tns:ErrorCode> W000026< / tns:ErrorCode>
< tns:说明>结算日期不在公差范围内< / tns:说明>
< / tns:IntegrationServiceErrorCode>
< tns:IntegrationServiceErrorCode>
< tns:ErrorCode> E000033< / tns:ErrorCode>
< tns:说明>邮件上的号码不匹配< / tns:说明>
< / tns:IntegrationServiceErrorCode>
< tns:IntegrationServiceErrorCode>
< tns:ErrorCode> E000001< / tns:ErrorCode>
< tns:说明>无可能匹配< / tns:说明>
< / tns:IntegrationServiceErrorCode>
< / tns:ErrorsAndWarnings>
< / tns:IntegrationServiceMatch>
< / tns:匹配>
< / tns:TAResponse>

我已经将所有这些Errorcode和Description存储在属性步骤中。我需要验证响应中的这些Errorcode和Description是否与属性步骤匹配。 (序列不是交易)
有人可以引导我通过groovy吗?
之前我只需要验证一个错误码和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不符合


解决方案

在SOAP Request测试步骤本身中使用下面的 Script Assertion ,这将避免额外的 Groovy Script 测试步骤。



这假设属性测试步骤名称的名称是属性。如果其名称不同,则相应地更改脚本。

脚本断言

  //将属性测试步骤的名称更改为
def step = context.testCase.testSteps ['Properties']

//检查响应是否不是
assert context.response,'响应为空或空'

//解析xml
def parsedXml = new XmlSlurper()。parseText(context.response)

//从响应中获取所有的错误细节信息,如地图
def errorDetails = parsedXml。'**'。findAll {it.name()=='IntegrationServiceErrorCode'}。inject([:]){地图,条目 - > map [entry.ErrorCode.text()] = entry.Description.text();
$ b //循环通过xml错误代码并验证属性测试步骤
errorDetails的属性。每个{键,值 - >
assert step.properties [key]?。value == value,无法匹配属性$ {key}的值
}


Groovy脚本测试步骤: p>

  //更改
下面的属性测试步骤的名称def step = context.testCase.testSteps ['Properties']

//像你在脚本中一样解析xml
def parsedXml = new XmlSlurper()。parse(file)

//获取所有错误来自响应的细节作为映射
def errorDetails = parsedXml。'**'。findAll {it.name()=='IntegrationServiceErrorCode'}。inject([:]){map,entry - > map [entry.ErrorCode.text()] = entry.Description.text();
$ b //循环通过xml错误代码并验证属性测试步骤
errorDetails的属性。每个{键,值 - >
assert step.properties [key]?。value == value,无法匹配属性$ {key}的值
}

EDIT2:根据其他要求,在 Groovy脚本下添加

  //在$ b $下面更改Properties测试步骤的名称def step = context.testCase.testSteps ['Properties'] 

//解析你的脚本中的xml
def parsedXml = new XmlSlurper()。parse(文件)

//从响应中获取所有错误信息,如地图
def errorDetails = parsedXml。'**'。findAll {it.name()=='IntegrationServiceErrorCode'}。inject([:]){map,entry - > map [entry.ErrorCode.text()] = entry.Description.text();
$ b def failureMessage = new StringBuffer()

//通过属性的循环属性步骤并检查响应
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没有错误代码$ {key} )
}
}
if(failureMessage.toString()){
throw new Error(failureMessage.toString())
}


Properties ScreenshotResponse Snippet

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

I have stored all these Errorcode and Description in a property step. I need to validate whether these Errorcode and Description in the response matches with the property step. (sequence is not a deal) Can someone pls guide me through groovy? Previously i had to validate only one errorcode and desc from the response. so i validated using below script.

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

解决方案

You can use below Script Assertion for the SOAP Request test step itself, that will avoid additional Groovy Script test step.

This assumes that the name of the Properties test step name is Properties. If its name differs, then change in the script accordingly.
Script Assertion

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

EDIT: based on OP's comment

Groovy Script test step:

//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: Based on additional requirement, adding below 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())
} 

这篇关于Groovy使用存储在属性步骤中的值验证节点值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆