[JMeter] Use BeanShell to write content to a file

insert image description here

I. Introduction

In our daily work, we may encounter the need to write the data returned by the request to a file. This is often the case when we use JMeter for performance testing. To achieve this purpose, we generally use the BeanShell post-processor to write the content to the file.

2. Extraction

In most current performance tests, the results are returned in the form of JSON. So here we take the JSON extractor as an example to extract.

insert image description here

Let's introduce the JSON extractor first.

  • Main sample and sub-samples: Matches include the current parent sampler and overrides the sub-samplers

  • Main sample only: The matching range is the current parent sampler (the most commonly used case)

  • Sub-samples only: match only sub-samplers

  • JMeter Variable Name to use: Supports matching of Jemter variable values

  • Names of created variables: variable name

  • JSON Path expressions: the corresponding location of the JSON value we need to extract

  • Match No.: 0 represents a random value, n selects the matching value, -1 matches all

  • Compute concatenation var: If many results are found, the plugin will concatenate them using ',' delimiter and store it in a var called _ALL

  • Default Value: If the parameter does not get a value, then give a default value to let it take

3. Write

​ When we get here, we will start writing scripts. Here we directly upload the code. According to the extracted data, our scripts are also different (this is a test of everyone’s thinking). Here we need to pay attention to one thing, JMeter scripts are in When executing, it will first check whether the csv file exists, and if it does not exist, an error will be reported, so you need to manually create a file in the directory first, and the content can be empty

java
import java.io.*;
FileWriter fstream = new FileWriter("E:\\stuId2.csv",true);    // true表示追加模式
BufferedWriter out=new BufferedWriter(fstream);
var num=vars.get("stuId_matchNr");  // 获取JSON提取器中提取的数据
for(int i=1;i<=(Integer.parseInt(num));i++){
    
    
	log.info(vars.get("stuId_"+i));
	out.write(vars.get("stuId_"+i)+"\n");  // 写入数据到csv中并换行
}
out.close();
fstream.close();

In this way, the data we need is extracted, and then the data can be applied to our script to be tested for parameters.

insert image description here

The above is the whole content of this section, if there is any mistake, please correct me!

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves【保100%免费】
insert image description here

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_67695717/article/details/132414956