I am new to jmeter. so I need to know what is the alternative for csv data set config because if I use csv data it okay for couple of user but every time i need to change or add the data in file. it time consuming i need efficient way which i can extract the user name and password from code instead of using hot code value.
2 Answers
The alternative options would be:
- Getting credentials from the database using JMeter's JDBC Test Elements , see How to Retrieve Database Data for API Testing with JMeter
- If your users are synthetic, i.e.
user1
,user2
, etc. and have the same password or password of the similar pattern you can use Counter configuration element or __counter() function or __threadNum() function to generate incremented number
In JMeter, you can extract user id and password without using a CSV file by using the User Parameters pre-processor element. This element allows you to define variables that can be used in your test plan.
Here are the steps to extract user id and password using the User Parameters pre-processor element:
Add a User Parameters pre-processor element under the Thread Group in your test plan.
Define the variables for user id and password by clicking on the "Add" button in the User Parameters pre-processor element. For example, you can define the variable names as "username" and "password" and assign them with default values.
Use the defined variables in the HTTP request for login. For example, in the "Username" and "Password" fields of the HTTP request, use ${username} and ${password} respectively.
To update the user id and password for each thread, you can use the __threadNum function in the User Parameters pre-processor element. For example, you can define the default values of the "username" and "password" variables as "user${__threadNum}" and "password${__threadNum}" respectively. This will generate unique usernames and passwords for each thread, based on the thread number.
Here's an example of how to extract user id and password without using a CSV file:
First, add a HTTP Request sampler to the Thread Group in JMeter.
In the HTTP Request sampler, enter the URL of the login page and select the method (e.g. POST).
Add a HTTP Header Manager to the HTTP Request sampler and set the content type to "application/x-www-form-urlencoded".
In the Body Data section of the HTTP Request sampler, enter the following parameters (replace "username" and "password" with your actual parameters):
username: ${__P(username)}
password: ${__P(password)}
Add a Regular Expression Extractor to the HTTP Request sampler.
In the Regular Expression Extractor, set the following values:
Reference Name: userID
Regular Expression:
Template: $1$
Match No.: 1
Add another Regular Expression Extractor to the HTTP Request sampler.
In the Regular Expression Extractor, set the following values:
Reference Name: password
Regular Expression:
Template: $1$
Match No.: 1
Now, you can add a Loop Controller to the Thread Group and set the number of iterations to the desired number of users.
Inside the Loop Controller, add a User Defined Variables configuration element.
In the User Defined Variables, add the following variables (replace "user1" and "pass1" with your actual values):
username_1: user1
password_1: pass1
username_2: user2
password_2: pass2
...and so on for each user.
Finally, go to the HTTP Request sampler and replace the hard-coded values for username and password with the variables you just created, like this:
username: ${_P(username${__jm__Loop Controller__idx})}
password: ${_P(password${__jm__Loop Controller__idx})}
This way, each iteration of the Loop Controller will use a different set of user credentials, without the need for a CSV file.