1. SPEForums need your contributions to build up a strong repository of performance engineering resources.

    Dismiss Notice
  2. Whatsapp Group For Software Performance Engineering Professionals!! Click here to get added . No Spam for strictly professional discussions only.
    Dismiss Notice
Dismiss Notice
Hello Guest,

Please reach out to us at admin@speforums.com/+91 8600177662 in case you face any issues with SPEForums. Share your feedback - Click Here

Please support us by sharing performance engineering tutorials/resources with SPEForums.com

-Crew

In JMeter getting error while generating request payload data dynamically with Groovy

Discussion in 'Apache JMeter' started by dipak, Mar 31, 2018.

  1. dipak

    dipak New Member

    Joined:
    Mar 31, 2018
    Messages:
    1
    Likes Received:
    0
    This is the request JSON

    {
    "Transaction": {
    "TrType": "Vehicle",
    "CustomerType": "${__chooseRandom(Individual,Company,)}",
    },
    "IndividualClient": {
    "firstName": "test first name",
    "lastName": "test last name"
    }
    "CompanyClient": {
    "CompanyName": "test company name",
    }
    }

    So if customer type = Individual then generate Individual else generate Company.

    I used JSR223 Pre-Processor and in that.

    import groovy.json.JsonOutput
    import groovy.json.JsonSlurper
    import groovy.json.JsonBuilder

    def jsonRequest = null

    JsonSlurper jsonSlurper = new JsonSlurper()
    jsonRequest = jsonSlurper.parseText(sampler.getArguments().getArgument(0).getValue())

    println(JsonOutput.prettyPrint(JsonOutput.toJson(jsonRequest)))

    if (jsonRequest.Transaction.CustomerType.equalsIgnoreCase("Individual")) {

    jsonBuilder = new JsonBuilder()
    jsonBuilder.IndividualClient {
    firstName "test first name"
    lastName "test last name"
    }
    vars.put("customertypedetails",jsonBuilder.toPrettyString())
    println(vars.get("customertypedetails"))
    } else {
    jsonBuilder2 = new JsonBuilder()
    jsonBuilder2.CompanyClient {
    companyName "test company name"
    }
    vars.put("customertypedetails",jsonBuilder2.toPrettyString())
    println(vars.get("customertypedetails"))
    }

    This ${customertypedetails} i am using in my JSON as;

    {
    "Transaction": {
    "TrType": "Vehicle",
    "CustomerType": "${__chooseRandom(Individual,Company,)}",
    },
    ${customertypedetails}
    }

    2018-03-28 18:06:42,342 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
    javax.script.ScriptException: javax.script.ScriptException: groovy.json.JsonException: expecting '}' or ',' but got current char '$' with an int value of 36

    The current character read is '$' with an int value of 36
    expecting '}' or ',' but got current char '$' with an int value of 36
    line number 12
    index number 305
    ${newcustomertypedetails}
    ..^
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-all-2.4.13.jar:2.4.13]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_161]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:849) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:467) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: javax.script.ScriptException: groovy.json.JsonException: expecting '}' or ',' but got current char '$' with an int value of 36

    The current character read is '$' with an int value of 36
    expecting '}' or ',' but got current char '$' with an int value of 36
    line number 12
    index number 305
    ${newcustomertypedetails}
    ..^
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-all-2.4.13.jar:2.4.13]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155) ~[groovy-all-2.4.13.jar:2.4.13]
    ... 8 more
    Caused by: groovy.json.JsonException: expecting '}' or ',' but got current char '$' with an int value of 36

    The current character read is '$' with an int value of 36
    expecting '}' or ',' but got current char '$' with an int value of 36
    line number 12
    index number 305
    ${newcustomertypedetails}
    ..^
    at groovy.json.internal.JsonParserCharArray.complain(JsonParserCharArray.java:153) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.internal.JsonParserCharArray.decodeJsonObject(JsonParserCharArray.java:144) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:186) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:157) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:46) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:384) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:107) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.JsonSlurper.parseText(JsonSlurper.java:205) ~[groovy-all-2.4.13.jar:2.4.13]
    at groovy.json.JsonSlurper$parseText.call(Unknown Source) ~[?:?]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) ~[groovy-all-2.4.13.jar:2.4.13]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) ~[groovy-all-2.4.13.jar:2.4.13]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) ~[groovy-all-2.4.13.jar:2.4.13]
    at Script52.run(Script52.groovy:12) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-all-2.4.13.jar:2.4.13]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155) ~[groovy-all-2.4.13.jar:2.4.13]
    ... 8 more
    2018-03-28 18:06:45,352 INFO o.a.j.t.JMeterThread: Thread is done: TG - Create Transaction 2-1
    2018-03-28 18:06:45,353 INFO o.a.j.t.JMeterThread: Thread finished: TG - Create Transaction 2-1
    2018-03-28 18:06:45,498 INFO o.a.j.t.JMeterThread: Thread is done: TG Authorization 1-1
    2018-03-28 18:06:45,499 INFO o.a.j.t.JMeterThread: Thread finished: TG Authorization 1-1
    2018-03-28 18:06:45,499 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
    2018-03-28 18:06:45,500 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

    Please let me know where i am incorrect.
     

Share This Page