Liver finishing, Postman interface test - parameter association actual combat (detailed steps)


foreword

When does an interface test need parameter association?

When the data returned by the previous interface needs to be used as the parameter or request header of the next interface, parameter association is required, that is, interfaces that depend on each other require parameter association

Two ways of parameter association

1. Create two interfaces, obtain the returned data in the Test of the previous interface and save it as a global variable or environment variable, and then refer to the next interface as an input parameter. At this time, because the two interfaces are related, Therefore, the two interfaces must be executed sequentially to pass data between each other.

2. Create an interface, send the dependent request in Pre-request Script before sending the current request, get the interface return and save it as a global variable or environment variable, and then reference it where the interface needs (such as Header)

For example: Many systems need to log in successfully before performing other operations, so the login interface needs to return a token as a parameter for the next interface. The following figure is a query interface information I obtained from packet capture

C1

Through the above information, I found that this interface needs a token value to send successfully, so I need to get the token value for login first.

The body message returned by the login interface is as follows, which is in json format:

{
    
    
    "code": 0,
    "msg": "success",
    "data": {
    
    
        "token": "53a43036d5d6f132c86720a735bf106a",
        "expire": 27141
    }
}

way 1

Enter the following code in Tests of the login interface:

var responseData = JSON.parse(responseBody);//把responseBody的内容转化成json格式存储在responseData 里面
token=responseData.data.token//获取token的值
pm.environment.set("Token",token) //将token设置为环境变量Token

C2

Then, in the interface that needs to use token, such as query interface, just replace the corresponding value with {{Token}}

C3

way 2

Send the login interface in the Pre-request Script of the current query interface and get the returned token value of the interface and save it as an environment variable

//定义post的参数
var paramdata = {
    
    
"_t":"1619059684163",
"password":"****",
"platformType":"1",
"systemCode":"dcp-adm",
"username":"***"
}
//const定义一个post请求常量
const loginRequest =
 {
    
    
    url: 'http://127.0.0.1:14444/user/login',  
    method: 'POST',
    header: 
    {
    
    'Content-Type':'application/json;charset=utf-8',
     'Accept': 'application/json;charset=utf-8'
    },
    body: //请求体信息,请求body中携带的参数
    {
    
    
        mode: 'raw',
        raw: JSON.stringify(paramdata) 
    }
};
//发送登录请求
pm.sendRequest(loginRequest, function (err, res)
{
    
    
    if (res.json().data.token) {
    
    
    tests["Body has token"] = true;
//把得到的结果,设置环境变量token
    pm.environment.set("tokenValue",res.json().data.token);
}
else {
    
    
tests["Body has token"] = false;
}}
);

C4

Then, where the token value needs to be changed, replace it with tokenValue, as shown in the figure

C5

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Light up the flame in your heart, and the struggle is not limited to dreams. Temper your will and work hard to move forward. Difficulties are only temporary obstacles in the way. Facing challenges bravely, raising the banner of faith, going forward indomitably, you will eventually reap your own glory and success!

Keep walking, go forward bravely, struggle is the melody of chasing dreams. No matter how many difficulties and challenges we face, persistent efforts will surely open the door to success. Believe in yourself, surpass yourself, and bloom a brilliant chapter of life!

With faith in mind, go forward bravely, and struggle like stars shining in the sky. Forge a tough soul in adversity, and never stop chasing dreams. Believe in your own strength, burn your passion, and you will eventually create your own brilliant life!

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/132083550