当前位置:网站首页>Postman interface test (II. Set global variables \ sets)

Postman interface test (II. Set global variables \ sets)

2022-07-07 09:08:00 Song_ Lun


use postman When doing interface tests We often encounter delivery token/Authorization And dynamic id The problem of , It is automatically generated after the user logs in token/Authorization After that, the next interface is written manually every time header Inside , This is actually quite troublesome .

Manually set variables

Be careful ! Variable references use { { value }} , and jmeter equally .
 Insert picture description here

Automatically get the value and fill it in dynamically

Pass on token

Log in successfully and get token And set it to variable

  1. First, in the test Add to determine whether the interface returns successfully .
//  First, judge whether the interface login is successful or not 200
pm.test("Status code is 200", function () {
    
    pm.response.to.have.status(200);
});
  1. And then get token take token Set as variable
//  obtain data  The data in it (id,token etc. )
var data = JSON.parse(responseBody);

if (data.data.id) {
    
  tests["Body has id"] = true;
   postman.setEnvironmentVariable("id", data.data.id);
}
else {
    
  tests["Body has token"] = false;
}
  1. Set the variable name in the variable , Value is empty ; Variable id With the following id Corresponding
     Insert picture description here

Pass on Authorization

Authorization And token Dissimilarity It is placed in the header among
 Insert picture description here
test Parameter write the following assertion judgment

//  First, judge whether the interface login is successful or not 200
pm.test("Status code is 200", function () {
    
    pm.response.to.have.status(200);
});


//  obtain header  Inside  Authorization
var jsondata = postman.getResponseHeader("Authorization");
postman.setEnvironmentVariable("Authorization", jsondata);
console.log(jsondata)

 Insert picture description here

Global interface usage

Pass on Data Next Tonkerh&id

 Insert picture description here

Pass on Herder Next Authorization

 Insert picture description here
Let's take a look at the effect
 Insert picture description here

原网站

版权声明
本文为[Song_ Lun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130628526958.html