Jmeter interface actual combat (2) get request result written to file

Jmeter Interface Practical Combat (2) The get request result is written to the file

Second formula: get request result written to file


Article directory

  • Jmeter interface actual combat (2) get request result written to file
  • Preface
  • 1. Effect drawing
  • 2. Analysis of knowledge points
    • 1. How to find the interface for obtaining page data
    • 2.json extractor
      • Usage scenario ①: Extract a value, such as token
      • Usage scenario ②: The same field appears multiple times and all are extracted. For example, the ID appears 10 times in the response.
    • 3.Beanshell writes data to file
  • Summarize

Foreword

This series of articles will teach you how to get started with Jmeter quickly from the basics to the more advanced ones.
Scenario: I want to write a stress test for the audit interface and find that the incoming parameters change, such as ID, number, etc. Then I need to obtain these parameter information in real time before auditing the interface. Two methods:
①Simply connect to the database directly via JDBC and write SQL to get the value
②What should I do if it is inconvenient to connect to the database sometimes? Then get it through the interface.

1. Renderings

Let’s start with the renderings. Idea:Generally, when you open a certain menu, there will be an interface for obtaining data. After we get the response, we use the json extractor to extract the desired field information from the numerous response data, and then Write it into a file to facilitate the next interface to retrieve data.


import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;

File file = new File("${filepath}" + "test.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String data="";

for(int i=1;i<=Integer.parseInt("${page}");i + + ){<!-- -->
data + = vars.get("taskid_" + i) + ","
+ vars.get("CCID_" + i);

if(i != Integer.parseInt("${page}")){<!-- -->
data = data + "\
";
}
}
bw.write(data);
bw.close();

2. Knowledge point analysis

1. How to find the interface for obtaining page data

Open your little bell-like eyes and take a look. If your current page has 10 pieces of data, open F12, look at the preview, and find an interface with a column of numbers. The value corresponds to the number of data pieces on the current page. Fill in this interface. In jmeter, let’s take a look at two examples.

This is the interface data obtained by searching in csdn

2.json extractor

Usage scenario ①: Extract a value, such as token

Right-click->Add post-processor->json extractor

Usage scenario ②: The same field appears multiple times, and all are extracted. For example, the ID appears 10 times in the response

If you want to get the taskid of 10 pieces of data, how to write it?
analyze:
1. First is a root node, below find detaDetail–>$.detaDetail
2. Below is an array, and there are strings under each array, similar to this [{…},{…},{…}…]
What I want to get is the taskid in each string, which means []
Take out each taskid in

$.detaDetail[*].taskid

3. How to write if you only want to retrieve the taskid under the specified curly braces? If you want to retrieve the number, write the number of n.

$.detaDetail[n].taskid


4. If you want to get the values of multiple fields, separate them with semicolons.

3.Beanshell writes data to file

Right-click->Add post-processor->Add beanshell postprocessor

There are two parameters ${filepath}-》It is the path of the file to be written. You can add a configuration original [user-defined variable], or add variables under the test plan. It is very flexible. You can do whatever you like.
User-defined variables

Test plan variables


${page} is the number of items in my data acquisition interface. I want to customize this number according to the stress test situation.

Let’s take a look at the specific writing method
First create the file, then concatenate the parameters separated by commas, and then write them into the file

import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
// Create a file
File file = new File("${filepath}" + "test.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String data="";
// splicing parameters
for(int i=1;i<=Integer.parseInt("${page}");i + + ){<!-- -->
data + = vars.get("dw_taskid_" + i) + ","
+ vars.get("dw_CCID_" + i);

if(i != Integer.parseInt("${page}")){<!-- -->
data = data + "\
";
}
}
// write to file
bw.write(data);
bw.close();


Summary

tips: Under the options, there is [Log View]. When executing the script, you can open the log. Note that when there are Chinese symbols in the path, the log will report an error. Just change it to English format

I think two points are more important.
①The json extractor extracts multiple values and uses asterisks to match them.
②Beanshell writes the results to a file. You need to have some code foundation, or you can understand it and copy and paste it. The most important thing is to think according to your own business interface.

The next preview will talk about how to read the data in the file. Friends, please be patient and practice a lot~