Test Tool-Postman interface test introduction manual, how Postman performs data association, automatically update cookies, simple programming

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
Insert picture description here

② Account login

Just come in and need to create a new account.
Insert picture description here
enter main page.
Insert picture description here

③ Obtain the packet capture tool

Obtaining address: Fiddler official website
Insert picture description here
Fill in the information to download.
Insert picture description here

Two, interface test

① Create a directory

Create a collection folder to store our recorded interface use cases.
Insert picture description here
Insert picture description here
Folders can be added under the directory for classification management, right click to add folders.
Insert picture description here
Insert picture description here

② 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.
Insert picture description here
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.
Insert picture description here
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.
Insert picture description here

③ 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.
Insert picture description here

④ 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.
Insert picture description here
Click Key-Value Edit to switch back.
Insert picture description here

⑤ 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.
Insert picture description here

⑥ 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 . Insert picture description here
Generally, the data returned by json will have a status, which can also be used to see if it is successful.
Insert picture description here

⑦ Use case playback

Click runner in the upper left corner to enter the playback page.
Insert picture description here
We select the necessary script and replay it.
Insert picture description here

⑧ 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.
Insert picture description here
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.
Insert picture description here
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 .
Insert picture description here
We can see it here after we set the environment variables.
Insert picture description here
You can see that the value in set-cookis is the cookie information we need .
Insert picture description here
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]);

Insert picture description here
If the settings are correct, test it and you can see that the cookie value we need has been extracted.
Insert picture description here
Then we refer to the previous cookie environment variable, and the request is sent successfully.
Insert picture description here

② 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 .
Insert picture description here
You can see that this value is already in the environment variable.
Insert picture description here

③ 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.
Insert picture description here
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");

Insert picture description here
The raw reference variable of the body also uses double brackets.
Insert picture description here

④ json data format

Clicking beautify can be used to format json data, which is convenient for us to observe and process data.
Insert picture description here
Like it if you like it ❤!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/103721713