jmeter : how to save parameter from response to external file and use it to enter another page? - jmeter

How can i Save parameter or any word from the response of the page to external file (txt ot csv)ang get this parameter from the file to enter another page ?
In fact I want to be able to change the parameter dynamically via file.
Thanks.

To save the whole response thing into a file:
Add a Beanshell PostProcessor as a child of the sampler, whose data you need to save.
Add the following code to the PostProcessor's "Script" area
import org.apache.commons.io.FileUtils;
File file = new File("/path/to/your/file.txt");
FileUtils.writeByteArrayToFile(file, data);
To read file contents in JMeter:
Just use __FileToString function wherever file's content is required as:
${__FileToString(/path/to/your/file.txt,,)}
For more information on Beanshell bit refer to How to use BeanShell: JMeter's favorite built-in component guide.
For high loads it will be better to replace Beanshell PostProcessor with JSR223 PostProcessor and choose "groovy" as the language (you'll need groovy-all.jar somewhere in JMeter's classpath)

Related

How to create a Java program in Beanshell PostProcessor in Jmeter to merge all the responses?

I have to send JSON requests based on the CSV test data. Suppose there are 90 records - which are basically the request bodies. I put the Thread in a loop to keep sending the request until the last one in the CSV.
Every time I get the response, I need to append them into a single CSV file. Now, since Jmeter Listener does not consolidate all the responses into CSV (I do not want it in xml), I want to know if I can write a Java snippet in BeanShell, capture all responses and write them to a CSV file.
You can use JSR223 Sampler with File.append adding text with , to append to CSV file
This will append to the end of the file.
File file = new File("out.txt")
file.append("hello\n")
If you want the "program"
Add JSR223 Listener to your Test Plan (since JMeter 3.1 it is recommended to use JSR223 Test Elements for scripting)
Put the following code into "Script" area
new File('myfile.csv') << prev.getResponseDataAsString() << System.getProperty('line.separator')
where prev stands for previous SampleResult class instance. Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for the JSR223 Test Elements.
A better option would be saving a response into a JMeter Variable using i.e. Regular Expression Extractor and writing it to a file via sample_variables property

Save body request in JMeter

I am using CSV file which contains variables for my XML requests. I know how to see the request sent, but I would like to save each request in a separate file.
Is that possible?
The easiest way seems to be using Flexible File Writer, you can save requestData into a file.
If you need more flexibility - for HTTP Request samplers you can do it with Beanshell Listener like:
Add Beanshell Listener to your Test Plan
Put the following code into the Listener's "Script" area
import org.apache.commons.io.FileUtils;
String data = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
int threadNum = ctx.getThreadNum();
int loop = ctx.getVariables().getIteration();
FileUtils.writeStringToFile(new File("request-" + threadNum + "-" + loop + ".txt"),data);
You'll get files like request-x-y.txt generated in JMeter's "bin" folder where:
x - JMeter Thread Number
y - Current Loop Iteration (Thread Group level)
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell in JMeter tests.
you should save the results as XML and turn on only the "Save Sampler Data (XML)"
Add > Listers> View Result Tree
In result tree click on config button and check the type of data thats has to be stored and provide the link to csv file.
Check the snapshot for more info.
Hope this helps.

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.

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.

jMeter How to get Multipart body in BeanShell PreProcessor

In jMeter How to get multi part body in BeanShell PreProcessor
I need to get the image data and post parameters
by using sampler.getArguments(); I am able to get the post parameters but not the image file
Please help me
You can use getHTTPFiles method of Sampler API.
sampler.getHTTPFiles() will return the file path HTTPFileArg in an array through which you can update new file at run time.
Update:
String path = sampler.getHTTPFiles()[0].getPath();
byte[] array = Files.readAllBytes(new File(path).toPath());
Something like:
File image = new File(sampler.getHTTPFiles()[0].getPath());
//do what you need with the image file
If you need extended image information take a look at ImageIO. For more Beanshell tips and tricks check out How to Use BeanShell: JMeter's Favorite Built-in Component

Resources