Back JMeter Beginner Tutorial 15 - How to read data from CSV file (Parameterisation)

10-20
Published At: 2016-08-11
Search Query: Java Method Parameters
Tags: jmeter parametrization jmeter parametrisation jmeter csv csv jmeter jmeter data driven jmeter data-driven jmeter how jmeter how to read data from csv file jmeter csv file jmeter data set How to read data from csv file jmeter beginner tutorials CSV Data Set Config jmeter get data from external file jmeter how to get data jmeter how to jmeter how to create data driven tests
more from Raghav - https://automationstepbystep.com/
Step 1 : Add config element - CSV Data Set Config
Step 2 : Add details in CSV Data Set Config
Step 3 : Update value fields: ${variable_name}
Step 4 : Run and validate

------------ PERFORMANCE TESTING ------------
JMeter Beginner - https://bit.ly/2oBbtIU
JMeter Intermediate - https://bit.ly/2oziNVB
JMeter Advanced - https://bit.ly/2Q22Y6a
JMeter Tips & Tricks - https://bit.ly/2NOfWD2
Performance Testing - https://bit.ly/2wEXbLS


________ ONLINE COURSES TO LEARN ________

Visit - http://automationstepbystep.com/

------------ UI TESTING ------------
Selenium Beginners - https://bit.ly/2MGRS8K
Selenium Java Framework from Scratch - https://bit.ly/2N9xvR6
Selenium Python - https://bit.ly/2oyMp5x
Selenium Tips - https://bit.ly/2owxc50
Selenium Builder - https://bit.ly/2MKNtlq
Katalon Studio - https://bit.ly/2wARFdi
Robot Framework - https://bit.ly/2Px6Ue9

------------ API TESTING ------------
Web Services (API) - https://bit.ly/2MGafL7
SoapUI - https://bit.ly/2MGahmd
Postman - https://bit.ly/2wz8LrW
General - https://bit.ly/2PYdwmV

------------ MOBILE TESTING ------------
Mobile Playlist - https://bit.ly/2PxpeUv

------------ CI | CD | DEVOPS ------------
Jenkins Beginner - https://bit.ly/2MIn8EC
Jenkins Tips & Trick - https://bit.ly/2LRt6xC
Docker - https://bit.ly/2MInnzx

------------ VERSION CONTROL SYSTEM ------------
Git & GitHub - https://bit.ly/2Q1pagY

------------ JAVA ------------
Java Beginners - https://bit.ly/2PVUcXs
Java Tips & Tricks - https://bit.ly/2CdcDnJ

------------ MAVEN ------------
Maven - https://bit.ly/2NJdDRS

------------ OTHERS ------------
Redis- https://bit.ly/2N9jyCG
Misc - https://bit.ly/2Q2q5xQ
Tools & Tips - https://bit.ly/2oBfwoR
QnA Friday- https://bit.ly/2NgwGpw
Sunday Special - https://bit.ly/2wB23BO

Ask Raghav- https://bit.ly/2CoJGWf
Interviews - https://bit.ly/2NIPPxk
All Playlists - https://bit.ly/2LSiezA

------------ Follow ------------
Facebook - https://www.facebook.com/automationstepbystep
Twitter - https://twitter.com/automationsbs
Youtube - http://youtube.com/automationstepbystep

Never Stop Learning
Raghav
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@rajukp1
2017-03-05

Hi Raghav, You are a great tutor. Thanks for your help. I request you to please add the notes that you prepared while teaching us if you have it. Sorry if I am bothering you

@RaghavPal

Hi Kalyan, I am glad it helped. Untill now i have been writing the steps down on a notepad as shown in the videos and i also copy the same in the description of the video. Will try to add more description for everyone's help. Keep Learning.

VidTutorAssistant (GPT-4)

Hi @rajukp1, thank you for your kind words and I'm glad to know you find these tutorials helpful. I'm always open to providing any resources that can further assist you. I don't typically prepare detailed notes, but the video is actually structured to be as informative as possible. However, you've given me a good suggestion. I'll look into possibly creating notes for future videos. If you have any particular points that you found confusing or you want elaborated on, feel free to mention them in the comments. Remember, no question is bothering me - the goal is to learn and grow together. Keep learning. :)


@amoujahid
2019-10-28

Great tutorial. Thx a lot

@RaghavPal

You're welcome Abdallah

VidTutorAssistant (GPT-4)

Thanks for watching, @amoujahid! I'm glad you found the tutorial helpful. Keep following the channel for more insightful and beginner-friendly programming content. Happy learning!


@gayatritankar7856
2023-10-30

Hello Sir can we Pass Data Input to Different Users in a Thread group of JMeter by using dummy data? #Raghavpal #Automationstepbystep

@RaghavPal

Gayatri
Yes, you can pass data input to different users in a thread group of JMeter by using dummy data. There are a few ways to do this.

One way is to use a CSV Data Set Config. A CSV Data Set Config allows you to read data from a CSV file and pass it to your test cases. To use a CSV Data Set Config, follow these steps:

1. Create a CSV file with the dummy data that you want to pass to your test cases.
2. In JMeter, add a CSV Data Set Config to your thread group.
3. In the CSV Data Set Config, specify the path to the CSV file and the names of the variables that you want to extract from the CSV file.
4. In your test cases, use the `__eval()` function to access the variables that you extracted from the CSV file.

Here is an example of a CSV Data Set Config:

```
CSV Data Set Config
File name: users.csv
Variable names: username, password
```

Here is an example of a test case that uses the CSV Data Set Config to pass dummy data to a login form:

```
HTTP Request
URL: http://localhost:8080/login
Method: POST
Parameters:
username: ${__eval(username)}
password: ${__eval(password)}
```

Another way to pass dummy data to different users in a thread group of JMeter is to use a BeanShell Sampler. A BeanShell Sampler allows you to execute scripts in JMeter. To use a BeanShell Sampler to pass dummy data to different users, follow these steps:

1. Create a BeanShell Sampler and add the following code to it:

```
import java.util.Random;

public class RandomUserData {

public static String getRandomUsername() {
Random random = new Random();
return "user_" + random.nextInt(1000);
}

public static String getRandomPassword() {
Random random = new Random();
return "password_" + random.nextInt(1000);
}
}
```

2. In your test cases, use the `__groovy()` function to call the `RandomUserData` class and generate dummy data for the username and password fields.

Here is an example of a test case that uses a BeanShell Sampler to generate dummy data for the username and password fields:

```
HTTP Request
URL: http://localhost:8080/login
Method: POST
Parameters:
username: ${__groovy(RandomUserData.getRandomUsername())}
password: ${__groovy(RandomUserData.getRandomPassword())}
```

You can also use a combination of CSV Data Set Configs and BeanShell Samplers to pass dummy data to different users in a thread group of JMeter. For example, you could use a CSV Data Set Config to read a list of user IDs from a CSV file and then use a BeanShell Sampler to generate dummy passwords for each user ID.

Which method you choose to pass dummy data to different users in a thread group of JMeter will depend on your specific needs. If you have a simple need, such as generating random usernames and passwords, you can use a BeanShell Sampler. If you have a more complex need, such as passing different data to different users based on their user ID, you can use a combination of CSV Data Set Configs and BeanShell Samplers.

VidTutorAssistant (GPT-4)

Hello @gayatritankar7856, thank you for your question! Yes, you can certainly pass data input to different users in a Thread Group of JMeter by using dummy data. This approach is called parameterization, which avoids hard coding and enables the test plan to use different data sets. You can utilize something known as a CSV Data Set Config element to read data from a CSV file.

So, in your CSV file, you can define all the dummy data for multiple users. Each row in your CSV file will represent a unique user with a unique set of data. As explained in the tutorial video, you can replace hard coded values in your HTTP requests with the variable names from your CSV file using the ${variable_name} syntax.

Remember, you have to specify the number of Thread count (users) and loop count equal to data set count. This can be adjusted according to the scenario you are testing.

Once you correctly set up your CSV Data Set Config, each thread (user) in your Thread Group will take a unique line of data from the CSV file and use it for the respective HTTP requests. Make sure to validate your setup by running your JMeter test and checking if all users are sending the requests with the correct data.

If there are more number of rows (data sets) in the CSV than the Thread count, JMeter will use a new line of data for each iteration across the Threads. If there are fewer rows than the Thread count, data entries will start repeating.

I hope this answers your question! Keep learning and practicing, and feel free to ask if you have more doubts. Thanks for watching! #HappyLearning


@vinay24c
2023-08-14

Hi Sir, Thanks for your videos. I want to substitute (example 10 unique data) in my single request like each occurrence in LoadRunner, is there any way to achieve the same in JMeter, please assist it.

@RaghavPal

Vinay
Yes, there are a few ways to substitute different data in a single request in JMeter.

One way is to use the CSV Data Set Config element. This element allows you to specify a CSV file that contains the data that you want to substitute. The CSV file should have one row for each data point, and the columns in the file should match the names of the variables in your request.

To use the CSV Data Set Config element, you need to add it to your test plan and then configure it with the path to the CSV file. You can also specify the delimiter that is used in the CSV file, and the number of rows that you want to read.

Here is an example of how to use the CSV Data Set Config element:

```
<CSVDataSetConfig fileName="data.csv" delimiter="," numberOfLines="10"/>
```

In this example, the CSV file is called `data.csv` and the delimiter is a comma. The `numberOfLines` attribute is set to 10, which means that the element will read the first 10 rows of the file.

The data in the CSV file will be substituted into the variables in your request. For example, if your request has a variable called `username`, the value of the `username` variable will be replaced with the first value in the first row of the CSV file.

Another way to substitute different data in a single request is to use the *BeanShell PostProcessor* element. This element allows you to execute a BeanShell script before or after a request is executed.

The BeanShell script can be used to read data from a file, generate random data, or perform other operations. The data that is generated by the BeanShell script can then be substituted into the variables in your request.

Here is an example of how to use the BeanShell PostProcessor element:

<BeanShellPostProcessor guiclass="TestBeanGUI" testclass="BeanShellPostProcessor" testname="BeanShell PostProcessor" enabled="true">
<stringProp name="filename"></stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="script"></stringProp>
</BeanShellPostProcessor>


In this example, the BeanShell script is stored in the `script` attribute. The `script` attribute can contain any valid BeanShell code.

The BeanShell script can be used to read data from a file, generate random data, or perform other operations. The data that is generated by the BeanShell script can then be substituted into the variables in your request.

I hope this helps

VidTutorAssistant (GPT-4)

Hello @vinay24c, I'm glad to see your interest in JMeter and my videos. To use multiple unique values in your request just like in LoadRunner, JMeter's CSV Data Set Config is a good way to go about this.

You can add the CSV Data Set Config to your Thread Group and input your file name containing the unique data sets. Every new virtual user (iteration) will pick a new line from the CSV file, so assuring that each of your 10 unique data items are on separate lines of your CSV file will help achieve your goal.

Remember to structure your HTTP request to substitute the parts where you need the unique values as '${variable_name}'. As explained in the video, the variable name should match the column header in your CSV file.

Please just ensure your 'Recycle on EOF?' setting is false (to avoid repetition of values) and that your CSV file has enough unique values for all iterations. I'd suggest watching the tutorial again closely to better understand how data from CSV files are parsed in JMeter and how you can fine-tune the process according to your needs.

Thanks for your question! Let me know if you have other queries. Happy coding!


@bilalrahaoui191
2019-02-03

for who does not work with him , please change in "CSV Data Set Config" the 4th option "Ignore First Line" to true (for new version 5.0 of jmeter)

@RaghavPal

Thanks for adding this info Bilal

VidTutorAssistant (GPT-4)

Thanks for your suggestion, @bilalrahaoui191! You're absolutely right. If anyone is using JMeter version 5.0 or newer, you might need to change the "Ignore First Line" option to true in the "CSV Data Set Config." This option enables JMeter to skip the first line of the CSV file, which is typically the header information. Always be sure to check the version-specific documentations for the tools you're using. Happy learning!


@PrincetonKurtz-zn9qv
2023-05-21

Hi Raghav ,Suppose for multi user load test I want to login with different usernames & passwords from CSV file then what values should be under "Variable names " box ? I should left it blank ?

@RaghavPal

Hi Princeton
In JMeter, when you want to use different usernames and passwords from a CSV file for a multi-user load test, you need to specify the variable names that correspond to the columns in the CSV file.

Here's what you should do:

1. Create a CSV file with columns for usernames and passwords, for example:
```
username,password
user1,password1
user2,password2
user3,password3
```

2. Add a CSV Data Set Config element to your Test Plan in JMeter.

3. In the CSV Data Set Config element, set the "Filename" field to the path of your CSV file.

4. In the "Variable Names" field, you should enter the names of the variables that correspond to the columns in your CSV file. For example, if your CSV file has columns named "username" and "password", you should enter `username,password` in the "Variable Names" field. Make sure the names match exactly (case-sensitive).

5. In your HTTP Request, use the variable names in the format `${variable_name}` to refer to the values from the CSV file. For example, if you want to use the username and password values from the CSV file, you can use `${username}` and `${password}` in the appropriate fields of the HTTP Request.

By setting the correct variable names in the "Variable Names" field, JMeter will automatically read the values from the CSV file and assign them to the corresponding variables during the load test. Each thread/user will then use a different set of credentials from the CSV file.

VidTutorAssistant (GPT-4)

Hi @PrincetonKurtz-zn9qv, good question! If you want to perform a multi-user load test by logging in with different usernames and passwords from a CSV file, you would need to put the relevant variable names in the "Variable Names" box, separated by a comma.

Let's say your CSV file has two columns, the first one for usernames and the second one for passwords. You could then use "username,password" in the "Variable Names" box. You would then replace your hard-coded login callbacks with these variable names by using ${username} and ${password} in your login scripts.

Leaving the "Variable Names" blank is not advisable as it will be harder for you, or anyone else reading your scripts, to understand which values from your CSV file correspond to which parameters in your scripts. Naming your variables makes your scripts more readable and maintainable. Hope this helps, happy learning!