2017-01-05 104 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斷言。