'Sample result's label is not changed through BeanShell post processor for some conditions
I made this code so that if status == 500 the name of the api would be equal to "FAKE_CLIENT_RETRY", if the status of the api == "ERROR", the name would be equal to "FAKE_CLIENT_CALLBACK_ERROR"
import org.apache.jmeter.samplers.SampleResult;
//process main sample
if(("${status}").equals("500")) {
SampleResult.setResponseCodeOK();
SampleResult.setSuccessful(true);
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_RETRY");
}else if(("${status}").equals("ERROR")){
SampleResult.setSuccessful(false);
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_ERROR");
}else{
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_SUCESS");
}
But even when status == "ERROR" the name it returns is "FAKE_CLIENT_RETRY"
The strangest thing is that I know that the execution entered the "if" of the condition == "ERROR", because the return that comes with status == "ERROR" appears with execution failure in Jmeter and I forced the return in this case to return with fails via code snippet:
SampleResult.setSuccessful (false);
But despite having entered, it ignores the snippet that asks to rename the api.
Jmeter Sreenshot ----> Jmeter response
Solution 1:[1]
Following script worked without any issue within BeanShell Post Processor
if(("${status}").equals("500")) {
prev.setResponseCodeOK();
prev.setSuccessful(true);
prev.setSampleLabel("FAKE_CLIENT_RETRY");
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_RETRY");
}else if(("${status}").equals("ERROR")){
prev.setSuccessful(false);
prev.setSampleLabel("FAKE_CLIENT_CALLBACK_ERROR");
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_ERROR");
}else{
prev.setSampleLabel("FAKE_CLIENT_CALLBACK_SUCESS");
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_SUCESS");
}
Please note that you have access to the SampleResult through the variable previous
. Lets use prev.setSampleLabel("Label");
to set the label of the sample result.
prev - (SampleResult) - gives access to the previous SampleResult
Migration to JSR223 PostProcessor+Groovy is highly recommended for performance, support of new Java features and limited maintenance of the BeanShell library
Add a Debug Post Processor to view the JMeter variables through view result tree.
Sample test plan (JMX) is available in GitHub.
Solution 2:[2]
You're setting an Api_Fake_Client_Name
variable value in the Assertion, it will be available (updated) either in the next sampler of during the next iteration of the current sampler:
Also be aware that starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so you could change your code to something like:
switch (vars.get('status')) {
case '500':
prev.setResponseCodeOK()
prev.setSuccessful(true)
vars.put('Api_Fake_Client_Name', 'FAKE_CLIENT_RETRY');
prev.setSampleLabel('FAKE_CLIENT_RETRY')
break;
case 'ERROR':
prev.setSuccessful(false)
vars.put('Api_Fake_Client_Name', 'FAKE_CLIENT_CALLBACK_ERROR')
prev.setSampleLabel('FAKE_CLIENT_CALLBACK_ERROR')
break;
default:
vars.put('Api_Fake_Client_Name', 'FAKE_CLIENT_CALLBACK_SUCESS')
prev.setSampleLabel('FAKE_CLIENT_CALLBACK_SUCESS')
}
More information:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dmitri T |