如何检查每个标记值与其他具有相同名称的标记中的值不同

时间:2017-05-04 12:05:30

标签: xml jmeter

inn jmeter,在具有多个同名标签的xml响应中,如何验证标签中存在的每个值在Jmeter上是否彼此不同。我附加了必须验证的响应。此代码包含2的描述优惠,但这个数字可以在0-10之间变化。我想对“msgid”进行一次唯一的检查(即响应中没有msgid值相同),并且还给出了msgid标记在响应中重复的次数。

enter code here <offers>
           <number xsi:nil="true"/>
           <number>
              <amrit>S</amrit>
              <interestedFlag>Y</interestedFlag>
              <msgid>145983</msgid>
              <msisdn xsi:nil="true"/>                 
              <offerIndex>1</offerIndex>
              <msgtext>Rs115=225 </msgtext>
              <onlineFulfilmentFlag>Y</onlineFulfilmentFlag>
              <orderId xsi:nil="true"/>
              <otherSearchInfo>
                 <number>
                    <key>START_DATE</key>
                    <value>2016-09-16 11:28:46.921455</value>
                 </number>
                 <number>
                    <key>END_DATE</key>
                    <value>2017-08-03 00:00:00.0</value>
                 </number>
                 <number>
                    <key>OFFER_CATEGORY</key>
                    <value>Others</value>
                 </number>
              </otherSearchInfo>
              <price>115</price>
              <shortDesc>115=225Loc+STD M,28D</shortDesc>
              <subscriptionStatus>P</subscriptionStatus>
           </number>
           <number>
              <amrit>O</amrit>
              <interestedFlag>Y</interestedFlag>
              <msgid>143429</msgid>
              <msisdn xsi:nil="true"/>
              <msgid>2803708</msgid>
              <offerIndex>2</offerIndex>
              <msgtext>Rs306=Rs306 Full</msgtext>
              <onlineFulfilmentFlag>Y</onlineFulfilmentFlag>
              <orderId xsi:nil="true"/>
              <otherSearchInfo>
                 <number>
                    <key>START_DATE</key>
                    <value>2016-09-16 11:28:46.921455</value>
                 </number>
                 <number>
                    <key>END_DATE</key>
                    <value>2017-08-03 00:00:00.0</value>
                 </number>
                 <number>
                    <key>OFFER_CATEGORY</key>
                    <value>Combo</value>
                 </number>
              </otherSearchInfo>
              <price>306</price>
              <shortDesc>Rs306=306TT(Ultd Val)</shortDesc>
              <subscriptionStatus>P</subscriptionStatus>
           </number>
           <number>
              <amrit>O</amrit>
              <interestedFlag>Y</interestedFlag>
              <msgid>145022</msgid>
              <msisdn xsi:nil="true"/>
              <msgid>2800482</msgid>
              <msgtext>Rs110=Rs110 Full</msgtext>
              <onlineFulfilmentFlag>Y</onlineFulfilmentFlag>
              <orderId xsi:nil="true"/>
              <otherSearchInfo>
                 <number>
                    <key>START_DATE</key>
                    <value>2016-09-16 11:28:46.921455</value>
                 </number>
                 <number>
                    <key>END_DATE</key>
                    <value>2017-08-03 00:00:00.0</value>
                 </number>
                 <number>
                    <key>OFFER_CATEGORY</key>
                    <value>Others</value>
                 </number>
              </otherSearchInfo>
              <price>110</price>
              <shortDesc>Rs110=Rs110 FTT, ULTD</shortDesc>
              <subscriptionStatus>P</subscriptionStatus>
           </number>

1 个答案:

答案 0 :(得分:0)

你可以使用JSR223 Assertion

一举两得
  1. 将JSR223 Assertion添加为请求的子节点,该请求返回上述XML
  2. 将以下代码放入“脚本”区域:

    import org.apache.commons.collections.CollectionUtils
    import org.apache.commons.collections.Predicate
    
    def response = prev.getResponseDataAsString()
    def xml = new XmlSlurper().parseText(response)
    def msgids = xml."**".findAll { node ->
        node.name() == "msgid"
    }
    msgids.each { msgid ->
        int msgidCount = CollectionUtils.countMatches(msgids, new Predicate() {
            boolean evaluate(Object input) {
                return (input.equals(msgid));
            }
        })
        log.info("Message ID: " + msgid + " count: " + msgidCount) 
        if (msgidCount > 1) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage("Message ID " + msgid + " occurred " + msgidCount + " times")
        }
    
    }
    log.info("msgid count: " + msgids.size())
    
    • 如果存在重复的msgid标记,断言将使采样器失败并提供标记值和消息中出现的次数
    • 每个msgid标记的出现次数将打印到 jmeter.log 文件

      JMeter JSR223 Assertion XML

  3. 参考文献: