0

For the below response I need to fetch the rideId and pass it to the next request in Jmeter.Also ,the API that generates below response should be executed until the eventType is HANDSHAKE.

 [{"id":90856,"eventType":"HANDSHAKE","parameters":"{\"handshakeExpiration\":1669217518986,\"rideId\":3107}"}]

I am using the code :

import groovy.json.JsonSlurper;
def jsonSlurper=new JsonSlurper();
def apiDetailsArr=jsonSlurper.parse(prev.getResponseData())
def apiDetails=apiDetailsArr.size()>0?apiDetailsArr.get(0):null
def shouldRun = "1"
if(apiDetails!=null)
{
log.info("details",apiDetails.eventType+"")
if(apiDetails.eventType="HANDSHAKE"){
shouldRun="0"
}
def object=jsonSlurper.parseText(apiDetails.parameters)
log.info("xyz",object+"")

def id=object.rideId;
log.info("id",id+"")
vars.put("id", id+"")
}
else{
shouldRun="1"`enter code here`
}  


 `Condition for while controller :     `${__javaScript( "${shouldRun}" != "0",)}``

1 Answer 1

0
  1. All your log.info function calls cause syntax errors, you can see it yourself in (surprise!) the jmeter.log file
  2. I fail to see where you're assigning shouldRun JMeter Variable which is used in the While Controller

Suggested code change:

import groovy.json.JsonSlurper;

def jsonSlurper = new JsonSlurper();
def apiDetailsArr = jsonSlurper.parse(prev.getResponseData())
def apiDetails = apiDetailsArr.size() > 0 ? apiDetailsArr.get(0) : null
def shouldRun = "1"
if (apiDetails != null) {
    log.info("details", apiDetails.eventType + "")
    if (apiDetails.eventType = "HANDSHAKE") {
        shouldRun = "0"
    }
    def object = jsonSlurper.parseText(apiDetails.parameters)
    log.info("xyz" + object)

    def id = object.rideId;
    log.info("id" + id)
    vars.put("id", id as String)
} else {
    shouldRun = "1"
}  

vars.put("shouldRun", shouldRun)

More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.