1

mobileNubmer is being generated in JSR223 PreProcessor but it's not replacing the parameter in the request body (JSON). Here is my setup and what am I doing wrong?

JSR223 PreProcessor
def randomValue = ${__Random(00000000,99999999)} as String 
def mobileNumber = "04" + randomValue
vars.put('randomValue', mobileNumber) 

Follow by HTTP request

HTTP request
POST - Body
{
    "senderId": "Test",
    "messages": [
    {"to": ${mobileNumber}}]
 } 

Failed request

POST https://api

POST data:
{
    "senderId": "SMAPIJ",
    "messages": [
    {"to": ${mobileNumber}}]
 }

2 Answers 2

1

All good. I wasn't using the right parameter. I replaced mobileNumber with randomValue and it's working now.

1

As per JSR223 Sampler documentation:

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

  • Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
  • Or Use Script Text and check Cache compiled script if available property.

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

So either call __Random() function directly in your HTTP Request body like:

{
  "senderId": "SMAPIJ",
  "messages": [
    {
      "to": 04${__Random(00000000,99999999)}
    }
  ]
}

or if you prefer to generate a random number in Groovy amend your code to look like:

def randomValue = '04' + String.format('%07d', org.apache.commons.lang3.RandomUtils.nextInt(0, 99999999))

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.