I am new to JMeter.
In my GET request I want to have random number of parameters, so sometimes I want to have:
a = value1
a = value2
a = value3
and sometimes I want to have
a = value1
a = value2
etc.
Can I achieve it in JMeter in another way than making separate request modules?
You can specify parameters dynamically in the Path field, using a variable:
The variable should be created / formatted before HTTP request is sent. For example here I am using counter and BeanShell pre-processor to create a proper set of parameters:
So if I run this with 3 iterations, I will get:
GET http://stackoverflow.com/x?a=value0
GET http://stackoverflow.com/x?a=value0&a=value1
GET http://stackoverflow.com/x?a=value0&a=value1&a=value2
etc. Of course the logic of creating params should be based on your needs, this is merely an example. The reusable part of that example is saving params in a string, and then saving them into variable:
String myDynamicParameters = "";
// your logic here
vars.put("myDynamicParameters", myDynamicParameters);
If you want to generate variable directly inside your GET parameters, the fastest way is to use inline snippets.
something like:
GET http://xx.com/${__Random(1,99999)}
You can do it via i.e. Beanshell PreProcessor like:
sampler.addArgument("name", "value");
See:
HTTPSasmplerBase.addArgument() method JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component for comprehensive information on using JMeter and Java APIs in your test.
Related
In my test Plan, I extract a list of variables and stock them into a csv file, but I want to stock them into a list and use them in the next HTTP request
How to do it please ?
enter image description here
It is not possible to come up with the comprehensive answer without seeing what kind of "list" do you want to generate out of the JMeter Variables.
2 things are obvious:
CSV Data Set Config is initalized before beanshell test elements therefore you cannot use the data generated in beanshell this way.
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for any form of scripting
If the list needs to be something like [foo, bar, baz] you can do it like:
def list = []
vars.entrySet().each { var ->
if (var.getKey().startsWith('userID')) {
list.add(var.getValue())
}
}
vars.put('myList', list as String)
Once done you can reference the list as ${myList} where required.
I have a jmeter variable suppose ${employee} whose value is {empid:"E101",empname:"XYZ",empcity:"Chennai"}.
I want to use this variable in different Http calls for e.g.
demo.exmaplecode.com?Id=${employee}.empid
demo.exmaplecode.com?Name=${employee}.empmname
demo.exmaplecode.com?City=${employee}.empcity
I am not able to extract values by using "." operator.
Please suggest extracting values from an object type variable in jmeter.
You can achieve this fairly easily using __groovy() function available since JMeter 3.1, the relevant expressions would be something like:
${__groovy((new groovy.json.JsonSlurper().parseText(vars.get('employee'))).empid,)}
${__groovy((new groovy.json.JsonSlurper().parseText(vars.get('employee'))).empname,)}
${__groovy((new groovy.json.JsonSlurper().parseText(vars.get('employee'))).empcity,)}
Demo:
More information: Groovy: Parsing and producing JSON
Use JSON Extractor and choose radio button JMeter Variable and enter variable name
to get for example empid use the following setting:
Variable Names: empid (your new variable name for id)
JSON Path Expressions: $.empid
Match No.: 1
I have test case in my csv file. The request URL has a custom variable.
Sample URL : .../abc/$id
I need to replace this id by the id that we get in response from the previous request. I used json extractor to fetch the id from the response. Now I need to update this id in the next test case request. Fetched the Request URL from jmeter context using below code:
String path = ctx.getCurrentSampler().toString();
path.replaceAll("$id", id);
I am not able to set this updated URL in jmeter context (ctx)
You need to assign new path value to path variable
You need to set sampler path to the new value using sampler.setPath() method
So you need to amend your code like:
String path = ctx.getCurrentSampler().toString();
path = path.replaceAll("$id", id);
sampler.setPath(path);
Demo:
Also consider switching to JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black article for details.
Try to avoid the pre / post processors if possible.
Your requirement is very simple and straight forward.
Directly use this in the path - assuming id is the name of variable which has the value.
/abc/${id}
String pass = "123456789";
vars.put("token01",pass);
System.out.println(pass);
System.out.println(token01);
the result of System.out.println(pass) is right ,it is 123456789,
but the result of token01 is null, I can not understand .
If you need to access a JMeter Variable value programatically you need to consider one of the following options:
If your variable is a String: String value = vars.get("token01")
If your variable is an Object: Object value = vars.getObject("token01")
Demo:
References:
vars.get()
vars.getObject()
You may also find Groovy Is the New Black article useful.
You have to use vars.get to access jmeter variables.
So you should use System.out.println(vars.get("token01")). Also you can use debug sampler and view results tree combination to make sure your script is working fine see How To Debug Your Apache Jmeter Scripts
for more information on jmeter troubleshooting techniques.
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.