【问题标题】:Script Assertion for json responsejson 响应的脚本断言
【发布时间】:2016-04-19 10:55:37
【问题描述】:

我尝试在 SoapUI 中使用脚本断言进行 json 响应,但出现错误

“Lexing 在第 1 行,第 1 列失败,在读取 '

请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://localhost">
   <soapenv:Header/>
   <soapenv:Body>
      <loc:GetRoomAttribute>
         <loc:countryId>123</loc:countryId>
         <loc:locationId>36</loc:locationId>
         <loc:roomId>213</loc:roomId>
      </loc:GetRoomAttribute>
   </soapenv:Body>
</soapenv:Envelope>

回应

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetRoomAttributeResponse xmlns="http://localhost">
         <GetRoomAttributeResult>{"roomAttribute":{"EndPointType":"SR","GroupId":23,"RegionId":22,"Occupancy":6}}</GetRoomAttributeResult>
      </GetRoomAttributeResponse>
   </soap:Body>
</soap:Envelope>

脚本断言

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response

assert json.roomAttribute.EndPointType == "SR"

如何使用带有JsonSlurper 类的脚本断言来断言'EndPointType','GroupId','RegionId','Occupancy'

【问题讨论】:

  • 这是 XML,而不是您传递给 parseText 的 Json
  • 你能告诉我如何断言这一点。谢谢

标签: json groovy soapui


【解决方案1】:

这里是请求的 json 属性的脚本断言。为脚本的每一行添加了内联 cmets。

import groovy.json.*
import com.eviware.soapui.support.XmlHolder
def xml = '''
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetRoomAttributeResponse xmlns="http://localhost">
      <GetRoomAttributeResult>{"roomAttribute":{"EndPointType":"SR","GroupId":23,"RegionId":22,"Occupancy":6}}</GetRoomAttributeResult>
    </GetRoomAttributeResponse>
  </soap:Body>
</soap:Envelope>'''
//response xml holder
def response = new XmlHolder(xml)
/*you may replace the above constant xml using context.response in the above line i.e new XmlHolder(context.response) and also remove def xml.. line too */
//read the json data from xml using xpath
def data = response.getNodeValue("//*:GetRoomAttributeResult")
//log the json data
log.info data
//read roomAtribute from json using json slurper
def roomAttribute  = new groovy.json.JsonSlurper().parseText( data.toString()).roomAttribute 
//Assert EndPointType property is not  present  in the response
assert  null != roomAttribute.EndPointType, "Endpoint type property does not exists or null"
//Assert EndPointType Value
assert  roomAttribute.EndPointType , "Endpoint type does not have value"
//Assert GroupId property is not  present  in the response
assert  null !=roomAttribute.GroupId, "GroupId property does not exists or null"
//Assert GroupId Value
assert  roomAttribute.GroupId, "GroupId does not have value"
//Assert RegionId property is not  present  in the response
assert  null != roomAttribute.RegionId, "RegionId property does not exists or null"
//Assert RegionId Value
assert  roomAttribute.RegionId, "RegionId does not have value"
//Assert Occupancy property is not  present  in the response
assert  null != roomAttribute.Occupancy, "Occupancy property does not exists or null"
//Assert Occupancy Value
assert  roomAttribute.Occupancy, "Occupancy does not have value"
//log the values
log.info "Endpoint Type  : ${roomAttribute.EndPointType}"
log.info "Group Id  : ${roomAttribute.GroupId}"
log.info "Region Id  : ${roomAttribute.RegionId}"
log.info "Occupancy : ${roomAttribute.Occupancy}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多