腳本
腳本采用ECMAScript語言,最新版本支持ES6語法.
利用腳本可以降低聯調工作量.
例如這樣一個場景:讀取第一個請求響應結果中的數據,構造下一個請求;如果人工拷貝,費眼,費勁,費神;而寫成腳本,則省不少事.
全局變量與環境變量
全局變量只有一組,環境變量可以有多組;
postman先嘗試從環境變量中獲取變量,獲取不到則嘗試從全局變量中獲取變量;
使用少的,建議使用全局變量;使用多的,建議使用環境變量;使用newman腳本時,建議使用環境變量;
使用變量
凡是可以輸入數據的地方,都可以使用變量,使用兩個花括號將變量包起來.
{{foo}}
設置環境變量
pm.environment.unset("foo");
pm.environment.set("foo", "gists");
設置全局變量
pm.globals.unset("foo");
pm.globals.set("foo", "gists");
前置腳本
http請求前執行;用於設置參數;
后置腳本
http請求后執行;可以測試請求執行結果;可以解析結果后,設置變量,用於下一個請求;
前置腳本和后置腳本的示例
(截圖太麻煩,這是導出的腳本)
{
"info": {
"_postman_id": "1c956670-9c33-40a8-b881-34f0b5c73ca7",
"name": "github",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "GetUserInfo",
"event": [
{
"listen": "prerequest",
"script": {
"id": "9c67def9-8629-4fa1-8902-67098645c8df",
"type": "text/javascript",
"exec": [
"",
"pm.globals.unset(\"foo\");",
"",
"pm.globals.set(\"foo\", \"gists\");",
""
]
}
},
{
"listen": "test",
"script": {
"id": "7c627a4f-dddd-4936-877c-cf7a76107d4e",
"type": "text/javascript",
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
" pm.globals.set(\"bar\", pm.response.json()[0].url);",
" ",
" console.log(pm.response.json()); ",
"});",
"",
""
]
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {},
"url": {
"raw": "https://api.github.com/users/octocat/{{foo}}",
"protocol": "https",
"host": [
"api",
"github",
"com"
],
"path": [
"users",
"octocat",
"{{foo}}"
]
}
},
"response": []
}
]
}
發送請求
其他功能點
查看請求原始報文
View->Show Postman Console
Alt+Ctrl+C
打開開發者工具, 調試, 或者查看console.log的輸出
View-->Developer->Show DevTools(current view)
Ctrl+Shift+I
SSL
(1) File->Settings->General->SSL certificate verification, 關閉之
(2) File->Settings->Certificates->Add Certificate
配置:
Host: IP地址 端口號(默認443, 取決於你要測哪個端口)
CRT file: 信任證書
KEY file: 個人證書
Passphrase: 個人證書的加密密碼
導出腳本, 批量執行腳本
https://github.com/postmanlabs/newman
導出腳本
略
安裝工具
npm install newman --save-dev;
npm init 新建工程
新建index.js
let newman = require("newman"); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run(
{
collection: require("./github.postman_collection.json"),
reporters: "cli"
},
function(err) {
if (err) {
throw err;
}
console.log("collection run complete!");
}
);
執行結果
√ Status code is 200
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 1 │ 0 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 4s │
├───────────────────────────────────────────────┤
│ total data received: 13.7KB (approx) │
├───────────────────────────────────────────────┤
│ average response time: 3s │
└───────────────────────────────────────────────┘
collection run complete!