简体   繁体   English

Groovy脚本在JMeter中验证ResponseData

[英]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: 这是我的BSF断言:

在此处输入图片说明

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 如果要检查JSON响应数据类型,请更改您的行,例如

     (eventName.getClass() == String) 

    to

     assert eventName instanceof String 

    See Groovy Testing Guide for details 有关详细信息, 请参见Groovy测试指南

  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. 我建议从BSF断言切换到JSR223断言,因为它能够编译脚本和缓存,因此脚本的性能会更好。 See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! 请参见Beanshell,JSR223和Java JMeter脚本:您一直在等待的性能下降! 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 还可以通过JMeter插件使用JSON路径声明 ,该声明主要用于检查响应内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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