Get Random value from key value pair using Groovy scripting - jmeter

I have a key value pair
def mp = [ 'QLD': 'Brisbane','NSW': 'Sydney','NT': 'Darwin']
I can get specific value using mp.get("QLD"). But how can we get a random value from dictionary?. Can you please help in this regard

Maybe something like:
mp[mp.keySet().asList().get(org.apache.commons.lang3.RandomUtils.nextInt(0,mp.keySet().size()))]
org.apache.commons.lang3.RandomUtils.nextInt(0,mp.keySet().size() - get random index of keys using RandomUtils class
mp.keySet().asList().get() - get key name by index
mp[] - get value
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Related

How to generate Random number in Jmeter with prefix and store it into variable

I am using JMeter's Function Helper Dialogue and i found below syntax -
Syntax - ${__Random(12,99,customer-id)}
Result - random values b/w 12 to 99 getting generated, which will get stored in variable "customer-id"
Now Problem is I have to generate value with prefix 'test' (say-test12) and store it in variable 'customer-id'
How to do that ?
I don't see why would you need this because JMeter Variables can be normally concatenated with plain text so if you need test12 just use test${customer-id} statement where required.
However if you really need to generate some static text followed by a random number you could go for __groovy() function configured like:
${__groovy('test' + org.apache.commons.lang3.RandomUtils.nextInt(12\,99),customer-id)}
Demo:
More information:
RandomUtils JavaDoc
Apache Groovy - Why and How You Should Use It

JMeter - compare assersion between two variables

I have two variables (create_date) took from SQL queries, and I want to validate the results.
Using Regular Expression I fetch both created date and I want to make sure that the dates is not equal.
I have created the following BeanShell assersion to compare both variables:
enter image description here
however, I'm getting the following error:
enter image description here
You're mixing 2 approaches:
Referencing variables like ${CreateDateBefore} in scripts (which you should not be using at all)
And using vars shorthand for JMeterVariables class instance
Apart from this you're using double to store some form of a timestamp
So you need to update your code like:
String var1 = vars.get("CreateDateBefore");
String var2 = vars.get("CreateDateAfter");
Also be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion
And last but not the least, you can compare 2 JMeter Variables using "normal" Response Assertion configured like:

JMeter - how to get test group's max loops?

I want to get the max number of loops in the test group. But how?
I do not see anything to do with loops here
JMeterContextService.getContext().getThreadGroup()
Number of loops is not about the Thread Group, it is property of the underlying Loop Controller so you need to amend your code like:
ctx.getThreadGroup().getSamplerController().getProperty('LoopController.loops')
Demo (assumes using __groovy() function):
More information on scripting in JMeter: Apache Groovy - Why and How You Should Use It
You can follow the convention of setting loop count as a property for example using ${__P(loopCount)}. Then get the property number with default in groovy:
props.get("loopCount", "1") as Integer
This is assuming you call JMeter with overriding property, for example:
jmeter -n -t -JloopCount=6 my.jmx

How to increase int value obtained from previous response

I'm using jmeter and I would like to automate below scenario :
(In general I would like to increase value, I already know how to extract value from previous request)
Execute request_1
Extract value1 from request_1 using Regular expression extractor
Increment value1.
Put new value (increased) to the request_2
Any idea how can I achieve it ?
Check out __intSum() function, you can sum an arbitrary number of arbitrary integers via it.
Given you have a JMeter Variable called yourVar where the extracted value lives, the relevant __intSum() function configuration to increment ${yourVar} value by 1 will be something like:
${__intSum(${yourVar},1,yourVar)}
Demo:
If the value you're getting from the Regular Expression Extractor is more than 2 147 483 647 you will need to use __longSum() function instead.
See Apache JMeter Functions - An Introduction guide for more information on JMeter functions concept.

How to split the data from csv and store in a variable for later use in Jmeter

I tried my best but no luck. Experts please help
I'm trying to get the vin numbers from csv file and using BeanShellPreProcessor to split
Here is my code
"vin" is the datasheetname and storing in variable t. I can successfully print the value of t in the Jmeter console.
Now I took the value that I got in t, and want to get the string starting from 10th Index to 17th Index and want to put that splitted string into a variable that I can use in my SOAP Request.
But when I use in the SOAP Request like this ${output}, it is getting as is instead of the value
what am I doing wrong?
String t = vars.get("vin");
System.out.println(timer);
string varm = t.substring(10, 17);
vars.put("output", varm.toString());
You made a mistake in second line:
System.out.println(timer)
should be:
System.out.println(t)
And
string
should be:
String
By the way you should use Groovy within JSR223 rather than beanshell

Resources