jsonjmeterpreprocessordynamic-values

Jmeter: Values not populated when request are generated dynamically


Test Plan Structure

TestPlan
JDBC Connection Configuration
HTTP Request Defaults
HTTP Header Manager
User Defined Values
    udvBranchCode
TestFragment
    JDBCRequest - Written query to get data from database
        JSR223 PostProcessor 
ThreadGroup - CreateTransaction
    IFController - ${__jexl3("${udvBranchCode}"== "",)} If udvBranchCode is blank then only run JDBCRequest
        ModuleController - Pointed to JDBCRequest JSR223 PostProcessor
HttpRequest
    JSR223 PreProcessor - Here checking I am checking if udvBranchCode has value; if not then value from the database will get set.

Below are the details
1. JDBCRequest's JSR223 PostProcessor

Here I am checking the resultSet size and then randomly storing the branch code as properties.

resultSet = vars.getObject("resultSetUserDetails")
if (resultSet.size() !=0 ) {
    Random random = new Random()
    int i = random.nextInt(vars.getObject("resultSetUserDetails").size())
    strBranchCode = vars.getObject("resultSetUserDetails").get(i).get("BranchCode")
    props.put("propsBranchCode" + ctx.getThreadNum(), strBranchCode)
} 

2. HttpRequest Details IFController

Here if branch code value is blank in user Defined Values then I am calling Database.

${__jexl3("${udvBranchCode}"== "",)}
  1. HttpRequest Details
{
  "TR": {
     "BranchCode": "${brnCode}"  -> Here brnCode is not populating
     "CustomerType": "${udvCustomerType}"
   }
   "Individual": {
        "FirstName": "Deepak",
        "MiddleName": "Kumar",
        "LastName": "Verma",
   }
   "Company": {
        "CompanyName": "Company",
        "StartDate": "23/01/2020",
    }
}
  1. Above HttpRequest's JSR223 PreProcessor Details

Here I am checking if branch code is not present in user defined values then set the code getting from the database. Also based on customer type removal of respective object/elements from the request is working fine. But for BranchCode I am getting issue, here instead of populating BranchCode values, this has been "${brnCode}" populated.

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

def oldRequest = new JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())

def bcode = props.get("propsBranchCode" + ctx.getThreadNum())
if (vars.get("udvBranchCode").isEmpty() == true) {
    vars.put("brnCode", bcode)
} else {
    vars.put("brnCode", vars.get("udvBranchCode"))
}

if (oldRequest.Transaction.CustomerType.equalsIgnoreCase("Individual")) {
    oldRequest.remove("Company")
}

def newRequest = new JsonOutput().toJson(oldRequest)
sampler.getArguments().removeAllArguments()
sampler.setPostBodyRaw(true)
sampler.addNonEncodedArgument('',new JsonOutput().prettyPrint(newRequest),'')

Solution

  • The issue has been resolved by replacing in HttpRequest's JSR223 PreProcessor Details

    • vars.put("brnCode", bcode)
      WITH
    • oldJSONRequest.TR.BranchCode = bcode

    Below

    import groovy.json.JsonSlurper
    import groovy.json.JsonOutput
    
    def oldRequest = new JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
    
    def bcode = props.get("propsBranchCode" + ctx.getThreadNum())
    if (vars.get("udvBranchCode").isEmpty() == true) {
        oldJSONRequest.TR.BranchCode = bcode 
    } else {
        oldJSONRequest.TR.BranchCode = vars.get("udvBranchCode")
    }
    
    if (oldRequest.Transaction.CustomerType.equalsIgnoreCase("Individual")) {
        oldRequest.remove("Company")
    }
    
    def newRequest = new JsonOutput().toJson(oldRequest)
    sampler.getArguments().removeAllArguments()
    sampler.setPostBodyRaw(true)
    sampler.addNonEncodedArgument('',new JsonOutput().prettyPrint(newRequest),'')