postman——集合——執行集合——測試腳本——pm對象簡單示例01


console.log(pm.info.requestName);                           返回請求名
console.log(pm.info.requestId);                                 返回請求id
console.log("++++++++++++++++++++++++++");
console.log(pm.response.code);                             返回狀態碼
console.log(pm.response.responseTime);           返回響應時間
console.log(pm.response.headers);                   返回響應頭信息,返回一個列表

 

 

 

 

 

 

 

 

===========================================================================================================================

 

 

//返回狀態碼
console.log(pm.response.code);
//
console.log(pm.response.reason());
//返回 Header頭信息
console.log(pm.response.headers);
//返回響應時間
console.log(pm.response.responseTime);
//以文本格式返回body響應b內容
console.log(pm.response.text());
//以json格式返回body響應b內容
console.log(pm.response.json());

--------------------------------------------------------------------------------

 

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,是則真,否則假
-----------------------------------------------------------------------------------------------------------------

實踐斷言響應代碼

以斷言響應狀態是否是200為例

查看結果,相應狀態為200.

 

 

=============================================================

網址:https://www.jianshu.com/p/61047ceb048c

 

 

pm 對象與請求相關的功能有以下幾個:

  • pm.info info 對象包含與正在執行的腳本有關的信息。例如請求名稱,請求ID和迭代計數;
  • pm.variables/pm.environment/pm.globals,變量管理;
  • pm.request,pm 內的 request 對象表示當前腳本所在的請求;
  • pm.response,response 對象表示當前腳本所在請求的響應結果;
  • pm.cookies,cookies 對象包含與請求域相關聯的 cookie 列表;

pm.info

pm.info.eventName:String

獲取當前環境名稱。

console.log(pm.info.eventName); /// 測試環境 

pm.info.iteration:Number

返回當前是第幾次運行迭代,必須要在 Collection 運行才會生效;

console.log(pm.info.iteration); /// 0 

pm.info.iterationCount:Number

計划運行的迭代總數。

console.log(pm.info.iterationCount); /// 4 

pm.info.requestName:String

返回當前請求名稱,前提是請求已被保存到某個 Collection。

console.log(pm.info.requestName); /// test login 

pm.info.requestId:String

獲取標識正在運行的請求的唯一 GUID。

console.log(pm.info.requestName); /// c78dee07-399a-4885-bfcc-ca524e6114f6 

pm.request

request 對象表示腳本所在的當前請求。對於 Pre-request Scripts,相當於即將要發送的請求,在 Tests 中時,相當於是已發送的請求。

請求對象包含以下面結構存儲的信息:

pm.request.url:請求的 url

console.log(pm.request.url); /// {host: [4], path: [3], protocol: "http"…} 

pm.request.headers:請求 headers 列表,與 responseHeaders 不同的是返回的是數組且結構也有區別

console.log(pm.request.headers); /// [{…}, {…}, {…}, {…}, {…}, …] 

pm.request.headers.add(headerName:String):為當前請求添加指定的頭部字段,當然沒有添值。

pm.request.headers.add('xxx'); console.log(pm.request.headers); /// [...{key: "xxx" value: ""}] 

pm.request.headers.delete(headerName:String):刪除指定的頭部字段。

pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):以對象類型傳入鍵值對的形式添加頭部字段。但是要注意的是頭部字段如果存在,不會再次添加只會更新其值。

pm.request.headers.upsert({'content-type': 'application/json'}); 

pm.response

The response details are stored in the following format:

pm.response.code:Number:返回狀態碼

console.log(pm.response.code); /// 200 

pm.response.reason():Function → String:狀態碼描述

console.log(pm.response.reason()); /// OK 

pm.response.headers:HeaderList:響應頭列表

console.log(pm.response.reason()); /// [{…}, {…}, {…}, {…}, {…}, …] 

pm.response.responseTime:Number:響應時間

console.log(pm.response.responseTime); /// 54 

pm.response.text():Function → String:以文本形式返回響應消息體 body 內容

console.log(pm.response.text()); 

pm.response.json():Function → Object:以對象形式返回響應消息體 body 內容,相當於對文本形式的內容做了 JSON.parse,但是要注意只針對 json 格式的響應結果。

console.log(pm.response.json()); 

pm.cookies

pm.cookies: 讀取與當前請求域相關的所有 cookie 的列表

console.log(pm.cookies); /// [{…}, {…}] 

pm.cookies.has(cookieName:String):Function → Boolean:判斷指定的 cookie 是否存在

onsole.log(pm.cookies.has('cookie_token')); /// true 

pm.cookies.get(cookieName:String):Function → String:獲取指定的 cookie 的值

onsole.log(pm.cookies.get('cookie_token')); /// 4c2d02e1...a21ae1f635d88623161 

pm.cookies.toObject():Function → Object:以對象的形式獲取所有 cookie 及其值(不包括 host, domain 等信息)。返回的 cookie 是與請求的域 domain 和 路徑 path 一致的 cookie。

console.log(pm.cookies.toObject()); {{cookie_token: "aff62d5e...fcea38", phpsessid: "a9sun4...fek7"}


作者:貓與測試
鏈接:https://www.jianshu.com/p/61047ceb048c
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

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



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