简体   繁体   中英

How to use cell values of .csv file in jmeter?

I have a .csv file which contains the multiple entries of employee names.Now i want to read that .csv file in my jmx script in jmeter. It should be in a way that each thread of my jmx script reads the different value from the .csv file. And then i want to use those cell values in the HTTP Request.

I am using Bean Shell pre processor for it. But it is of no help for me.The below is code which i have written for it

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

CSVReader reader = new CSVReader(new FileReader(C:/Users/manoj/Downloads/View_All_names.csv));
String[] row;
String name;
while ((row = reader.readNext()) != null) 
            {
                            name = row[1];

            }

The value "name" i want to use it in my HTTP Request then.DO i have to mention this attribute "name" in Test Plan??

Any help is appreciated.

First of all, are you aware of __CSVRead() function? You should be able to use it directly in the place where you need the value from CSV file without having to write a single line of code.


Just in case you still want Beanshell for any reason.

  1. You need to surround the path to CSV file with quotation marks
  2. You need to write the resulting value into JMeter Variables for later reuse.
  3. Where did you get the above code? If CSVReader stands for com.helger.commons.csv.CSVReader from ph-commons-6.2.4.jar - you need to use it a little bit differently, like use List instead of array of strings to wit:

     import com.helger.commons.csv.CSVReader; CSVReader reader = new CSVReader(new FileReader("C:/Users/manoj/Downloads/View_All_names.csv")); List row; String name; int counter = 1; while ((row = reader.readNext()) != null) { name = row.get(1).toString(); vars.put("name_" + counter, name); counter++; }

    After Beanshell PreProcessor finishes its work you will be able to access variables values like:

    • ${name_1}
    • ${name_2}
    • ${name_3}
    • etc.

vars stands for an instance of JMeterVariables class which provides read/write access to the JMeter Variables in scope. See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter tests.

All you need is to put one CSV Data Set Config component, and it will read entries for you. Each line for each thread (user). In the component you need to specify variable name which you will later use in your test plan, as well as the path to your .csv file.

CSV 数据集配置

If you put variable name like "name" (w/out quotes) you will use ${name} in requests/samplers where you want to use it.

在此处输入图片说明

A simple way to capture the column values from excel in jMeter

Just insert data in excel column wise ie horizontally insert all the data and use

${__CSVRead(filePath,ColNum)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM