JMeter: Can't copy CSV variable into another variable - performance

I am reading a token from a .csv file into variable CSV_ACCESS_TOKEN. I have 3 request under one ThreadGroup. I want a scenario when logged in user loads a page thrice (or N time). So 1 thread is looping N time. After reading token once, I dont want to read next token in loop but want to loop through URL three (or N) time with same token.
Right now I am reading data from CSV, and using "BeanShell Sampler" inside "Once only Controller". In the sample I am using line like: vars.put("ACCESS_TOKEN",vars.get("CSV_ACCESS_TOKEN"). But that BeanShell sampler is recorded in my Summary Result. I don't want that.
I tried using "User Defined Variable" controller and try to assign the value ${__evalVar(CSV_ACCESS_TOKEN)} but it return empty value for ${ACCESS_TOKEN}. When I use ${CSV_ACCESS_TOKEN}, it shows me the values. If I use some other variable instead of CSV_ACCESS_TOKEN in UDV controller, it assigns the value from other variable and I see values for ${ACCESS_TOKEN}.
Why CSV variable is not assigning the values in regular variable.
Thanks
Vinay

If you have 3 requests, I suggest you put a Beanshell preprocessor on the first request, which copies the CSV_ACCESS_TOKEN to ACCESS_TOKEN.
Each of your samples can the use ACCESS_TOKEN, so CSV is accessed once per cycle of 3.
Each time the preprocessor run (ie before each 1st request), CSV_ACCESS_TOKEN will get updated from the dataset.
If it is the same request, which you do not wish to duplicate, you can look into use of test fragments and modules, so you can run the same sample from a variety of controllers. First from a simple controller with the preprocessor attached, and then from a loop controller to perform 2 more requests.
I think the code you have used already to manipulate the CSV values will continue to work in this scenario.

Related

Can a request parameter extracted from response of previous request have different values when run multiple times - Jmeter

Scenario:
We are extracting a value from 1st request and passing it as a parameter for the 2nd request.
The 2nd request is in Loop controller and it is run multiple times, But every time the 2nd request runs , It should take different value. Is there any way to do this.
Eg: Below is the example screenshots for the same. data is the variable which is passed to the second request .When second request is hit multiple times , It should extract different values.
In the Regular Expression Extractor set "Match No" to -1 to you will extract all the matches into:
data_1=1
data_2=2
etc.
In the Loop Controller set the "Loop Count" to ${data_matchNr}. This way the controller will iterate as many times as there are matches in the Regular Expression Extractor
Instead of ${data} use ${__V(data_${__intSum(${__jm__Loop Controller__idx},1,)},)}
More information: Here’s What to Do to Combine Multiple JMeter Variables

Variable value not updated in each iteration in Jmeter

I am using timestamp as a value in my script to keep it unique and distinguishable in each of the iterations. I am using ${__time(/1)} function and storing it in the user defined variable inside a transaction controller and later using same variable in other transactions and json payload as well. This works well for single iteration.
I expected that in each iteration timestamp will be updated. but I can see that in each iteration same timestamp is being used. How could I make it updating timestamp in every iteration as I need to use only one timestamp value in an iteration and not repeating in successive iterations.
The User-Defined Variable config element is processed only once at the start. It is processed after the test plan. It is not suitable for your use case.
UDVs should not be used with functions that generate different results each time they are called. Only the result of the first function call will be saved in the variable.
There could be a number of options.
Using User Parameters Pre-processor
You may set the Update Once Per Iteration to suit your requirement.
Using Set Variables Action plugin
Set the value in JSR223 Pre-processor
You may place the controller below the Test Plan.
As per User Defined Variables documentation:
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
So the variable is evaluated only once, when the test starts, if you want it to return you the current timestamp each time it's called - just use __time() function directly where required, there is no need to declare it as the variable.
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
I also don't think that /1 bit in your ${__time(/1)} function does anything meaningful, maybe it worth considering removing it completely
Adding value to a variable will save time only once on load, you can put the time function inside payload to get different times
Another option is to use pre processor as User Parameters over User Defined Variables
For defining variables during a test run, see User Parameters. UDVs are processed in the order they appear in the Plan, from top to bottom

Looping in Jmeter

Looping test occurrence based on the data count retrieved from the JDBC request and also as input data for the HTTP request
I have test scenario where i need to use the DB output as the input criteria for the HTTP request. Based on the DB output count( from the first request) i need to loop the HTTP request and it data accordingly
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
Debug Sampler Output
You can extract the counter using Post Processor [either Regular Expression Extractor or JSON Extractor etc.]
Once you have extracted that count, now place a Loop controller as a parent of HTTP request.
For example. I am using User Defined Variable for loop Count:
Any reason for using ${TEST_ID_#} variable? If your Debug Sampler screenshot is full and correct you should be using ${KEY_ID_#} instead.
Also it might be a better idea to use ForEach Controller instead of the Loop Controller, the relevant configuration would be something like:
References:
How to Use ForEach Controller in JMeter
Using Regular Expressions in JMeter

In JMeter how do I set multiple dynamic variables in User Parameters or CSV Data Set Config

Very new to JMeter (and a long time Stack Overflow listener but first time caller so take it easy on me) so any help would be appreciated.
I am trying to set up a JMeter test that uses multiple dynamically generated access tokens to run across scenarios. I currently have a set of data using the CSV Data Set Config containing login credentials of a user's email and password for example:
email1#email.com,password1
email2#email.com,password2
Next I send a HTTP POST request to the Login service which generates an accessToken. Then, I am using the JSON Extractor to grab the generated accessToken. After that I am using the BeanShell Assertion to store the accessToken property/variable.
My issue seems to lie here in this last step since it will only store the last generated variable instead of each of the generated accessTokens. I want to be able to store/overwrite the grabbed accessToken for each email password combination. I would like it to modify/populate the CSV file like this:
email1#email.com,password1,accessToken1
email2#email.com,password2,accessToken2
I have also tried using the Pre Processor > User Parameters
Screen shot of User Parameters
I would like to have the "userBearerToken" variable update/overwrite along with the tests, but I cannot find a way to do so or if this can even currently be done.
I'm finding it difficult to word what I am trying to ask, but basically I want to store multiple dynamically generated variables (accessTokens that change and time out) and use them in other tests. I don't care which component can handle this (either the CSV or User Parameter), but I need to be able to store these variables with their corresponding email password credentials.
If you want to store the accessToken value into a JMeter property for using in other Thread Group(s) be aware that properties are global for the whole JVM and remain until JMeter is restarted so if you define a single accessToken property - each JMeter Thread (or iteration) will overwrite the value.
The solution is to use current thread (or iteration) number as prefix or postfix, this can be done using either __${__threadNum} function or relevant JMeter Variable depending on how iteration is defined or both.
Example setup:
In first thread group: ${__setProperty(access_token_${__threadNum},bar,)}
In second thread group: ${__P(access_token_${__threadNum},)}
Demo:
If you want to save the values into a file writing into the current one is not the best idea as you can (and most probably will) get malformed file due to a form of a race condition. So I would recommend using Sample Variables property instead.
If you add the next line to user.properties file:
sample_variables=email,password,accessToken
JMeter will store the variables named ${email}, ${password}, and ${accessToken} along with their values in the .jtl results file which is basically CSV file which can be re-used anywhere else.
However if you have a requirement to store only the credentials and the token you can go for the Flexible File Writer plugin and configure it to save the aforementioned variables values into a separate file, the relevant configuration would be as simple as:
variable#0,variable#1,variable#2
You can install Flexible File Writer plugin using JMeter Plugins Manager

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