Read a string from a file with a dynamic name in JMeter - jmeter

My test plan contains a thread group that looks like that :
Only once controller : Login to application
CSV Data Set Config : Info used to login to application
Only once controller
HTTP Request : POST login information
Loop controller (10 times)
HTTP Request : POST add a "comment" to a "question"
My CSV dataset contains these fields :
username
password
group_id
A user have access to a question depending on it's group_id. I want to add 10 comments per thread to different questions.
My idea was to build a file like questions_by_group_${group_id}.txt containing some ids of questions that the ${group_id} would have access. Then I could do ${__StringToFile(questions_by_group_${group_id}.txt)} in the POST parameter value to add a comment. Using __StringToFile function should loop through lines of my file at every call. That way I can add many comments to many different questions.
The problem is that JMeter seems to open the file one time and share it for every thread (even thought I know that the ${group_id} changes for every thread.
I tried many things like
${__StringFromFile(questions_by_group_${group_id}.txt)}
${__StringFromFile(${__eval(questions_by_group_${group_id}.txt)})}
${__eval(${__StringFromFile(${__eval(questions_by_group_${group_id}.txt)})})}
but nothing works.

StringFromFile works this way by default, read:
__StringFromFile
You could organize your csv like this:
username, password, group_id , commentID1, commentID2....
Then put CSV Data Set Config before Once Only Controller.
Set Sharing Mode to All threads for example.
Finally just use variables instead of using __StringFromFile.
It's not the best solution as you have to build this file and repeat group_id ids.
Another option, if your number of groups is not very big, would be to do this in a setUp Thread Group:
read a CSV file containing:
groupID1, IDA1, IDA2,....
groupID2, IDB1, IDB2,....
...
set one property per group __setProperty or calling this inside a Beanshell Sampler:
JMeterUtils.setProperty(propertyName, propertyValue);
Then in your controller, use a Beanshell Sampler to get ids using:
JMeterUtils.getProperty(propertyName);
I must say I hate all these solutions :-)

Related

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

How to hit parameterized url in Apache Jmeter?

This is my test URL " http://appr.seconddemo.org/hitssurveys/survey?uid=113&offerid=311&subid=subvalues&offr_id={Email} "
I want to hit the url 1500 times per second , and want to change the "{Email}" with real value in each iteration.
How it's possible please give me a step by step guide.
Considering that you need to pass the emails from the external file, the most efficient way that we are using in software testing companies is to pass the variable from .csv or .txt files using 'CSV Data Set Config' element of the JMeter.
Please find the steps that you need to follow:
Add a 'CSV Data Set Config' element from 'Config Element' by right-clicking the thread group
Set Filename field with complete path to your .csv or .txt file that contains your emails
Set Variable field as 'Email' [This variable name should be same as you set in your url request]
Ignore first line to False
Set other fields as per your requirement
Now add HTTP Sampler in your Thread Group and set the Protocol, Server Name, Method & Path as instructed in the screenshot:
Create .csv or .txt file and add all emails separated by new line:
Hope this answer is useful.
You can use the CSV Data Set Config. Put all your email credentials in the CSV file and make sure you have put this CSV file in your JMeter /bin directory.
Add a CSV dataset config in your test plan. Your CSV dataset config should be like:
Now in your thread group, define the number of threads you want to execute and then in your sampler put the path as follows:
http://appr.seconddemo.org/hitssurveys/survey?uid=113&offerid=311&subid=subvalues&offr_id=${Email}
Depending on where do your emails live there are following options:
If they are in a text file, each email on a new line you can use __StringFromFile() function like:
If they are in a database you can use JDBC PreProcessor to fetch the data from the database table column and put your request under ForEach Controller
If you need to provide just some random characters you can use __RandomString() function.
More information: JMeter Parameterization - The Complete Guide

How can I log the value of a variable sent in an HTTP Request sent from JMeter, if the value was first read in from a csv file

I would like to read the exact value of a variable I use to pass through an HTTP Request. I first read in many values of variables using the CSV Data Set Config. For the username, it is in the form of an email address. So, I have a variable called "email" in the Data Set Config. In the actual HTTP Request, for "name", I call it "username". For the "Value" field for this same "username", I added a time() function to it like this so I would end up creating unique users in my tests:
${email}${__time()
When I view the "Request" in a View Results Tree, I can see my parameter is listed correctly:
username=email1%40email.com1390854377360
I do not care if this is correct in real world terms. I already know that is not a valid email. That is ok for now.
What I want to know is how can I log that email that I just created on the fly? I would like to not have to pull in the whole request every time also and then use some type of Regular Expression extractor. It just seems like there should be an easy way to do this.
I think there are 2 ways,
Beanshell Pre/Post processors : you can write custom code in which you can log all your variables in some custom log file
Simple data writer : you can configure it and check save url,save field names,save response data field checkboxes that will give you complete data but in that later postprocessing on result file is required to get all usernames (email in your case).
first approach is easier and allows you create your own logging format(easy to retrieve and use somewhere else).
second approach is somewhat tedious and requires post processing.

Automatically generate HTTP request URLs in JMeter

I have an API call URL similar to this:
http://domain.com/rest/getValues?apiKey=sdfsdf&customerId=2200&timestampBegin=2013-08-15%2018:00:00%20CEST&timestampEnd=2013-08-19%2018:00:00%20CEST
I would like to dynamically change the time stamps, as well as the customerId, picked from a given set of timestamps and customerIds. Is this possible to do in Jmeter (preferably through GUI)?
Assuming that you are have inputs coming from external CSV file, you can use CSV data set config as explained here.
Your test plan will look something like
Thread groups
CSV Data Set config (define the file path and the variable names. You will be able to read these variables in request.
HTTP Sampler
http://domain.com/rest/getValues?apiKey=sdfsdf&customerId=${custid}&timestampBegin=${begin}&timestampEnd=${end}

JMeter : how to use the timestamp of the parent

I'm testing a group of urls for performance tests. We have an SLA that states that a certain group of URLS must have an average of 80% success within a certain timerange.
The logic of the sla is done in a separate application. The data is fed from JMeter output into a database.
I need a way to identify the 5 tests of these urls, so that the application knows they belong to the same test. I use a Transaction Controller to group all the URL tests, but I still don't see how I can put an identifier in the generated output file (done by View Results Tree Listener). If I could reuse the timestamp of the parent, i.e. the Transaction Controller for the individual HTTP Requests, that would make my day. I tried adding User Defined Variables under the Transaction Controller, but I don't see how I can output the value of my variable into the output file.
Is anything similar possible?
Best regards,
Wim
Store your value in jmeter context using a Beanshell sampler then in jeter.properties uncomment sample_variables ans add the name under which you stored your value:
Optional list of JMeter variable names whose values are to be saved in the result data files.
Use commas to separate the names. For example:
sample_variables=SESSION_ID,REFERENCE
Regards
Philippe M.

Resources