如何从SOAPUI响应中获取XPath查询

时间:2017-01-05 09:07:21

标签: xml xpath groovy soapui cdata

我想为SOAPUI编写一个xPath查询,用于验证参数Score仅在" BDS"用于参数中的验证值。

代码的所有回复都在这里:



<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <MatchResponse xmlns="http://www.bottomline.com/intellinx/webservices">
         <MatchResult><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ResultBlock>
<MatchSummary matches="1">
<TotalMatchScore>50</TotalMatchScore>
<Rules totalRuleCount="5">
<Rule ruleCount="1">
<RuleID>Rule3_2_R</RuleID>
<Score>10</Score>
</Rule>
<Rule ruleCount="1">
<RuleID>Rule18_In</RuleID>
<Score>20</Score>
</Rule>
<Rule ruleCount="1">
<RuleID>Rule14_Su</RuleID>
<Score>20</Score>
</Rule>
</Rules>
</MatchSummary>
<ExternalScores>
<ExternalScore>
<extClientLegacy>2003-01-03-03.26.32.285776</extClientLegacy>
<SourceID>BDS</SourceID>
<Score>-1.0</Score>
</ExternalScore>
<ExternalScore>
<extClientLegacy>2003-01-03-03.26.32.285776</extClientLegacy>
<SourceID>O2</SourceID>
<Score>0.128</Score>
</ExternalScore>
</ExternalScores>
<ErrorWarnings>
<Errors errorCount="0"/>
<Warnings warningCount="0"/>
</ErrorWarnings>
</ResultBlock>]]></MatchResult>
      </MatchResponse>
   </S:Body>
</S:Envelope>
&#13;
&#13;
&#13;

SOAPUI在图像上。问题是如何编写xpath ..非常感谢!

1 个答案:

答案 0 :(得分:0)

由于数据位于cdata内,您可能必须使用groovy来实现相同目标。

注意您的xml响应已经过编辑,无法正确解析。你可能没有遇到这个问题,因为你会有实际的回应。

简而言之,这是需要做的事情:
  - 首先提取cdata部分,以获得需要应用xpath的实际xml   - 然后提取xpath

以下是Groovy Script

def xml = """<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <MatchResponse xmlns="http://www.bottomline.com/intellinx/webservices">
         <MatchResult><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ResultBlock>
<MatchSummary matches="1">
<TotalMatchScore>50</TotalMatchScore>
<Rules totalRuleCount="5">
<Rule ruleCount="1">
<RuleID>Rule3_2_R</RuleID>
<Score>10</Score>
</Rule>
<Rule ruleCount="1">
<RuleID>Rule18_In</RuleID>
<Score>20</Score>
</Rule>
<Rule ruleCount="1">
<RuleID>Rule14_Su</RuleID>
<Score>20</Score>
</Rule>
</Rules>
</MatchSummary>
<ExternalScores>
<ExternalScore>
<extClientLegacy>2003-01-03-03.26.32.285776</extClientLegacy>
<SourceID>BDS</SourceID>
<Score>-1.0</Score>
</ExternalScore>
<ExternalScore>
<extClientLegacy>2003-01-03-03.26.32.285776</extClientLegacy>
<SourceID>O2</SourceID>
<Score>0.128</Score>
</ExternalScore>
</ExternalScores>
<ErrorWarnings/>
<Errors errorCount="0"/>
</ResultBlock>]]>
</MatchResult>
</MatchResponse>
</S:Body>
</S:Envelope>"""

//The data you are looking for
//You may edit as you needed

def lookForScoreID = 'BDS'
def expectedScore = '-1.0'

//Closure to extract data of given node name
def searchData = { data, element ->
   def parsedData = new XmlSlurper().parseText(data)
   parsedData.'**'.find {it.name() == element} as String
}

//Closure to check the xpath
def searchByXpath = {data, xpath ->
   def holder = new com.eviware.soapui.support.XmlHolder(data)
   holder.getNodeValue(xpath)
}

//Gets the CDATA part of the response
//NOTE: if you want to use Script Assertion, use **context.response** instead of **xml** in the below statement. Also, you can remove the above xml.

def cdata = searchData(xml, 'MatchResult')
println cdata
def actualScore = searchByXpath(cdata, "//ExternalScore[SourceID = '$lookForScoreID']/Score")
log.info actualScore
assert expectedScore == actualScore, "Score is not matching for SourceID ${lookForScoreID}"

您也可以将上述脚本用作Script Assertion而不是单独的groovy script测试步骤,请参阅评论部分中的注释,即使用 context.response 代替 xml

话虽如此,您不需要xpath断言。