How to pass response from one beanshell sampler to another beanshell sampler in same thread in Jmeter - jmeter

I am making dme2 call to api from beanshell and i am getting response from it like {"stagedcustomerId":"165ce369-a9fb-4d42-b8f0-f119a6ae20eb"}
so now i want to pass only customer id value to another beanshell sampler for next api call as one of parameter in request body in same thread in jmeter.
Please suggest what can we do in this case. is there any way to do beanshell postprocessor?

You can use SampleResult shorthand in order to define the Beanshell Sampler response data like:
SampleResult.setResponseData("{\"stagedcustomerId\":\"165ce369-a9fb-4d42-b8f0-f119a6ae20eb\"}","UTF-8")
Once done you can add a JSON Extractor as a child of the Beanshell Sampler and configure it like:
That's it, now you will be able to access the extracted value as String id = vars.get("id"); in other Beanshell Sampler or as ${id} in any other test element.
Also be aware that starting from JMeter 3.1 it's highly recommended to use JSR223 Test Elements and Groovy language for scripting so consider refactoring your test on next available opportunity.

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

How to hardcode the value in JMeter

How to hardcode the value in JMeter? We need to hardcode field value for each request. Than after the response I need to crosscheck if the response are exactly matching the with the hard coded one. after that only we will be passing the test case. using Beanshell postprocessor
If you want to match the response against "reference" value the best option is using JMeter Assertions, to wit Response Assertion.
If your assertion logic is more complex than equals/contains/matches which can be implemented using the Response Assertion - be aware that since JMeter 3.1 it's recommended to use JSR223 Elements and Groovy language for any form of scripting so my expectation is that you should go for JSR223 Assertion instead.

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

How to use variable "sampler" in JSR223 Sampler (JMeter)

I'm finding the way to use the variable sampler in JSR223 Sampler, JSR223 PreProcessor, JSR223 PostProcessor, and all other JSR223 script.
There are some other variables like vars, props, prev, SampleResult. I can use them easily.
For example:
vars: vars.get("VARIABLE_NAME"), vars.put("VARIABLE_NAME","VALUE"), ...
props: props.get, props.put, ...
prev: prev.getTime(), prev.isSuccessful(), prev.getLatency(), ...
SampleResult: SampleResult.getResponseCode(), SampleResult.getResponseMessage(), ...
But I don't know how to use variable sampler. Only thing I can do with this variable is:
sampler.sample(): It helps to returns the Name of current Sampler
So, could anyome please let me know there is any other way to use this variable?
Thanks in advance!
For JSR223 Sampler sampler variable stands for JSR223Sampler, see the JavaDoc for all available methods and fields.
When it comes to JSR223 Pre or Post Processor - in that case sampler variable stands for parent sampler class instance, for instance in case of HTTP Request it will be HTTPSamplerProxy, for JDBC Request - it will be JDBCSampler and so on.
You can check exact class using Groovy expression like:
log.info(sampler.getClass().getName())
You can check How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on pre-defined variables and their usage. It is applicable for Groovy as well.
sampler is a Sampler object. you can use whatever the methods available here, not only methods declared in Sampler class but also the methods in super classes/interfaces like TestElement.
For example:
sampler.sample() - returns sampler's name
sampler.setProperty() - set a property by specifying key, value
sampler.setThreadName() - set thread name for the sampler.

Use BeanShell PostProcessor in If controller using Jmeter

I'm trying to create a file which contains a Regex variable.
I'm using BeanShell PostProcessor to create the file which add values from the vars.
I want to add it to If controller so it will add the value to the file only if a condition comes out as True.
My BeanShell code is:
artLink = vars.get("artLink");
// If you want to overwrite, then don't pass the second argument
f = new FileOutputStream("result3.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(artLink);
f.close();
When I take the BeanShell out of the If Controller, it creates a file, but when I put it back, no file is being created.
So I guess the problem is with the If condition.
My If condition is:
"${notFound}"=="AA"
The "notFound" is the regex var and "AA" is the default value.
So my question is if BeanShell PostProcessor can't be done under If controller, or is it my IF condition which makes the problem?
Beanshell Post Processor won't be executed if there are no other Samplers under the If Controller.
So the options are:
Switch from Beanshell Post Processor to Beanshell Sampler
Add another sampler which does nothing, i.e. Dummy Sampler as a child of the If Controller
See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.

Resources