參考 官網文檔
測試示例
大多數這些在Postman中可以作爲片段。大多數測試都與簡單的JavaScript一樣。可以根據要求提供儘可能多的測試。
設置環境變量
postman.setEnvironmentVariable("key", "value");
或者
pm.environment.set("variable_key", "variable_value");
這兩種方式使用其中一種即可,根據自己的喜愛,以下示例使用pm對象,其他的可以參考官方文檔的後半部分
將嵌套對象設置爲環境變量
var array = [1, 2, 3, 4];
pm.environment.set("array", JSON.stringify(array, null, 2));
var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
pm.environment.set("obj", JSON.stringify(obj));
獲取環境變量
pm.environment.get("variable_key");
獲取一個環境變量(其值是一個字符串對象)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));
清除環境變量
pm.environment.unset("variable_key");
設置一個全局變量
pm.globals.set("variable_key", "variable_value");
獲取全局變量
pm.globals.get("variable_key");
清除一個全局變量
pm.globals.unset("variable_key");
獲取一個變量
從全局變量和激活的環境中(當前環境)
pm.variables.get("variable_key");
檢查響應是否包含一個字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
或者
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
書寫test:舊方式是使用tests[],新方式是pm.test
檢查返回體是否等於一個字符串
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
檢查json值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
檢查當前的Content-Type
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
檢查相應時間小於200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
檢查http響應碼是否爲200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
狀態碼名稱包含一個字符串
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
或者
tests["Status code name has string"] = responseCode.name.has("Created");
成功的POST請求狀態碼
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
或者
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
將TinyValidator用於JSON數據
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;
});
console.log("Validation failed: ", tv4.error);
解碼base64編碼數據
var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
pm.test('Contents are valid', function() {
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});
發送匿名請求
該請求適用於pre-request和test
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
xml轉json
var jsonObject = xml2Json(responseBody);
請指正~