Conditionally sending jmeter variables with HTTP request - jmeter

I am using JMeter to send HTTP POST requests.
My body of the request is JSON, for example something like {"Var1": "${Var1}","Var2": ${Var2},"Var3":"${Var3}"}.
These are set in the parameters of the HTTP requests with no name for the parameter. This works fine and I am able to send requests using the variables that I set in a beanshell pre processor (by setting the variables and using vars.put() ).
My question is how can I send programmatically through the preprocessor part of the parameters? For example:
if(a){
send parameters `{"Var1": "${Var1}","Var2": ${Var2}` as my JSON
}
else {
send parameters `{"Var3":"${Var3}"}` as my JSON
}
vars.remove() doesn't work for me as it removes the value from the variable but still sends it in the request (for example as "${Var1}").

Replace the preprocessor by a Beanshell Sampler that will compute a boolean value a and put it as a var:
vars.put("a", value)
Then use 2 If Controllers where each one will contain a sampler with the different parameters.
Condition of first one will be ${a} and for be it will be the negation of ${a}.

Just use the "Body Data" tab. You can conditionally create the JSON string and then just "print" the variable in the body data using normal placeholders.

The easiest and fastest way of achieving what you want to do is to use the JMeter if controller (Add -> Logic controller -> If controller).
You add an if controller to the Thread Group that you're working on and place your expression that returns a boolean in Condition (default Javascript). As a child node for the if controller you place the HTTP Request sampler that you want to fire in case the if is successful.
Suppose you want to send a request if a property that you are passing to JMeter exists:
${__P(media)}.length > 0
The you add another if controller with a negated condition for what you just checked with another HTTP Request sampler.
You're done.

Related

In jmeter, can we use few parameters with in what we declared in the HTTP request parameter section

In my case i have created one HTTP Request with all the possible parameter as below -
My .csv file is looking as below -
For some test case i need to send details in one or two parameter only, not for all. Now how can i do that in the same HTTP request without creating a new one?
Theoretically you can just send empty parameter values, just make sure that you have a blank value in the CSV file, i.e.:
param1,param2
foo,bar
baz,
,qux
Alternatively if you want to completely remove the parameters with empty values from the request you can add a JSR223 PreProcessor as a child of the HTTP Request sampler and put the following code into "Script" area:
def newData = new org.apache.jmeter.config.Arguments()
0.upto(sampler.getArguments().size() - 1, { idx ->
def arg = sampler.getArguments().getArgument(idx)
if (!arg.getValue().equals('')) {
newData.addArgument(arg)
}
})
sampler.setArguments(newData)
This way JMeter will remove the parameters which don't have their respective values from the request:
In the above example sampler stands for HTTPSamplerProxy, see the JavaDoc for all available functions decriptions
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It

ForEach Controller won't run multiple HTTP Requests

I have a ForEach Controller that loops over an array of values which works. Inside the ForEach block I have two HTTP requests and one extractor.
ForEach Loop {
+ HTTP Request 1 (This uses attribute from ForEach)
+ Extractor
+ HTTP Request 2 (This uses attribute from Extractor)
}
The first HTTP request runs and the extractor as well but the second HTTP request fails to run.
I've checked the logs and nothing is mentioned about this HTTP Request. I also tried moving the Extractor out from under the HTTP Requestor 1 but that also doesn't work.
There is no problem to have multiple Samplers as ForEach Controller children
The possible reasons for not executing the 2nd HTTP Request are in:
Your extractor fails somewhere somehow, double check that the variable is set and has expected value using Debug Sampler and View Results Tree listener combination
Your 2nd HTTP Request configuration is not correct, i.e. invalid characters in "Server Name" field or unsupported protocol or the request is actually being executed but takes too long, etc. I would recommend looking into jmeter.log file as normally it has enough troubleshooting information

How do you have multiple Regular Expression Extractors running in one test plan?

I'm trying to extract variables returned from one HTTP request, use them in a second HTTP request then extract the variables returned from the second HTTP request.
I then need to use both the first extracted and second extracted variables in a third HTTP request.
I can extract the variables from the first HTTP request and use these in the second HTTP request but cant seem to get the variables extracted from the second HTTP request to then use in the third HTTP request.
Is it even possible to do this?
This is my current set up
It should be straightforward and possible as long as both extractors are going into unique variable names. The Debug Postprocessor will show you the values stored in each variable after a test run. What do you see in those in the context of the third http request?

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

JMeter - do not pass post value in post data if null

In my JMeter script, I have one HTTP request which has 4 different parameters to be passed in post body. I have corresponding variables. Values of these variables are not available every time, depending on configuration.
If a value is not available, I get an error "bad request". How do I see if a variable is not null and only then pass corresponding parameter in request post body?
Given you have the following configuration:
and you don't want to send foo parameter if ${bar} variable is not defined
Add Beanshell PreProcessor as a child of your HTTP Request Sampler
Put the following code into the PreProcessor's "Script" area:
if (vars.get("bar") == null) {
sampler.getArguments().removeArgument("foo");
}
Where:
vars - is a shorthand to JMeterVariables class instance
sampler - shorthand to parent sampler implementation class instance, in this case - HTTPSamplerProxy
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Java and JMeter API from Beanshell scripts.
Just use the Logic Controller - If Controller. It allows to define the if statement using your variables. So, you can perform your actions only in case all parameters are not equal to null:
I've defined one single User Defined Variable in this example. Jmeter sends HTTP request only if it has a value defined.

Resources