how can i pass dynamic values to url in jmeter - 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.

Related

Passing one api parameters value to another api

In JMeter I have two Api , one api generate filename and id then these parameters pass to another api here I used plugin path extractor and also use csv data set config to extract , save and pass parameters and its value to another api but problem is when multiple user it generate multiple filename and id but how to pass those file name and id to every httprequest to another api.
You don't need any CSV Data Set Config, it will be sufficient to
Add a suitable Post-Processor to extract the generated file name
The Post-Processor will store the generated name into a JMeter Variable
You should be able to use the variable in the "2nd API"
As per JMeter Documentation Variables are local to a thread so each thread (virtual user) you define in the Thread Group will have its own value.
Demo:
More information on JMeter Correlation concept: Advanced Load Testing Scenarios with JMeter: Part 1 - Correlations

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

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: Pull paths from file at random

I am building a load/stress test for some webpages. I have a HTTP Request Default set up that has the base server name. I would like to use a Random Controller and HTTP Request to check all pages at random, but I do not want to have to make 150 HTTP Request that each hold a unique path. I would rather have one HTTP request that pulls a path at random from a file.
Is what I am describing possible? Can anyone point me in the right direction?
In fact it's possible. Everything is possible. In case of JMeter you'll need to do some scripting.
Given the following test plan structure:
setUp Thread Group
Beanshell Sampler
import org.apache.commons.io.FileUtils;
List lines = FileUtils.readLines(new File("/path/to/your/file"));
bsh.shared.lines = lines;
Thread Group
HTTP Request Sampler, Path: ${randomline}
Beanshell PreProcessor
List lines = bsh.shared.lines;
Random rnd = new Random();
vars.put("randomline", lines.get(rnd.nextInt(lines.size())));
It will be possible to use random URL from file as HTTP Request Path.
Explanation:
setUp Thread Group - special Thread Group type which is executed before any other Thread Group. The idea is to read file only once.
Beanshell Sampler - uses FileUtils library to read all the lines into lines array and bsh.shared namespace so the array will be accessible globally to all Thread Groups
Beanshell PreProcessor - uses Random class to generate random line number, obtains random value from the lines array and stores the value into randomline variable via JMeterVariables class.
Refer generated random line as ${randomline} where required.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter and a kind of Beanshell cookbook.
I re-worded my searching and have found that this is not possible. I think I am going to take the advice from another forum and randomize my file to achieve this. Seems to be the simplest solution.

Want to save Correlated variable locally in Jmeter

I want to save correlated variable locally (localdrive) then i might need to edit that variable with other dynamic value and then i should send it to subsequent request in Jmeter.
Please suggest me a way to perform the same.
Correlation in JMeter works as follow:
Add a Regular Expression Extractor on the sampler that contains the data you want to extract, example:
JMeter will automatically add a variable QUANTITY that will contain the extracted data
If you want to modify the variable you will use a JSR223 Sampler
In the next sampler you can use the data as ${QUANTITY}
No need to store anything on disk.

Resources