API Jmeter create a reusable request depends on the selection of the user

Does anyone know how simulate scenario below and write on a reusable format? Your response is highly appreciated. Thanks!

Example I have this userdefine variable name "userInput" if user tries to enter these following metrics it should generate the corresponding request as well. Note variable name may varries from time to time depends on user selection.

Scenario 1 – network,cpu,process

{
"networkMetric": "network.json",
"cpuMetric": "cpu_json",
"processMetric": "process_json"
}

Scenario 2 – cpu,process

{
"cpuMetric": "cpu_json",
"processMetric": "process_json"
}

Scenario 3 –network,cpu

{
"networkMetric": "network.json",
"cpuMetric": "cpu_json"
}

Scenario 4 – process

{
"processMetric": "process_json"
}

>Solution :

You can generate the "request" using a suitable JSR223 Test Element (i.e. JSR223 PreProcessor) and the following code:

def payload = [:]
vars.get('userInput').split(',').each { metric ->
    payload.put(metric + 'Metric', metric + '_json')
}

vars.put('request', new groovy.json.JsonBuilder(payload).toPrettyString())

you will be able to refer the generated request body as ${request} where required and the body will depend on the ${userInput} variable value.

enter image description here

More information:

Leave a Reply