当前位置:网站首页>JMeter record the BeanShell written into excel instance caused by an automatic data generation
JMeter record the BeanShell written into excel instance caused by an automatic data generation
2022-07-23 10:41:00 【Ali test Jun】
【 The end of the article leaves a lot of benefits for everyone 】
One 、 Preface
Recently, work and life are busy , Neither busy nor busy , But I haven't felt it for a long time get New technical skills , There is no such thing as Yichui , Ha ha ha , Today, let's talk about the recent get To the small skills of it .
In the work , Because a certain requirement requires hundreds of data to verify an interface , You can't add it manually , Based on the tight test cycle , Directly consider using jmeter The loop controller of .
Two 、 Interface background
Business description : Click the Create button , Pop up the creation interface , After entering the required items , Click ok , Return to create data successfully .
1、 Create an interface
You need to use this interface to create data , The description of the interface is as follows :
1) Interface request method :PUT
2) The fields of variables required for the input parameters that must be passed are :userCode,userId,userName,remarkid
3) The remaining fields are based on the actual business , You can transfer a fixed value
Ideas tip: First, confirm the variable field , Secondly, confirm the source of variable fields , Finally, confirm how to get variables . In the current instance , except remarkid, rest 3 Variable fields are read from another query interface , See page for details 2 spot .
2、 Query user interface
Creating interface , When you enter a user name, you call 【 Query the user 】 Interface , The data returned by this interface is 【 establish 】 Input parameter fields required in the interface , Back to json The format is as follows :

3、 ... and 、Loop Controller Introduce
This component has been used in work for a long time , Today, I suddenly found that I didn't write a detailed blog about this component , It's embarrassing , Now let's briefly understand the usage of this component .

Loop Count: cycles , Divided into forever 、 Custom times —— Check Forever after , Custom times are grayed out and cannot be entered , Uncheck , Then you can
Literally , In fact, it is the request under the controller , Generally, it is used together with the counter , According to the actual scene .
If you are very familiar with Jmeter The tool , You must also know that thread groups can also set the number of cycles , In fact, these two can be understood as father son relationship , For example, the cycle controller sets the cycle 2 Time , Thread group set loop 4 Time , The result of execution is as follows : The threads run in cycles 4 round , Each round will request the interface 2 Time

Four 、 Method 1— Loop Controller Loop call interface
1、 The script structure is as follows

2、 Use powerful JSON Extractor , Extract the returned fields

3、 The user query interface returns the following results
Looking at the fruit tree Debug You can see the extracted userCode,userId,userName, among userCode_matchNr Indicates the total number of matches

4、 Create interface input parameters and use variable values
The query interface extracts all the values of the returned fields , The writing method of quoting variables is equivalent to ${userCode_1}, That can't be written dead in the entry _1 Well , Because you want to create a loop , We have to find a way to make userCode_x, hinder _x It can increase each time , So we should combine counter Counter functions .
and __V Is to nest them ,__V Belongs to nested function , If you are interested, you can visit Baidu online , If you don't understand, just copy the string and change the variables .
remarkid Duplication is not allowed in business , So here we use Random function , That's the random number function , Let it randomly generate a string of numbers .
"userCode":"${__V(userCode_${__counter(\,)},)}","userName":"${__V(username_${__counter(\,)},)}","userId":"${__V(userId_${__counter(\,)},)}","remarkid":"${__Random(1000,9000,)}"
5、 The number of cycles of the loop controller uses the variable value
The number of customizations in the loop controller allows for flexibility , Use the query interface to match the total number ( Variable _matchNr), A total of 3 A field , So use any of these fields , Citation format : ${username_matchNr}

6、 perform
Complete the above steps , You can create data , I won't post the execution results .
5、 ... and 、 Method 2—Loop Controller Cycle call JDBC
Follow the method 1 The steps are the same , Just the way 2 By calling jdbc request, namely sql Loop insert data , There is no mapping here , See the script structure for details , And then in jdbc Write a note inside insert sentence , Replace the corresponding fields with reference variables .
notes : perform insert,Query Type Need to choose 【Prepared Update Statement】
6、 ... and 、 Method 3—BeanShell write in excel, Generate... Manually sql sentence
1、BeanShell PostProcessor
This is also through sql Insert , It's just manual insertion , Add BeanShell PostProcessor, The script is as follows :

// Specify which file to write to
FileWriter fs=new FileWriter("C:/Users/13710/Desktop/9000.csv",true);
// Create a character cache output stream
BufferedWriter out = new BufferedWriter(fs);
// Another way to get the value of variables
//int b = Integer.valueOf("${username_matchNr}");
//System.out.println(" The total number returned by the interface is :"+b);
for(int i=1;i<= ${username_matchNr};i++){
username= vars.get("username_"+i);
userId = vars.get("userId_"+i);
userCode = vars.get("userCode_"+i);
out.write(username+","+userCode+","+userId);
out.write(System.getProperty("line.separator"));
System.out.println(" The output value is :"+username);
}
out.close();
fs.close();
2、 Automatically generated after execution .csv file
You can see excel Are there in 3 Column , because beanshell The script gets 3 Variable fields are written excel In the

3、 Use powerful excel Function generation sql sentence
D4 Column is to write one first sql Ha , After writing, drag down to automatically generate other , Table variables are referenced for all the variables that need to be changed , Format :"&A1&"
Cozy tip: If the string in the formula is too long , Use CONCATENATE function
Example 1( This is not super ): =CONCATENATE("INSERT INTO contact(name, post,id)VALUES ("&A1&", '"&B1&","&C1&"');")
Example 2( Divided into 4 A string , Because every string in the formula cannot exceed 255 character ):


4、 take D4 Columns can be directly copied to the database for execution

a key : Learning materials of course, learning is inseparable from materials , Of course, here is also prepared for you 600G Learning materials
Private keywords needed 【000】 Get it for free Note that the keywords are :000
Project practice :

Large e-commerce platform :

Full set of software test automation test teaching video

300G Download tutorial materials 【 Video tutorial +PPT+ Project source code 】

A complete set of software testing automation testing factory has been

python automated testing ++ A complete set of templates + Performance testing


It's said that the iron juice who has paid attention to me for three consecutive years has been promoted, raised and made a fortune !!!!
边栏推荐
- Network data leakage events occur frequently, how to protect personal privacy information?
- VirtualBox如何设置端口转发?
- Kingbasees SQL language reference manual of Jincang database (4. Pseudo column)
- 元宇宙浪潮震撼来袭,抓住时机,齐心协力
- 分期付款中的利率问题
- 比你老师详细系列————结构体
- 《Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks》论文阅读
- Data warehouse: workflow design and Optimization Practice
- TZC 1283: 简单排序 —— 堆排序
- 振奋人心 元宇宙!下一代互联网的财富风口
猜你喜欢

Redis replication cluster setup

8 < tag dynamic programming and LCS problems > lt.300. Longest increasing subsequence + lt.674. Longest continuous increasing sequence

CV (3)- CNNs

PyQt5_QListWidget分页多选控件

无套路、无陷阱、无广告 | 这个免费的即时通讯软件确定不用吗?

网络数据泄露事件频发,个人隐私信息如何保护?

PowerBI入门指南

Chapter 4: runtime data area - shared space

Idea integrated sonar complete process

Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
随机推荐
Unity Image中Sprite和overrideSprite区别(转载)
Ue5 official case Lyra full feature explanation 6. generate defense tower
振奋人心 元宇宙!下一代互联网的财富风口
redis 复制集群搭建
数据湖:Apache Iceberg介绍
Cache penetration, cache breakdown, cache avalanche
C# IValueConverter接口用法举例
信息安全危机四伏,企业数据资产泄露管控刻不容缓
Sonar中如何删除一个项目
什么是即时通讯?即时通讯的发展
Introduction to partition operators, broadcast variables and accumulators of 32 spark
2022/7/22
数据湖:从数据仓库看数据湖
常见神经网络参数量与计算量
分期付款中的利率问题
Idea integrated sonar complete process
MySQL query optimization - detailed explanation
Interest rate in installment payment
Operator usage and scheduling process of 31 spark
Flask学习笔记