JMeter "Parameters" variable not convertable to Integer - jmeter

I can't get my ${invoiceTotalCsv} parameter to respond to math equations. I am taking it from from a CSV file in which it is a number, usually with a decimal place.
The below code works, but if ${invoiceTotalCsv} = 10, then the log writes 101, not 11. This leads me to believe the variable is passed as a string. BUT, I've tried Parameters.toInteger() which throws a NumberFormatException.
No other clues are given by JMeter.

You need to convert String to number/int, in groovy script:
def total = Parameters as double

You can use JSR223 Sampler element and then convert your Jmeter variable in int, float or double as required.
For better understanding see this page

Related

Combine two string variables into one in Jmeter

I have a JSON API request that contains these lines (and many others)
"vatNumber": "${variableC},
I already have a variable called variableA, which generates a random number string. I also have a variable called variableB which is always going to be "01" as a string, i.e. variableC = variableA + variableB.
I've tried this in a preprocessor, but it doesn't work.
String var1 = vars.get("customerOrgNr");
String var2 = vars.get("vatSuffix");
vars.put("customerVatNr", var1+var2);
Also tried
"vatNumber": ${variableA}${variableB},
but that doesn't work either. The json request is not accepted.
Your "doesn't work" statement "doesn't tell" anything to us.
If you just need to concatenate 2 values - go for ${customerOrgNr}${vatSuffix}, it will generate combined string
If you have variable like foo01 with the value of bar and want to use this value - go for __V() function like ${__V(${customerOrgNr}${vatSuffix},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables
I also think you need to surround your value with quotation marks:
"vatNumber": "${customerVatNr}",
Check the request in View Results Tree listener and check the generated JSON again i.e. online JSON Validator

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.

In Jmeter, What would be syntax of parameters in Body Data section of HTTP Request Sampler, for Rest APIs and input should be generated dynamically

I'm trying to generate my input through JavaScript using BSF PreProcessor. Currently I've only BSF PrepProcessor and a HTTP Request. My Request body goes like this.,
{
"entity": {
"id": "${EnitityName}",
----
}
}
The randomly generated string should be the value for ID, i've parameterized how we normaly do it. This is not working. Can somebody tell me where i'm going wrong or any alternative approach.
try using __RandomString function: (no need of JavaScript & BSF PreProcessor if you want a random string)
{
"entity": {
"id": "${__RandomString(8,abcdefghiz,)}",
----
}
}
From Docs:
Examples:
${__RandomString(5)} will return a random string of 5 characters which
can be readable or not
${__RandomString(10,abcdefg)} will return a
random string of 10 characters picked from abcdefg set, like
cdbgdbeebd or adbfeggfad, …
${__RandomString(6,a12zeczclk, MYVAR)}
will return a random string of 6 characters picked from a12zeczclk set
and store the result in MYVAR, MYVAR will contain string like 2z22ak
or z11kce, …
Given ${EnitityName} variable is properly set in your BSF PreProcessor script, it should be substituted by the generated value given BSF PreProcessor success.
I would suggest checking jmeter.log file for any errors - it should give you an idea regarding what went wrong. You can also share your BSF PreProcessor code so others and myself could take a look and suggest the fixes.
Actually according to JMeter Best Practices it is recommended to use JSR223 Elements so you should switch to JSR223 PreProcessor and Groovy language, the example code which generates a random alphanumeric string of 10 characters and putting the value into the ${EntityName} JMeter Variable will look like:
vars.put("EnitityName",org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(10))
References:
RandomStringUtils
JMeterVariables
Groovy Is the New Black

Jmeter : random number with beanshell

I want to add 2 kind of parameters to a post request.
From an http request I extract 2 list of variables:
Order_id = input type="hidden" name="(drag.+?)" value="(\d+?)"
Weight_id = class="draggableviews-weight form-select" id=".+?" name="(drag.+?)"
In the Post Request that follows, I need to repost all this variables. I use a BeanShell PreProcessor for this. The first list is no problem, since this get the same value. The second list should get a new random value between -50 and 50. I also want a different random value for each iteration.
How should I do this ?
If I use Random Variabele Config Element I get the same random int for each variabele. I want a different one for each iteration.
enter image description here
I would recommend using ThreadLocalRandom.nextInt() method like
sampler.addArgument(name2, String.valueOf(java.util.concurrent.ThreadLocalRandom.current().nextInt(-50, 51)));
Don't inline JMeter Functions into Beanshell scripts, either pass them via "Parameters" section and refer via Parameters or args shorthands or use appropriate code-based substitutions.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter tests.
To retrieve multiple values from single Regular Expression Extractor, we use Templates as follows:
then refer groups as follows:
In the image, you can see that link is Reference Name and there is only match found with the regular expression.
number of matches is : link_matchNr
first group value : link_1_g1
second group value : link_1_g2
Note: The regular expression I tried on is google.com, you can also simulate the same as follows:
Use Random function as follows:
value2 = ${__Random(-50,50)};
log.info("valuee2 " + value2);
Use Random Variable:
then, refer Output Variable in Beanshell Preprocessor as follows:
value2 = vars.get("randomInt");

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