postman 版本信息:
VARIABLE 變量 INITIAL VALUE 默認值 CURRENT VALUE 當前值
選擇切換環境-》點擊眼睛查看當前環境變量值
二:集合變量(項目變量):在一個集合以及集合下面的目錄中的請求都可以使用。主要給某組請求使用。比如我們可以定義一個項目為一個collection,請求token變量定義為一個集合變量。
示例:定義一個變量測試集合,並添加集合變量ca。
創建設置:collections->add collections (-> edit )->variables
三:全局變量:整個postman中的請求都可以使用。
示例:創建全局變量ga。
創建設置:變量設置-》global-》變量添加
四:普通變量-在請求周期內某個階段定義,后續階段可以使用。比如在 Request Pre-request Script中定義,則后續可以在Collection Test中使用
請求生命周期圖:
五:使用四種變量
1 在請求中通過{{變量名使用}},比如url地址或者raw中調用。
示例:http://httpbin.org/get?a={{a}}&ca={{ca}}&ga={{ga}}
2 在 Pre-request Script和Tests腳本中使用
Pre-request Script 可以設置變量,並且會修改current value 值。
環境變量操作
pm.environment.get("variable_key");
pm.environment.set("variable_key", "variable_value");
集合變量操作
pm.collectionVariables.get("variable_key");
pm.collectionVariables.set("variable_key", "variable_value");
全局變量操作
pm.globals.get("variable_key");
pm.globals.set("variable_key", "variable_value");
普通變量操作
pm.variables.get("variable_key");
pm.variables.set("variable_key", "variable_value");
3 Pre-request Script和Tests 執行次序
collection Pre-request Script -》 folder Pre-request Script -》 本次request Pre-request Script ===》請求 Request -》響應 Response ===》collection Tests -》 folder Tests -》本次request Test
參考:
https://learning.getpostman.com/docs/postman/scripts/intro-to-scripts/