Groovy 用于在 JMeter 中验证 ResponseData 的脚本
Groovy script to validate ResponseData in JMeter
我编写了这个脚本来验证字段类型,但我不确定它是否被正确验证。我还想验证是否看到了所有预期的字段。
这是我的 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)
如果你想检查 JSON 响应数据类型,将你的行更改为
(eventName.getClass() == String)
至
assert eventName instanceof String
我建议从 BSF 断言切换到 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! 文章以获得全面的解释和脚本最佳实践。
还有一个JSON Path Assertion available via JMeter Plugins,这个主要用来查看响应内容
我编写了这个脚本来验证字段类型,但我不确定它是否被正确验证。我还想验证是否看到了所有预期的字段。 这是我的 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)
如果你想检查 JSON 响应数据类型,将你的行更改为
(eventName.getClass() == String)
至
assert eventName instanceof String
我建议从 BSF 断言切换到 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! 文章以获得全面的解释和脚本最佳实践。
还有一个JSON Path Assertion available via JMeter Plugins,这个主要用来查看响应内容