Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

[new Dream sharing] Jmeter skills: how to set variables in Json data

Shulou Source: shulou.com Published: 2022-06-02 19:12:10 10月02日 Update

I have two interfaces, interface An and interface B, and interface B is as follows:

The question is how to set the value after name in {"tag": {"name": "123456"}} as a variable, which is extracted from interface A.

If you use regular expressions or json extractors, such as:

The extracted value is directly used in API B, for example: {"tag": {"name": "${tokens}"}, and the final return value will be {"tag": {"id": 149, "name": "${tokens}"}}.

Solution: data in json format can store two forms of variables (as far as I know). One is a function in the function helper, such as:

The other is to read external files from the outside world and put them into data in json format, such as:

The rest is very simple. Now that you know how to put the data in json format, all that is left is how to extract the data from the first interface and put it in a file. Here is an example:

First, we add a pre-processor BeanShell PreProcessor or post-processor BeanShell PostProcess, and write a script in it, as shown in the following code:

FileWriter fw = new FileWriter ("E:\\ tokens.csv", false)

BufferedWriter bw = new BufferedWriter (fw)

Bw.write (vars.get ("tokens") + "\ n")

Bw.close ()

Fw.close ()

Now the local E: this path under the new tokens.csv file, false represents the next overwrite last time, if you write true, on behalf of the append, after the new file, new a bufferedWriter object, vars.get means to get the first interface with regular expressions to extract the data tokens, and then write to the file, and then close these two methods, be sure to write close.

A general process is that the regular expression extracts the data, the processor stores the extracted data in the specified file, and configures the original CSV data file to set the data in the file as variables into the data in json format.

Tags: Data interface file variable format processor that is regular expression processing two representation function method usage code helper original outside world object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Huawei macOS Shulou Tech Info