Modify a Property on JMeter - jmeter

I have created a Test Plan that has two thread groups. Each thread group has a SOAP/XML-RPC Request Sampler. Thread Group A also has a Regular Expression Extractor that contains:
Reference Name : ABC
Regular Expression :<response>([A-Z 0-9]+)</response>
Template: $1$
Moreover, Thread Group A has a BeanShellAssertion with
Name: Extract value
Script: ${__setProperty(ABC, ${ABC})};
What I want to do is modify the ABC variable and then pass it on the SOAP Sampler of the second Thread Group.
So, if ABC equals 1000 (response tag holds an int) I want to get that value divided it by two and then pass it on the second sampler like :
<abcValue>${__P(modifiedABC)}</abcValue>
Any ideas?
EDIT:
I have tried preProcessors(on the second thread group) and postProccessors (on the first thread group) but whatever I tried gave me back errors like:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String value = Integer.toString(Integer.parseInt(72295) /2); props.put("modifiedABC", v . . . '' : Typed variable declaration : Error in method invocation: Static method parseInt( int ) not found in class'java.lang.Integer'

If your response contains anything other than A-Z, ' ', or 0-9, the regex extractor will fail. It may be better to set the match group to (.+), so it collects whatever is in the response and use a separate regex assertion to check the contents are what you expect. That way you will get a sample fail when the results are bad, rather than a subsequent fail, when your next sample is badly formed through bad input.
In the Thread Group A assertion, you need some "s..
${__setProperty("ABC", "${ABC}")}
This sets a property called ABC to the value of variable called ABC, which is what I think you intend.
Easy way to divide your value is with __javaScript() function..
${__javaScript(${ABC}/2)}
You can use this anywhere in jmeter and it will substitute the value you require. Make sure you have retrieved the property at the start of Thread Group B, as the variable (ABC) is in different scope.

Thread Group 1
Please use 'Beanshell Post Processor' for your request. Add this post processor under the sampler where you extract ABC.
Below link will you give you an idea.
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor
Note that by default everything is String in Jmeter. So you might want to convert it to an Integer before dividing it by 2. You have to add something like this in the Beanshell Post processor.
modifiedABC = Integer.toString(Integer.parseInt(props.get("ABC"))/2);
props.put("modifiedABC",modifiedABC);
Thread Group 2
Now you want to access the modifiedABC in the second thread group.
Simply access it using
${__P(modifiedABC)}

Related

Jmeter - My response has multiple values of same id. I want to extract and pass these values incrementally to the next request

My response to a save action is somewhat like this-
"diagnosisId":45686,"confidence":0.0, --other text--
"diagnosisId":45966,"confidence":0.0,--other text-- etc. Say there are 27 diagnosis Ids.
Now i want to send request for the 1st Diagnosis Id in loop till the last id (multiple records/rows to save depending upon the diagnosis Ids).
Request is something like this -
"diagnosisId":45686,"confidence":0," etc.
I have extracted the Diagnosis Id using Regular Expression extractor and matched the first value -
"diagnosisId":(.+?),
How do I pass the values incrementally now?
Given you provide "Match No:" as -1 in the Regular Expression Extractor:
You will get the following JMeter Variables as the result (viewable via Debug Sampler):
At this stage you can add ForEach Controller and configure it like:
Input variable prefix: diagnosisId
Output variable name: anything meaningful, i.e. id
That's it, you can reference each consecutive ID as ${id} in the request(s) which will be the children of the ForEach Controller:
Another example: Using Regular Expressions in JMeter

How to capture dynamics values which will be generated from loop in JMeter

I have one scenario to test in which one transaction needs to be iterated for several times and then submit the request. For each iteration, I will get one detID (e.g: for 20 iterations - 20 unique detID). The problem is while submitting the request all the detID are been passing in the request parameters (example: if loop ran for 10 iterations then 10 detID are passing inside the request). I have put regular expression extractor to the transaction which is in loop which will capture all the mathces but it is capturing only the last one. (e.g: if loop ran for 10 iteration, regex capturing the 10th iteration value).
Please help me.I want to include these detID inside the submit request.
Add a Counter before the request with the regex.
Add a BeanShell PostProcessor as a child of the request after the regex with the below code:
String ID = vars.get("ID");// ID is the reference name of your regex
String Counter = vars.get("Counter");// Counter is the reference name of your regex
vars.put("ID_"+ Counter, ID);
You will have 20 different variables each one holds a different ID value and you can use them as ${ID_1} for the first ID and ${ID_2} for the second and so on.

Using Extracted JSON Value in Another JMeter Thread

First, let me preface this question that I've only been using JMeter for 36 hours.
I've been able to successfully create a thread that performs a POST (json body) to generate a new record.
{
"id":1257697771,
"displayName":"TERM2",
"functionName":"f_1257697771",
"displayableSourceExpression":"TRUE",
"typeId":200,
"groupId":300,
"clobObjId":1257697772,
"typeCode":5,
..........
}
I need to take the new record's ID (1257697771) value returned in order to perform updates, get by ID, delete, etc. on this record in other threads.
After much reading, I've created a Regular Expression Extractor where:
Apply to: Main Sample Only
Field to Check: Body as Document
Reference Name: newRecord
Regular Expression: "id":(.+?)\,"displayName"
Template: $1$
Match No: 1
Default Value: NONE
At this point, I'm not sure if my Regular Expression is formatted correctly where (.+?) is valid.
Also, I'm confused if I can either just specify the new reference (newRecord) in another thread's HTTP request's Parameters or use a BeanShell Post-Processor, or a Response Assertion, etc....
There a lot of answers for the same function of "Passing". Not being a programmer, I've tried to follow the discussion "how to extract json response data in jmeter using regular expression extractor?", but I'm still not clear.
Any insight is appreciated. Thanks.
JMeter Variables are local to Thread Group, you need to convert your variable to JMeter Property.
Use:
__setProperty() function in the Thread Group where you define your newRecord variable like:
${__setProperty(newRecord,${newRecord},)}
__P() function to access property value like:
${__P(newRecord,)}
See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more detailed explanation.
Also be aware of the Function Helper Dialog as it looks like JMeter functions syntax was developed by aliens.
To pass a value between threads you need to use the jmeter property function.
In a jsr223 postprocessor using groovy the code to get the value is as follows:
def userProperty = props.get('propertyToGet')
vars.put('userProperty', String.valueOf(userProperty))
You would then access the variable in your thread using:
${userProperty}
Or you can use shorthand directly:
${__P('propertyToGet')}
Variables in jmeter are thread specific.
Thanks everyone. I was able to resolve it with your help!
In the first thread:
set the Reg Expression Extractor Regular Expression = "id":(.+?)\,"displayName"
added a Bean Assertion where Parameters = ${__setProperty(newRecord,${newRecord},)}
In the second thread:
appended the Path url with ${__P(newRecord,)}
Executing the first thread (POST) resulted an new record with a unique ID. (1257698108)
Executing the 2nd thread (GET) shows
GET http://server/.../.../.../.../1257698108
And returns the exact data generated in the first thread.
Thanks everyone for your help!

How can I use the value of prev HTTP Request to the next HTTP Request in jMeter?

I'm newbie in jMeter and I'm working with it.
I have one Thread Group which consists of two Loop Controllers.
In 1st Loop Controller, there's a HTTP Request which contains body data
{ ..., "var1": "var-${__RandomString(10,1234567890abcdefg)}", ... }
In 2nd Loop Controller, there's also another HTTP Request that contains var1, and I want to asssign this var1 with same value as the prev var1 in the 1st Loop Controller.
I have tried use User Defined Variables, but it generated same value for another Thread Groups, I want generate different value of var1 for each Thread Group.
Can anyone help me what should I do to make it works? Thank you :)
Add Post Processors --> Regular Expression Extractor to the Http Request which you want to fetch the variable named var1.
As the above picture is shown use "var1": "(.+?)", as Regular Expression. The (.+?) part defines that Hey! Regular Expression Extractor, please fetch for me whatever is between "var1": " and ",, which you'll find in the response content. So var-${__RandomString(10,1234567890abcdefg)} would be fetched into a variable named Var1 (Because you defined Var1 as the Reference name.
Then, in the Http Request where you want to access that variable, use ${Var1} in order to send the value of Var1 variable as a request parameter. Something like the picture below
Note that the last value in the loop will be saved in Var1. If you want to save all values in the first loop in order to use those in the second loop,There's many way to do that.
You can add a Counter from Config Element to the first loop Controller and define for example loopCounter as the reference name for it. Fill the start and increment fields with 0 and 1.
Then change the regular expression to Var${loopCounter}
In the next loop's Http Request you can access these variables with ${Var1}, ${Var2} and ...
Just change your __randomString() function to look like:
${__RandomString(10,1234567890abcdefg,var1)}
it will overwrite ${var1} with random generated value so you'll get the same random string in both loop controllers.
See How to Use JMeter Functions articles series for more information on the above and other functions.

Variables in httprequest post body

I'm trying to generate a jmeter script where a unique folder is created each time the script is run - adding a variable of some sort to the folder name, such as a username+timestamp, should be enough to guarantee uniqueness. However, jmeter is not resolving the variable to its value - although it is when the variable is read out of a csv file (which isn't suitable).
Basically, I'm editing the PostBody in the http request, as follows:
{"alf_destination":"workspace://SpacesStore/90368635-78a1-4dc5-be9e-33458f09c3f6","prop_cm_name":"Test
Folder - ${variable}","prop_cm_title":"Test
Folder","prop_cm_description":"Test Folder"}
where variable is basically any variable I've tried so far (such as a random string, timestamp, etc.)
Can anyone suggest how to get the variable resolved?
You can use jmeter (since 2.9 version) uuid feature -> http://jmeter.apache.org/usermanual/functions.html#__UUID
${__UUID}
and
1) If you want just 1 value for the whole test, add a "User Defined
Variables" Config Element to your test. This will be evaluated when
you load the test script the first time.
2) If you want to have the value change for every thread execution,
but stay the same during each thread instance: under your 'Thread
Group', add a 'Pre Processors -> User Parameters' to your thread group
- and add the variable there.
Also, if you want the value to change each time the thread starts over
(each 'iteration' of the script within the thread group), you can
check the "Update Once Per Iteration" box on the User Parameters - and
it will get a new value each time it starts the thread over at the
beginning of th test script (within that thread group).
http://mail-archives.apache.org/mod_mbox/jmeter-user/201208.mbox/%3C004301cd853e$0c4a60c0$24df2240$#gmail.com%3E
With JMeter 2.9, the following works:
In HTTP Request Sampler, Tab "Post Body" add for example your JSON data and include the variables in it:
{"uuid":"${new-uuid}"}
new-uuid is a user defined variable.
This will send (from View Results Tree, Tab "Request"/"Raw"):
POST data:
{"uuid":"a1b2c3d4e5f6"}
I did this by referencing a variable in the http request post body - ${formvalues} - created using a beanshell preprocessor which is appended to the http request sampler. Beanshell contents:
double random = Math.random();
String formvalues ="{\"alf_destination\":\"workspace://SpacesStore/90368635-78a1-4dc5-be9e-33458f09c3f6\",\"prop_cm_name\":\"Test Folder - ${uname}_" + random + "\",\"prop_cm_title\":\"Test Folder\",\"prop_cm_description\":\"Test Folder\"}";
vars.put("formvalues",formvalues);
So this creates a folder with the username (${uname}, taken from the csv) plus a random number - it's crude as there could potentially still be cases where the script tries to create a folder with the same name as an existing one, but it will work for my case.
suppose you have the value "NewYork" in jmeter variable "Location".
Use it like this in HTTP POST BODY DATA:
{location:"${Location}"} => which gets interpreted as {location:"NewYork"}

Resources