How to find and replace a substring in sampler's response for web services? - jmeter

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

Related

Jmeter - how to return value in jmeter to HTTPS request

I was using JSR223 preprocessor and passing value to it's main request but value is not being return in that request.
How do I pass the value in the HTTPS request extracting from JSR223 preprocessor. In more I need to do parametrization. I am calling NodeJS code.
You need to use vars shorthand for JMeterVariables class instance in order to save the generated encoded value into a JMeter Variable.
i.e. in the last line of your script do something like
vars.put('password', response)
and in the HTTP Request sampler's request body use something like:
"password":"${password}"
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

How to pass response from one beanshell sampler to another beanshell sampler in same thread in 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.

How to get HTTP POST request body in Beanshell Preprocessor?

I am facing some trouble using jmeter. This is my use case, I am using CSV data source parameters to construct a HTTP POST request, the request body is read from a CSV column
which contains some placeholders like ${source_id}
I want to replace these placeholders with jmeter parameters which I am initialising through a regex/json extractor(read from the response of last PUT request). I tried using the jmeter variable name in the CSV file but the variable values are not getting substituted. I guess I will have to use the beanshell pre-processor to modify the HTTP POST request body. Can anyone help with the methods I can use to get the HTTP POST request body.
Something like
String requestBody = sampler.getArguments().getArgument(0).getValue();
should help.
sampler is a shorthand to parent sampler class instance, in your case it will be HTTPSamplerProxy, see the JavaDoc for all available methods and fields.
I would recommend considering migration to JSR223 PreProcessor and Groovy language as it is much faster and less resources consuming than Beanshell. Valid Beanshell code will be valid Groovy code so you should be able to convert to JSR223 elements with no or minimal changes. See Groovy is the New Black article for more details.

Parameterize tag value inside XML contained in the parameter at runtime in JMeter

I saved the whole XML in a DB table and am fetching the XML in JDBC sampler and using it in HTTP sampler. I want to parameterize a value inside a particular tag value inside this fetched xml at runtime. Could someone tell me how to do that. Thanks in advance
In http sampler ==> post body
xmldoc = ${xmlfromdb}
Here am able to fetch the whole XML and I can submit it successfully. How to parameterize a tag value inside this fetched xml at runtime.
You can do it via scripting like:
Add Beanshell PreProcessor as a child of the HTTP Request sampler
Put the following code into the PreProcessor's "Script" area
String body = sampler.getArguments().getArgument(0).getValue();
body = body.replace("Original Tag Value", "New Tag Value");
sampler.getArguments().removeArgument(0);
sampler.addNonEncodedArgument("", body, "");
sampler is a pre-defined variable available for Beanshell (and some other Test Elements) which stands for parent Sampler instance and in case of HTTP Request it's HTTPSamplerProxy. See JavaDoc for more information on available methods and fields and How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using JMeter and Java API from Beanshell test elements inside JMeter script.

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