Postman-斷言


一、環境變量
1、獲取環境變量中的值
 pm.environment.get("variable_key");

2、獲取全局變量中的值
pm.globals.get("variable_key");

3、Tests中設置環境變量中值
pm.environment.set("variable_key", "variable_value");

4、Tests中設置全局變量中值
pm.globals.set("variable_key", "variable_value"); 
 
5、獲取頁面 token 並在環境變量中添加
const $ = cheerio.load(pm.response.text());
var token = $("input[name='__RequestVerificationToken']").prop('value');
pm.environment.set("RequestVerificationToken", token);

6、Tests中清除環境變量中設置的值
pm.environment.unset("variable_key");

7、Tests中清除全局變量中值
pm.globals.unset("variable_key");

8、獲取一個變量
pm.variables.get("variable_key"); 

二、返回的狀態碼
1、判斷返回的狀態碼是200
pm.test("Status code is 200", function () {pm.response.to.have.status(200)});
將200改成500,則判斷的是狀態碼為500
當然還有其他的直接判斷狀態碼的
    pm.response.to.be.info; //檢查響應碼是否為1xx
    pm.response.to.be.success;  //檢查響應碼是否為2xx
    pm.response.to.be.redirection;  //檢查響應碼是否為3xx
    pm.response.to.be.clientError;  //檢查響應碼是否為4xx
    pm.response.to.be.serverError;  //檢查響應碼是否為5xx
    pm.response.to.be.error;  //檢查響應碼是否為4xx或者5xx
    pm.response.to.be.ok;  //檢查響應碼是否為200
    pm.response.to.be.accepted;  //檢查響應碼是否為202
    pm.response.to.be.badRequest;  //檢查響應碼是否為400
    pm.response.to.be.unauthorized;  //檢查響應碼是否為401
    pm.response.to.be.forbidden;  //檢查響應碼是否為403
    pm.response.to.be.notFound;  //檢查響應碼是否為404
    pm.response.to.be.rateLimited;  //檢查響應碼是否為429
 
2、判斷post請求返回的狀態正確
pm.test("Successful POST request", function () {pm.expect(pm.response.code).to.be.oneOf([201,202]);});

3、代碼名稱包含一個字符串
pm.test("Status code name has string", function () { pm.response.to.have.status("Created");}); 

三、body 中內容判斷
1、判斷返回的body中包含某些內容
pm.test("Body matches string", function () {pm.expect(pm.response.text()).to.include("string_you_want_to_search");});

2、判斷響應主體是否等於一個字符串
pm.test("Body is correct", function () {pm.response.to.have.body("response_body_string");});

3、將XML正文轉換為Json對象
var jsonObject = xml2Json(responseBody);

4、對於json數據使用TinyValidator
var schema = {   "items": { "type": "boolean" }};
         
var data1 = [true, false];
var data2 = [true, 123];
 
pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(data1, schema)).to.be.true;
  pm.expect(tv4.validate(data2, schema)).to.be.true;
}); 
 
四、json 文件
Postman中獲取返回的json內容:var jsonData = pm.response.json();

var JsonData = { 
"employees": [ 
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName":"Carter" }
]
}
 
1 、打印json內容
console.log(JsonData);

打開View-->>Show Postman Console,查看打印內容
 
2、 獲取字段的值
獲取某個Json 字段的值:var firstName1 = JsonData.employees[0].firstName;
判斷某個Json 字段的值與預期一致:pm.test("firstName1 is OK", function () { pm.expect(firstName1).to.eql("Bill");}); 
 
3、獲取json子項/數組的個數/長度
獲取json子項/數組的個數/長度:var dataLength = JsonData.employees.length;
判斷json子項/數組的個數/長度與預期一致:pm.expect(JsonData.employees).to.have.lengthOf(3);

4、判斷返回字段值的類型
tests["firstName1 is str type"] = typeof(JsonData.employees[0].firstName) === "string";
常見的類型有:number 、string 、object 、array 、boolean 、undefind 
 
五、headers中內容判斷
1、內容類型存在的判斷
大小寫不敏感:pm.test("Content-Type is present", function () {pm.response.to.have.header("Content-Type");});
大小寫敏感:tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type"); 

  

六、判斷響應時間

1、如下,判斷響應時間不超過200ms
pm.test("Response time is less than 200ms", function () {pm.expect(pm.response.responseTime).to.be.below(200);}); 

 

七、接口請求

1、發送請求
pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(response.json());});

2、接口請求失敗
tests["Request Failed"] = false;
tests["Request sucess"] = true; //接口請求成功 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM