Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
"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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading