Parameterize tag value inside XML contained in the parameter at runtime in JMeter - 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.

Related

How to send complete form-urlencoded post data from preprocessor

I know we can send/initialise the post data parameter variables from preprocessor but my requirement is i want to send complete post data/pay load which is shown in my screenshot from jsr223 preprocessor.
You have sampler shorthand which stands for HTTPSamplerProxy in the JSR223 PreProcessor
There is HTTPArgument class where you can specify name, value and whether they're encoded already or JMeter should perform the URL-encoding itself
So for each parameter you want to pass you need to add the line like:
sampler.getArguments().addArgument(new org.apache.jmeter.protocol.http.util.HTTPArgument('parameter_name', 'parameter_value', false))
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

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 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.

Extracting value from jmeter post request

I want to extract value of the parameter sent through post request in jmeter.
I know the use of regular expression for extracting response value or request URL but here I would like to extract the value of post request.
I've been thorough how to extract value from request in Jmeter but it didn't worked.
Not sure why do you need it as given you sending "something" you should already have that "something" hence you don't need to extract it, however here you go:
In order to save 1st parameter value (or the whole post data if you use "Body Data" mode):
Add Beanshell PostProcessor as a child of the HTTP Request.
Put the following code into the PostProcessor's "Script" area:
String request = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
vars.put("request", request);
You will be able to access extracted value as ${request} where required.
Clarifications:
ctx - shorthand for JMeterContext class instance
getCurrentSampler() - in case of HTTP Request sampler stands for HTTPSamplerProxy
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java API from Beanshell test elements in your JMeter test.
I added a Beanshell PostProcessor in my http request with following code.
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
Arguments argz = ctx.getCurrentSampler().getArguments();
for (int i = 0; i < argz.getArgumentCount(); i++) {
Argument arg = argz.getArgument(i);
String a = arg.getValue();
vars.put("EMAIL",a);
}
Explanation: I get a my request as a json and put it in EMAIL. Now I can use EMAIL as a variable in my other request.
Then, I added a jp#gc Json Path Extractor and I applied it to a Jmeter Varaible.
Now, Email will be used as variable, which contains my json request and I can extract using jsonPath Extractor.
An easy way to do this is using the JSON Path Extractor.
There are just
For the example you gave
{ "data" : { "name" : "john_doe", } }
'Variable Name: YourNewVar'
'JSON Path: $.data.name'
Should work, but you may need to do some experimenting.
You may want to add a "debug sampler" (its one of the standard samplers) and put in its title $YourNewVar so you can see what is being extracted.
Beanshell and "Regular Expression Extractor" will work, of course, but may be a little harder to use if you are not familiar with them.

how can i pass dynamic values to url in jmeter

I have to give dynamic values to url which takes number of users and their age , which can be selected though web page. but I want to give it in Jmeter using BeanShell PostProcessor.
Help me in this,since I'm new to Jmeter.
This is the path:
/destinations/packages?airports%5B%5D=LGW&units%5B%5D=000577%3ADESTINATION&when=29-05-2016&until=&flexibility=true&flexibleDays=3&noOfAdults=2&noOfSeniors=0&noOfChildren=1&childrenAge=3&duration=7114&first=0&searchType=search&searchRequestType=ins&sp=true&multiSelect=true
from what I've got looks like you can use CSV Data Set Config.
Create .txt file with the data you want to feed your test with;
Place the above .txt file to the folder where your .jmx file lies;
In your Test plan: Under your request sampler - place CSV Data set Config;
Then if you need to use your dynamic values within one threadgroup => refer these data as ${quanity}, ${age} in you url.
If you need to pass these values across the threadgroups => add BeanShell Assertion
Then (in the other Tread group) refer those as ${__property(_quantity)},${__property(_age)}.
Hope, it helps.
First of all you need Beanshell PreProcessor, not Beanshell PostProcessor.
All this parameters are basically name/value pairs which can be defined via HTTPSamplerBase class. HTTPSamplerBase class instance is available to Beanshell PreProcessor as sampler pre-defined variable so if you add the following code into the Beanshell PreProcessor "Script" area
sampler.addEncodedArgument("airports[]","LGW");
sampler.addEncodedArgument("units[]","000577:DESTINATION");
sampler.addEncodedArgument("when","29-05-2016");
sampler.addEncodedArgument("until","");
sampler.addEncodedArgument("flexibility", "true");
sampler.addEncodedArgument("flexibleDays","3");
sampler.addEncodedArgument("noOfAdults","2");
//etc
Your HTTP request will be populated with the values, you set via Beanshell.
JMeter Variables can be accessed via vars shorthand which stands for JMeterVariables class instance.
String airport = vars.get("airport");
sampler.addEncodedArgument("airports[]", airport);
//etc
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on how to use Beanshell Test Elements in Jmeter.
Remember that it is recommended to avoid scripting where possible so if there is another way of implementing your task - go for it.

Resources