简体   繁体   中英

Groovy script to validate ResponseData in JMeter

I have written this script to verify field types, but i'm not sure if it is being validated correctly. Also i want to verify all the expected fields are seen. This is my BSF Assertion:

在此处输入图片说明

import groovy.json.*;

def response = prev.getResponseDataAsString();    
def json = new JsonSlurper().parseText(response)

def eventName = json.event_name
(eventName.getClass() == String)

def eventDate = json.event_start
(eventDate.getClass() == Date)

def attendeeLimit = json.attendee_limit
(attendeeLimit.getClass() == Integer)

def orderCount = json.order_count
(orderCount.getClass() == Integer)

def attendanceLimit = json.attendance_limit_on
(attendanceLimit.getClass() == String)
  1. If you want to check JSON response data types change your lines like

     (eventName.getClass() == String) 

    to

     assert eventName instanceof String 

    See Groovy Testing Guide for details

  2. I would suggest switching fro BSF Assertion to JSR223 Assertion as it is able to compile the script and cache hence your script will perform much better. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article for comprehensive explanation and scripting best practices.


There is also a JSON Path Assertion available via JMeter Plugins , this one is mainly used to check response content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM