Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I tried to use script assert for json response in SoapUI but Error comes

"Lexing failed on line: 1, column: 1, while reading '<', no possible valid JSON value or punctuation could be recognized."

Request:

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

Response:

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

Script Assertion:

import groovy.json.JsonSlurper

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

assert json.roomAttribute.EndPointType == "SR"

How to Assert the 'EndPointType','GroupId','RegionId','Occupancy' using script assertion with JsonSlurper class.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
294 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is the script assertion for requested properties of json. Added comments in-line for each line of the script.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...