JMeter enable/disable HTTP Request Sampler upon certain condition - jmeter

I have a few HTTP Request Samplers that I would only like to execute when a certain condition has been met. What I have done is added a BeanShell PreProcessor to the HTTP Request Sampler with the following code
if (${getTeamName}.equals("Test Team") == true)
{
HTTPSampler.setEnabled(false);
}
If the value of getTeamName is Test Team then I want to disable that HTTP Request Sampler, as it doesn't have to be executed then. However it looks like that currently doesn't work.
Is there any one who knows what I'm doing wrong here, or a suggestion to what I should do?

As per JMeter Performance and Tuning Tips guide:
But of course ensure your script is necessary and efficiently written, DON'T OVERSCRIPT
Why not just to use If Controller like:
If Controller, condition: "${getTeamName}" != "Test Team"
HTTP Request Sampler
If ${getTeamName} will be Test Team child sampler(s) won't be executed.

While using beanshell, access variables using vars.get("VARNAME")
if (vars.get("getTeamName").equals("Test Team") == true)
{
sampler.setEnabled(false);
}

Related

How to call JSR223 Preprocessor in Jmeter only once for each request

In am using JSR 223 Preprocessor in my script. In script there is one API with the body data as below:
{`"key":"appStorage","value":"{\"model_sta10:\":\"{\\\"StratSim_Controls!R15C5\\\":\\\"10\\\",\\\"Inputs_Main!R23C5\\\":\\\"10amname\\\"}\",\"return_url\":\"/main/welcome-screen\",\`round_value`\":\"1\",\"round-one-star10d\":\"true\",\"intro-comple10\":\"true\"}"}
But when I execute the script it is not working getting the error and one slack got removed from the response.
So I added the JSR 223 preprocessor with below code for request:
`import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase
def request = sampler.getArguments().getArgument(0).getValue()
request = request.replace('te',vars.get('te')).replace('teamname', vars.get('teamname'))
def arguments = new org.apache.jmeter.config.Arguments();
sampler.setArguments(arguments);
sampler.addNonEncodedArgument('',request,'')
sampler.setPostBodyRaw(true)
`
and pass the parameter in body:
{"key":"appStorage","value":"{\"model_state:\":\"{\\\"StratSim_Controls!R15C5\\\":\\\"te\\\",\\\"Inputs_Main!R23C5\\\":\\\"teamname\\\"}\",\"return_url\":\"/main/welcome-screen\",\"round_value\":\"1\",\"round-one-started\":\"true\",\"intro-complete\":\"true\"}"}
`
But in script this request are present multiple times and I have to add JSR 223 preprocessor in each request, Do we have any solution that we can use the code only once for all request?
I tried with solution where I added the JSR223 preprocessor in the thread group but it is not working getting the error for getCalue().
It looks like there is at least one Sampler in the JSR223 PreProcessor's Scope which doesn't have a request body, i.e. you're sending a GET request without parameters.
You can use sampler shorthand and some if conditional structure to:
check whether the current sampler is a HTTP Request sampler
check whether it has at least 1 argument
Something like:
if (sampler instanceof org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy) {
if (sampler.getArguments().getArgument(0) != null) {
//your code here
}
}
If you put the JSR223 PreProcessor on Thread Group level it will be executed before each Sampler in the Thread Group so you might want to come up with more conditions to filter out the unwanted executions.
More information: The Groovy Templates Cheat Sheet for JMeter

how can I run multiple if controller in jmeter

I am currently working in a heavy load test, I have one login request which access with user and password and basic auth, I have to validate some info from the response and I am using assertions but I need to apply different kind of assert depending on the code response and to be able to do that I am using an if control putting the assertions inside as a child, the problem begins when I try to execute the assertions with an error code response, some how the if controller is not taking the value of the variable I created to store the code response. could some one help me? thanks!
You cannot put assertion as a direct child of the If Controller. In fact you can, however it will not make any sense as assertions obey JMeter Scoping Rules and since there is no any Sampler in the Assertion scope - it will simply not get executed.
I would recommend going for JSR223 Assertion where you have all power of Groovy SDK and JMeter API in order to set up your custom pass/fail criteria. The pseudo code would be something like:
if (SampleResult.getResponseCode().equals('200')) {
//do what you need when response code is 200
//for example let's check if response contains "foo" line
if (!SampleResult.getResponseDataAsString().contains('foo')) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Failed to find "foo" line in the response')
}
}
else if (SampleResult.getResponseCode().equals('300')) {
//do what you need when response code is 300
}
else if (SampleResult.getResponseCode().equals('400')){
//do what you need when response code is 400
}
else {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Unexpected response code: ' + SampleResult.getResponseCode())
}
References:
SampleResult documentation
AssertionResult documentation
Scripting JMeter Assertions in Groovy - A Tutorial

Jmeter - Common Response assertion for multiple request

I want a common Response assertion for 200 response code which should be applicable for each sampler request.
However it is also required for some sampler requests to overwrite that from sampler level as (response code 204,500).
Is there any possible way to achieve this?
JMeter Assertions obey Scoping Rules so if you put an Assertion on the same level as Samplers - it will be applied to all samplers
If you need to override expected response code for a specific sampler I would recommend changing the response code to 200 via JSR223 PostProcessor like:
if (prev.getResponseCode().equals('500')) {
prev.setResponseCodeOK()
}
this way the above Groovy code will change the individual sampler status code from 500 to 200 so "global" assertion will still be successful.
In Response Assertion you can add more Pattern To Test, so add 3: 200,204,500
And check the Or checkbox that you will allow to assert either one.
Note: Field to check is Response Code
Note: Pattern Matching rule can be Equals or Contains

How to add 'Or' condition in assertion

I want the request to pass in both the cases if response contains "Completed" or "Progress, Will take time to process".
But if I include both this assertions in response assertion, it will take it as 'and'. It will pass only if both are satisfied.
Here any one of this is sufficient. Please suggest.
You will need to go for an assertion which supports scripting, i.e. Beanshell Assertion
Add Beanshell Assertion as a child of the request which returns either "Completed" or "Progress" messages
Put the following code into "Script" area:
String response = new String(ResponseData);
Failure = !(response.contains("Completed") || response.contains("Progress, Will take time to process"));
Where:
ResponseData - byte array which holds parent sampler response
Failure - boolean which indicates whether parent sampler should be failed or not.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on how to use JMeter and Java API from Beanshell test elements and extend your JMeter tests with scripting.
Jmeter 3.2 allow to check using Or condition
Response Assertion now allows to work on Request Header, provides a "OR" combination

Jmeter If controller condition statement

I am trying to built a test plan in jmeter where i want to run test for specific HTTP request based on their names. I used if controller but I dont know what condition to write.
I am writing ${__samplerName()}=="HTTPRequestName" in the condition but it isn't executing.
kindly help me as soon as possible.
You need to surround ${__samplerName()} with quotation marks as follows:
"${__samplerName()}"=="HTTPRequestName"
See How to use JMeter's 'IF' Controller and get Pie. guide for more details on If Controller use cases and clauses.
In case if you need to run samplers basing on some condition, you can use JMeter Properties as follows:
Launch JMeter providing sampler name property like jmeter -Jrunsomesampler=true
Add If Controller with the following condition: ${__P(runsomesampler,)} == true
Add desired HTTP Requests as a children of the IF Controller

Resources