How to validate JMeter user defined variables?

1.6k views Asked by At

I'm new to JMeter, I want to validate a JMeter test input variables defined as part of "User Defined Parameters". Let's say I have a variable "sessions" and my tester should pass input values in between 0 to 30 for sessions, if tester passes other than this range the test should not go further and should throw an error with appropriate message.

Is it possible with any kind of JMeter controllers/assertions/... without writing code for validation?

2

There are 2 answers

0
Dmitri T On

You cannot achieve such validation in GUI without amending JMeter source code but you can check the variable range using scripting.

For example, add a JSR223 Sampler as a child of the first request and put the following code into "Script" area:

import org.apache.commons.lang3.Range;

int sessions = Integer.parseInt(vars.get("sessions"));
String errorMessage = "Provided sessions number is not between 1 and 30, stopping test";

if (!Range.between(1, 30).contains(sessions)) {
    log.info(errorMessage);
    SampleResult.setSuccessful(false);
    SampleResult.setResponseMessage(errorMessage);
    SampleResult.setStopTest(true);
}

Demo:

JMeter JSR223 Conditionally Stop Test

Make sure you are using Groovy as a language (the option should be default as per JMeter 3.1, if you are using earlier JMeter version for some reason - you will have to choose groovy from the "Language" dropdown)

0
Naveen Kumar R B On

I am not sure is there any efficient/direct way of doing this. but I achieved your requirement as follows:

  1. Add Setup Thread Group (before any other ThreadGroup). select radio button Stop Test Now in Action to be taken after a Sample error
  2. Add Debug Sampler to the Setup Thread Group.
  3. Add Response Assertion (RA) to the Debug Sampler.
  4. In RA, Select JMeter Variable in Apply to section.
  5. In RA, select Matches in Pattern Matching Rules section.
  6. In RA, add the regex ^[0-9]$|^0[1-9]$|^1[0-9]$|^2[0-9]$|^30$ in Pattern to test text area.

Test will be stopped if you provide sessions value other than 0-30 in User Defined Variables, as Setup Thread Group is configured to Stop Test Now on Sample error.

Note1: Added View Results Tree Listener for confirmation whether the test is continued and also to check the error message. View Results Tree is added only for visual confirmation, must be removed during the load test, does not effect the actual logic.

Note2: I am not aware of any component, which can show custom message/alert to the user. so, used View Results Tree. We should remove this command during load testing. I added here for visual confirmation purpose. If not present also, Test will be stopped on wrong value for sessions, i.e., other than 0-30

Note3: We need a Sampler component in order to apply an Assertion. so, added Debug Sampler. Debug Sampler just reports all the JMeter variable values at the point of its execution.

Image references:

Setup Thread Group: enter image description here

Response Assertion: enter image description here

View Results Tree: enter image description here