Advanced Postman (1) - Getting started with pre-request script and implementing random numbers for parameters

Starting from this article, Postman's series of tutorials will enter the advanced chapter. The content introduced later will be more difficult than before, but if you learn this knowledge, it can definitely become a bonus point for your personal ability and can also be added to your resume. Highlights

1 Overview

Pre-request Script As the name suggests, its definition is "a script that is executed before the request . "

At the same time, this script supports configuration under the interface collection. After configuration, all scripts in this interface combination will execute this script before each request.

When writing a script, if you want to debug the script, you can add log statements to the script. When the script runs, the console will output corresponding logs.

2. Write Pre-request Script (pre-request script)

Pre-request Script (pre-request script) is written in JavaScript language. Writing this script does not require proficiency in the JavaScript language. Most of the logic can be completed using simple codes. If you have a foundation in other programming languages, you can quickly start writing pre-request scripts.

3. Common methods of Pre-request Script

On the right side of the pre-request script tab, Postman provides several common methods. After clicking, the statement is directly generated and printed on the tab.
Insert image description here
Commonly used methods are as follows:

pm.environment.get("variable_key"); // 获取环境变量
pm.globals.get("variable_key"); // 获取全局变量
pm.variables.get("variable_key"); // 获取变量,该函数在全局变量和当前环境变量中搜索变量
pm.collectionVariables.get("variable_key"); // 获取集合变量
pm.environment.set("variable_key", "variable_value"); // 设置环境变量
pm.globals.set("variable_key", "variable_value"); // 设置全局变量
pm.collectionVariables.set("variable_key", "variable_value"); // 设置集合变量
pm.environment.unset("variable_key"); // 删除环境变量
pm.globals.unset("variable_key"); // 删除全局变量
pm.collectionVariables.unset("variable_key"); // 删除集合变量
// 发送请求
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    
    
console.log(response.json());
});

4. Use Pre-request Script to obtain random numbers for request parameters

We directly use a simple example to illustrate how to write a script:
when doing daily work, sometimes testing an interface, and do not want to use the same parameter request every time, then you can use the pre-request script to generate an interface request before Random number and called in request parameters.

Suppose, in the interface request shown below, I want the parameter "test" in the body to randomly pick a number between 0 and 9 every time.
image

Write the script under the pre-request script tab of the interface. The complete code is as follows:

//定义一个随机数(0, 9)之间
var test = parseInt(Math.random()*9);
//打印出生成的随机数,便于调试
console.info("随机生成test为",test)
//将生成的随机数设置为全局变量
pm.globals.set("test", test);

First, get a random number. Math.random() is a method of getting random numbers in JavaScript. This method is very common. There are many ways to use it on the Internet and you can easily search it.

var test = parseInt(Math.random()*9);

If we want to debug the results, we can print the obtained random numbers in the postman console .

console.info("随机生成test为",test)

The entrance to the console is at the bottom of the postman page. After clicking it, the display effect of the above log is as follows: (Detailed introduction to the console: console console )

image.png

Then, generate the generated number into a global variable for easy reference in request parameters.

pm.globals.set("test", test);

Finally reference the variable in the request parameter

image.png

Then click the sending interface. After the sending is successful, you can view the request parameters of the sending interface through the console.

image.png
You can see that the randomly generated data "4" is used in the parameter. In this way, the request parameter uses a random number when making a request.

If you feel that the method of creating global variables and then referencing them is too cumbersome, you can also directly add parameters in the Pre-request Script. The code is as follows:

//定义一个随机数(0, 9)之间
var test = parseInt(Math.random()*9);
//打印出生成的随机数,便于调试
console.info("随机生成test为",test)
// 在body中添加参数test,并且值为随机数
pm.request.body.formdata.add({
    
    'key':'test','value':test});

After the code is written, the parameters in the body can be deleted.
image.png

Then click Send, you can see that this can also achieve the effect of the parameter value being a random number during the request.

image.png

Postman series article directory:
https://blog.csdn.net/weixin_40883833/article/details/126452017
———————————————————————————— ——————
The sample files used in the postman series of articles have been uploaded to the public account I operate [Essential Skills for Software Testing]
Insert image description here
If necessary, you can click on the article to issue a QR code and go to get it~
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_40883833/article/details/126550398