简体   繁体   中英

How to Write Response Assertion Using Groovy in JMeter

I want to write a code for Response assertion using groovy for one of the Request Giving Response data like this

{
"value":"200"
"value_description":"pass"
"value_code":"pass"
"data_encode":"uyt-09-0nbv"
}

after google Search i am getting only with Response Assertion SOAP-UI tools and i also checked with Blaze meter blog i am not understating about what they are saying. simple way i want demonstrate that.write code for Response Data Assert value for 200 is this possible. please help me to this stuff

The relevant Groovy code to check whether value attribute in the response equals 200 would be something like:

def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())

def value = json.value

if (value != '200') {
    AssertionResult.setFailure(true)
    AssertionResult.setFauilreMessage('Expected 200, but got ' + value)
}
  1. Add JSR223 Assertion as a child of the request which returns the aforementioned JSON (it is not valid JSON by the way)
  2. Put the above code into "Script" area (make sure you tick Cache compiled script if available box and choose groovy from the "Language" dropdown)

More information:

You can add 4 JSON Extractor s, each with different Path Expressions:

$.value , $.value_description , $.value_code , $.data_encode

It will assert that JSON parameter returned.

You can add Regular Expression Extractor s to check each variable you got using Apply to JMeter Variable .

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