Groovy脚本来验证JMeter中的ResponseData [英] Groovy script to validate ResponseData in JMeter

查看:623
本文介绍了Groovy脚本来验证JMeter中的ResponseData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个脚本来验证字段类型,但我不确定它是否被正确验证。另外我想验证所有预期的领域都看到了。
这是我的BSF断言:

  import groovy.json。*; 

def response = prev.getResponseDataAsString();
def json = new JsonSlurper()。parseText(response)
$ b $ def eventName = json.event_name $ b $(eventName.getClass()== String)

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

def attendeeLimit = json.attendee_limit
(attendeeLimit.getClass()== Integer)
$ b $ def orderCount = json.order_count
(orderCount.getClass()== Integer)
$ b $ def attendanceLimit = json.attendance_limit_on
(attendanceLimit.getClass ()== String)


解决方案


  1. 如果您想检查JSON响应数据类型,请更改您的行,如

     (eventName.getClass()==字符串)

      assert eventName instanceof String 

    查看Groovy测试指南了解详情


  2. 我建议从BSF断言切换到 JSR223断言,因为它可以编译脚本和缓存,因此你的脚本将表现得更好。请参阅 Beanshell vs JSR223与Java JMeter脚本:您一直在等待的演出!文章,以获得全面的解释和脚本编写最佳实践。 /jmeter-plugins.org/wiki/JSONPathAssertion/rel =nofollow> JSON路径声明可通过 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

    这篇关于Groovy脚本来验证JMeter中的ResponseData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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