我花了几个小时的时间来阅读一些文章,我不确定我是否采取了正确的方法。关于这个问题的更多内容。
在我的previous post中,我发现当我不应该使用阵列时,我正在使用阵列。我的最新问题与此相反,因为我想从我的jdbc响应中获取数据到一个数组中,以针对同样在数组中的SOAP响应进行断言。
我从数组形式的SOAP响应中得到的实际(EDITED)结果工作正常,但JDBC响应只抓取第一个值,因此assert失败。
这是我的JDBC响应中的一个片段:
<Results>
<ResultSet fetchSize="64">
<Row rowNumber="1">
<TW070_VALIDATION.CODE>APP</TW070_VALIDATION.CODE>
<TW070_VALIDATION.VALID_DATA/>
</Row>
<Row rowNumber="2">
<TW070_VALIDATION.CODE>CHI</TW070_VALIDATION.CODE>
<TW070_VALIDATION.VALID_DATA>1</TW070_VALIDATION.VALID_DATA>
</Row>
<Row rowNumber="3">
<TW070_VALIDATION.CODE>DEN</TW070_VALIDATION.CODE>
<TW070_VALIDATION.VALID_DATA>1</TW070_VALIDATION.VALID_DATA>
</Row>
</ResultSet>
</Results>
我想为返回的每一行选取两个值。所以在这个例子中我希望得到以下内容用于我的预期结果:
APP =,CHI = 1,DEN = 1
我的断言脚本目前看起来像这样:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)
//grab the expected result from jdbc response
def expectedCodes = context.expand( '${JDBC Request for expected results#ResponseAsXml#//*:TW070_VALIDATION.CODE}' )
//grab the actual result from the SOAP response
def actualCodes = pxml.'**'.findAll{it.name() == 'ReportAssessment'}.collectEntries{[(it.ReportAssessmentGroup.text()):it.Ranking.text()]}
log.info expectedCodes
log.info actualCodes
assert expectedCodes == actualCodes
EDIT2:从我的SOAP响应中添加一个样本(步骤4)
<ns2:ReportAssessment>
<ns2:ReportAssessmentGroup>APP</ns2:ReportAssessmentGroup>
<ns2:Ranking>0</ns2:Ranking>
<ns2:ReportAssessmentGroupDescription>APPLIANCES</ns2:ReportAssessmentGroupDescription>
</ns2:ReportAssessment>
<ns2:ReportAssessment>
<ns2:ReportAssessmentGroup>CHI</ns2:ReportAssessmentGroup>
<ns2:Ranking>1</ns2:Ranking>
<ns2:ReportAssessmentGroupDescription>CHIROPRACTIC</ns2:ReportAssessmentGroupDescription>
</ns2:ReportAssessment>
<ns2:ReportAssessment>
<ns2:ReportAssessmentGroup>DEN</ns2:ReportAssessmentGroup>
<ns2:Ranking>1</ns2:Ranking>
<ns2:ReportAssessmentGroupDescription>DENTAL</ns2:ReportAssessmentGroupDescription>
我一直在阅读循环方法,因此不确定我是否可以在groovy脚本中解决这个问题,或者我是否需要一个不同的测试结构来包含循环。
答案 0 :(得分:2)
你走了:
看来你有一个问题,因为元素名称有.
。因此,它需要包含在引号之间,如下所示:
首先找到所有Rows
并在每行中创建地图。
以下是Script Assertion
//Define the expected map (key value pairs)
def expected = [APP: '', CHI: '1', DEN: '1']
//Read the xml and create map
def xml = new XmlSlurper().parseText(context.response)
def actual = xml.'**'.findAll{it.name() == 'Row'}.collectEntries{ [(it.'TW070_VALIDATION.CODE'.text()): it.'TW070_VALIDATION.VALID_DATA'.text() ]}
log.info actual
assert expected == actual
编辑:基于OP评论和聊天
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)
//grab the expected result from jdbc response
def jdbcResponse = context.expand( '${JDBC Request for expected results#ResponseAsXml}')
def xml = new XmlSlurper().parseText(jdbcResponse)
def expectedCodes = xml.'**'.findAll{it.name() == 'Row'}.collectEntries{ [(it.'TW070_VALIDATION.CODE'.text()):it.'TW070_VALIDATION.VALID_DATA'.text() ]}
//grab the actual result from the SOAP response
def actualCodes = pxml.'**'.findAll{it.name() == 'ReportAssessment'}.collectEntries{[(it.ReportAssessmentGroup.text()):it.Ranking.text()]}
log.info expectedCodes
log.info actualCodes
assert expectedCodes == actualCodes
您可以快速尝试在线 demo