用于json响应的脚本声明 [英] Script Assertion for json response

查看:275
本文介绍了用于json响应的脚本声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在SoapUI中使用脚本声明来执行json响应,但错误来了

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


Lexing在线失败:1,列:1 ,而在阅读'<'时,不可能识别出有效的JSON值或标点符号。

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

如何用 JsonSlurper 脚本断言来断言'EndPointType','GroupId','RegionId','占用' code> class。

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

推荐答案

这是json所请求属性的脚本断言。

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

这篇关于用于json响应的脚本声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆