Postman官方blog
https://www.getpostman.com/docs/pre_request_scripts
發(fā)送請求
Postman界面分為兩個部分,左邊是工具欄,右邊是請求編輯器

URL
一個URL就是一個接口,接口大致會分為一下幾個部分

- 請求協(xié)議:
http --- 普通的http請求
https --- 加密的http請求,傳輸數(shù)據(jù)更加安全 - 請求IP:就是指提供接口的系統(tǒng)所部署的服務(wù)器地址
- 請求端口:默認是80
- 接口路徑:指系統(tǒng)提供的接口在什么位置
- 接口參數(shù):參數(shù)在接口路徑后,用“?”來表示路徑地址完了,剩下的都是參數(shù)了,用“&”來區(qū)分參數(shù)個數(shù)
Params(參數(shù))
點擊URL Params按鈕,會打開key-value編輯參數(shù),這樣比較直觀

Headers
HTTP Headers是HTTP請求和相應(yīng)的核心,它承載了關(guān)于客戶端瀏覽器,請求頁面,服務(wù)器等相關(guān)的信息。

Method
GET
POST
Request body
post請求參數(shù)在實體中

環(huán)境變量
我們可能需要在多個環(huán)境下對同一個接口進行測試。比如我們請求的域名,開發(fā)、測試、生產(chǎn)環(huán)境,請求參數(shù)。在地址欄、header、請求參數(shù)、外部數(shù)據(jù)文件里,用 {{變量名}} 獲取環(huán)境變量的值
Postman 的環(huán)境變量分為 environment 和 global 2種
Environment
手動設(shè)置環(huán)境變量
- 點擊設(shè)置按鈕
-
選擇 Manage Environment
- 點擊Add
-
填寫變量名和變量值
代碼自動創(chuàng)建環(huán)境變量
- 請求發(fā)起之前創(chuàng)建,在Pre-request Script標(biāo)簽里面添加代碼
postman.setEnvironmentVariable("variable_key", "variable_value");
-
在某個請求發(fā)起之后創(chuàng)建,在Tests標(biāo)簽里面添加如下
變量的引用
引用的時候加上雙花括號:{{變量名}}
動態(tài)請求參數(shù)
Postman有以下三種內(nèi)建變量:
{{$guid}} // 生成GUID
{{$timestamp}} // 當(dāng)前時間戳
{{$randomInt}} // 0-1000的隨機整數(shù)
在Pre-request Script里寫代碼處理,存為環(huán)境變量,參數(shù)里用{{變量名}}取值
例如
// 隨機整數(shù)
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
// 隨機選項
const getRandomValue = list => list[randomInt(0, list.length - 1)];
// 隨機手機
environment.randomMobile = `18${randomInt(100000000, 999999999)}`;
// 隨機設(shè)備名
environment.randomDevice = getRandomValue(['ios', 'android']);
//隨機生成一個字符串作為用戶名
postman.setEnvironmentVariable("random_user", ("0000" +
(Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
//發(fā)起請求之前獲取當(dāng)前的時間戳放在參數(shù)里:
postman.setEnvironmentVariable("unixtime_now", Math.round(new Date().getTime()/1000));
Test
Setting an environment variable
postman.setEnvironmentVariable("key", "value");
Getting an environment variable
postman.getEnvironmentVariable("key");
Set a global variable
postman.setGlobalVariable("key", "value");
Get a global variable
postman.getGlobalVariable("key");
Check if response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
Convert XML body to a JSON object
var jsonObject = xml2Json(responseBody);
Check if response body is equal to a string
tests["Body is correct"] = responseBody === "response_body_string";
Check for a JSON value
var data = JSON.parse(responseBody); tests["Your test name"] = data.value === 100;
Content-Type is present (Case-insensitive checking)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
Content-Type is present (Case-sensitive)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
Response time is less than 200ms
tests["Response time is less than 200ms"] = responseTime < 200;
Status code is 200
tests["Status code is 200"] = responseCode.code === 200;
Code name contains a string
tests["Status code name has string"] = responseCode.name.has("Created");
Succesful POST request status code
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
Use TinyValidator for JSON data
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);
實例
//驗證本次請求的狀態(tài)碼
tests["Status code is 200"] = responseCode.code === 200;
tests["檢查返回的數(shù)據(jù)是否有成功執(zhí)行"] = responseBody.has("成功執(zhí)行");
tests["響應(yīng)時間不超過500ms"] = responseTime < 500;
//先聲明一個變量jsonData,調(diào)用JSON.parse()方法將responseBody轉(zhuǎn)換成json的標(biāo)準(zhǔn)格式并賦值給變量jsonData,將返回結(jié)果傳給其他接口使用
//檢測JSON中的某個值是否等于預(yù)期的值;
//JSON.parse()方法,把json字符串轉(zhuǎn)化為對象。parse()會進行json格式的檢查是一個安全的函數(shù)。
var jsonData = JSON.parse(responseBody);
var jsonObject = xml2Json(responseBody);
//檢測errcode字段的值是否為0
tests["errcode is 0"] = jsonData.errcode === `0`;
檢測errcode字段的長度是否是1
tests["errcode's length"] = jsonData.errcode.length ===1
tests["description is 成功執(zhí)行"] = jsonData.description === '成功執(zhí)行'
tests["description's length"] = jsonData.description.length ===4
//檢測description的數(shù)據(jù)類型是否是String
tests["description 的數(shù)據(jù)類型為String"] = _.isString(jsonData.description)
//測試response Headers中的某個元素是否存在(如:Content-Type)
//getResponseHeader()方法會返回header的值,如果該值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
//判斷字段的類型
var schema = {
properties:{
errcode: {type: 'String'},
description:{type:'String'}
}
};
tests["響應(yīng)實體的類型 "] = tv4.validate(responseBody, schema);



