Groovy script to validate ResponseData in JMeter

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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:

  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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!