Postman interface test
- Preface
- 1. Tool installation
- ① Get tools
- ② Account login
- ③ Obtain the packet capture tool
- Two, interface test
- ① Create a directory
- ② Write script
- ③ Crawl request
- ④ Fill in the head of the use case
- ⑤ Fill in the main content of the use case
- ⑥ Test
- ⑦ Use case playback
- ⑧ Set assertion and checkpoint
- Three, advanced settings
- ① Obtain cookies
- ② Data association, extract the last requested data
- ③ The address bar refers to global variables
- ④ json data format
Preface
Postman is a very popular interface testing tool in the testing field.
This article introduces the entire process of the tool from installation, to recording use cases, and then to smooth use case playback. Some more practical methods are introduced later, such as data association and automatic updating of cookies .
I hope that this article's continuous guidance from the shallower to the deeper can help Xiaobai to quickly master the tools.
1. Tool installation
① Get tools
Get the address: Postman official website download
② Account login
Just come in and need to create a new account.
enter main page.
③ Obtain the packet capture tool
Obtaining address: Fiddler official website
Fill in the information to download.
Two, interface test
① Create a directory
Create a collection folder to store our recorded interface use cases.
Folders can be added under the directory for classification management, right click to add folders.
② Write script
For recording interface use cases, you can directly click the recording request request in the upper right corner, or you can directly click the upper right corner.
Enter the name of the use case, and you can select the location of the file to be saved below. I saved it in the folder I just created.
There are many kinds of request methods, which form depends on the request method displayed in the header. The plus sign above can add new requests.
③ Crawl request
We use the fiddler tool to fetch the request. Check the raw format of the captured request , you can see that the above is the header parameters, and the bottom is the json body parameters. We fill the fetched value into the use case we created.
④ Fill in the head of the use case
The first parameter of the head parameter is filled in above, and the other parameters are filled in below. By Bulk Edit can quickly to fill the head content.
Click Key-Value Edit to switch back.
⑤ Fill in the main content of the use case
Select the raw format, paste the contents of the table body, and then select the JSON format.
⑥ Test
Click send to see if the return value is correct. The figure shows that I returned normal JSON data, which proves that the test was successful. Not sure if the returned value is correct, you can do the same operation in a normal browser and compare the returned value with the network of the developer tool .
Generally, the data returned by json will have a status, which can also be used to see if it is successful.
⑦ Use case playback
Click runner in the upper left corner to enter the playback page.
We select the necessary script and replay it.
⑧ Set assertion and checkpoint
When we test, we look at the response return value. If it succeeds, there will be a flag. We set this flag as a checkpoint to see if our interface is successful.
Click test in the navigation bar above , then select Response body: Contains string on the right , then some more code will be added, and fill in the text to be tested in the position I circled.
Note: If the detected text contains double quotes, you need to add a forward slash "\" to escape.
Show you two common ways to set checkpoints.
// 断言类型1 - 检查返回值
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("\"success\":true");
});
// 断言类型2 - 检查状态码
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Three, advanced settings
① Obtain cookies
Cookies will expire after a period of time. If the new operation does not retrieve the cookies returned by the login operation , it will no longer be available after a period of time.
Create a new environment to save environment variables, and let it automatically store the new cookies in the environment variables every time you replay .
We can see it here after we set the environment variables.
You can see that the value in set-cookis is the cookie information we need .
Choose the environment above as the environment we created.
Enter our code to extract the cookie , and then send the request.
// 获取返回值头部set-cookie的内容
var jsondata = postman.getResponseHeader("set-cookie");
// 通过;号来分割数据,转化为数组形式
data = jsondata.split(";");
// 设置环境变量,data数组里的第一个值即我们需要的cookie
postman.setEnvironmentVariable("Cookie",data[0]);
If the settings are correct, test it and you can see that the cookie value we need has been extracted.
Then we refer to the previous cookie environment variable, and the request is sent successfully.
② Data association, extract the last requested data
The submission action often requires the id generated when the document is saved . At this time, we need to associate context data to submit successfully.
// 把responseBody转为json字符串
var data = JSON.parse(responseBody);
// 把templetid提取出来,存到环境变量里
pm.environment.set("templetid", data.data.templetid);
In the example: I want to extract the json value I circled .
You can see that this value is already in the environment variable.
③ The address bar refers to global variables
Set up a global variable, and then let the address bar reference.
You can add global variables directly in the environment.
Enclose the two curly braces to reference global variables.
You can also add global variables through code.
// 添加全局变量
pm.globals.set("url", "http://test.nc-cloud.com");
The raw reference variable of the body also uses double brackets.
④ json data format
Clicking beautify can be used to format json data, which is convenient for us to observe and process data.
Like it if you like it ❤!