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

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

Related

I want to fail Beanshell sampler when my script is not conditions not satisfied

I am using Beanshell sampler with java code to compare the two files line by line. I want to fail the Beanshell sampler if comparison failed at any line. I see always my beanshell sampler is sucess in view results treee even the comparison failed or passed. So any one please give me a idea how to fail the sampler.
Note: I used Beanshell Assertion as well but its not worked.
View Results Tree Image
Beanshell Sampler with Beanshell Assertion
boolean differenceInFile = false;
SampleResult.setSuccessful(differenceInFile);
IsSuccess=differenceInFile;
line #2 and line #3 is what you need in your bean shell sampler to pass / fail depending on your file comparasion
Sample JMX File
It will show you as an xml, try to download it
There is SampleResult pre-defined variable which you can use for setting the sampler passed or failed, define response code, response message, response body, etc.
So something like:
if (there_are_differences) {
SampleResult.setSuccessful(false)
SampleResult.setResponseCode("500")
SampleResult.setResponseMessage("Files are different")
}
In the assertion you have AssertionResult for the same.
Also be aware that you should be using JSR223 Test Elements and Groovy language for scripting since JMeter 3.1 so it might be a good option for migration.
More information on JMeter API shorthands available for the JSR223 Test Elements - Top 8 JMeter Java Classes You Should Be Using with Groovy

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

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 find and replace a substring in sampler's response for web services?

I am using two soap/xml request samplers wherein response of one is to be used in request of the other. The issue is that the response of Sampler1 contains multiple occurrences of "a:" which has to be replaced by "eas1:" which can be used in Sampler2. Kindly suggest a solution.
I tried using beanshell postprocessor but could not come to any positive result.
Add JSR223 PostProcessor as a child of the Sampler1
Put the following code into "Script" area
def response = prev.getResponseDataAsString()
def request = response.replaceAll('a:', 'eas1:')
vars.put('request', request)
Use ${request} in the "Body Data" section of the Sampler2
References:
prev is a shorthand to SampleResult class instance which provides access to the parent Sampler result
vars is a shorthand to JMeterVariables class instance, it provides read/write access to JMeter Variables
String.replaceAll() method reference
Groovy is the New Black - guide to Groovy scripting in JMeter

JMeter enable/disable HTTP Request Sampler upon certain condition

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);
}

Resources