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}}]
 }
New contributor
user2388556 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

1

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

New contributor
user2388556 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
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

user2388556 is a new contributor. Be nice, and check out our Code of Conduct.

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

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