Jmeter:上传excel,硬编码值 [英] Jmeter : upload excel, hard coded values
问题描述
我已经记录了一个上传excel的场景,在接下来的后续请求中,excel中的这些记录作为参数传递。
但是,假设我需要更改excel,该请求将如何获取新值?
由于大量值,参数化似乎不是答案。
$ b $
如果您需要从Excel文件中提取一些值并将其添加为HTTP请求参数可以采用以下方法。
- 下载 Apache Tika 二进制(tika-app - *。jar)并将其放到JMeter / lib文件夹中。重新启动JMeter,如果它正在运行
- 添加 Beanshell PreProcessor 作为请求的小孩,您要根据Excel文件值修改哪些参数
-
在脚本中,开发的代码是读取Excel文件,并从它作为HTTP请求参数。示例下面的代码从testfile.xlsx文件的A1和B1单元格中提取值,并将它们发送为foo和barHTTP请求参数。
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
FileInputStream excelFile = new FileInputStream(new File(/ path / to / excel / testfile.xlsx));
XSSFWorkbook工作簿=新的XSSFWorkbook(excelFile);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
单元格a1 = row.getCell(0);
String a1Value = a1.getStringCellValue();
单元格a2 = row.getCell(1);
String a2Value = a2.getStringCellValue();
excelFile.close();
sampler.addArgument(foo,a1Value);
sampler.addArgument(bar,a2Value);
参考文献:
I have recorded a scenario in which an excel is uploaded, in the next subsequent request those records in excel are passed as a parameters.
But suppose i need to change the excel , how that request will take new values?
Parameterization seems not the answer because of large number of values.
Pls help.
If you need to extract some values from Excel file and add them as HTTP Request parameters you can use the following approach.
- Download Apache Tika binary (tika-app-*.jar) and drop it to JMeter's /lib folder. Restart JMeter if it's running
- Add a Beanshell PreProcessor as a child of the request which parameters you want to modify basing on Excel file values
In the "Script" are develop code which reads Excel file and adds values from it as HTTP Request parameters. Example below code extracts values from A1 and B1 cells of testfile.xlsx file and sends them as "foo" and "bar" HTTP Request parameters.
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; FileInputStream excelFile = new FileInputStream(new File("/path/to/excel/testfile.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(excelFile); XSSFSheet sheet = workbook.getSheetAt(0); XSSFRow row = sheet.getRow(0); Cell a1 = row.getCell(0); String a1Value = a1.getStringCellValue(); Cell a2 = row.getCell(1); String a2Value = a2.getStringCellValue(); excelFile.close(); sampler.addArgument("foo",a1Value); sampler.addArgument("bar",a2Value);
References:
- Apache POI User API (HSSF and XSSF)
- How do I read from an Excel file using POI?
- How to use BeanShell: JMeter's favorite built-in component
这篇关于Jmeter:上传excel,硬编码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!