2017-01-05 94 views
0

我想为SOAPUI编写一个xPath查询来验证参数Score中是否有参数验证值的“BDS”。如何从SOAPUI获得XPath查询

所有的代码响应是在这里:

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

SOAPUI在图像上。问题是我怎么写xpath ..非常感谢!

+0

请您澄清一下这个问题吗?当ScoreID是'BSD'时,你想检查'Score'为'-1'吗? – Rao

+0

Pavel,发布的数据不可分析。 – Rao

+1

请不要发表“为我编写代码”问题。如果您在某个XPath表达式中遇到问题,请包含该表达式以及一些推理。如果您到目前为止还没有XPath表达式,请花更多时间考虑您的问题并提出一些建议。 – Tomalak

回答

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断言。