Development tools - script usage of network testing tool POSTMAN
POSTMAN is one of the most commonly used interface testing tools in network programming, capable of continuous automatic testing using scripts. This is based on the local application version.
References:
postman script application basics
Postman can use scripts to preprocess before sending requests, and use scripts to test after receiving responses.
Some of the testing process of the script can be viewed using a browser-like console, in the lower left corner of the application, or using the shortcut "Ctrl + Alt + C"
Set variables, get variable examples
After setting variables in a script, you can use these set variables in requests, for example:
- Set an environment variable in the script, the variable name
veriable_key
, and the variable value areveriable_value
pm.environment.set("variable_key", "variable_value");
- You can use the corresponding method to get the value of this variable and output the result on the console
console.log(pm.environment.get("variable_key"));
- Click Send to view the results on the console
Methods for setting, getting, and clearing can be added using the links on the right
Use of environment variables
After setting the environment variables, they can be used in sending request settings. For example, there is an environment variable named pass:
In this way, you can use the environment variables to set parameters, request headers and other information in the request.
test
Pre-requests are processed before the request is sent, and testing occurs when the response is received. Whether the result of the test method pm.test()
passes or not will be prompted in the test result in the response column.
Send request example
Test requests can be sent directly in the script
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
The error information is in err, and the response information is response.
Commonly used variables and methods
You can add some commonly used fragments on the right side of the application. In addition, there are some variables and methods that can be used:
responseBody gets the response body content
responseBody is the content of the response packet, type is string. If it is json, you can use JSON.parse(responseBody)
it to convert it into an object.
pm.response gets response information
pm.response is an attribute of the pm object, used to record some response information. If the response is json, you can use pm.response.json()
to get it.
pm.expect() JSON inspection
pm.expect()
The method is used to check json data, for example, check the value of numRaters of the first rating attribute in the book (array type) attribute of a json object, which is 51459:
let jsonData = pm.response.json();
// 测试检查 jsonData.book[0].rating.numRaters 属性的值
pm.expect(jsonData.book[0].rating.numRaters).to.eql(51459);
cheerio.load() is similar to jquery in operating html elements
When the response content is a page and the content_type is text/html, you can use this method to obtain each element in the page, and use a method similar to jquery to obtain the required data.
const $ = cheerio.load(pm.response.text())
console.log($('title').text()) // 获取 title 标签的文本内容
console.log($('#csrf').attr('value')) // 获取标签的 value 属性值
In this way, the required data can be saved to environment variables for use.