一、獲取環境變量中的值pm.environment.get("variable_key");
二、獲取全局變量中的值pm.globals.get("variable_key");
三、Tests中設置環境變量中值pm.environment.set("variable_key", "variable_value");
四、Tests中設置全局變量中值pm.globals.set("variable_key", "variable_value");五、獲取頁面 token 並在環境變量中添加const $ = cheerio.load(pm.response.text());var token = $("input[name='__RequestVerificationToken']").prop('value');pm.environment.set("RequestVerificationToken", token);
六、Tests中清除環境變量中設置的值pm.environment.unset("variable_key");
七、Tests中清除全局變量中值pm.globals.unset("variable_key");
八、獲取一個變量pm.variables.get("variable_key");
一、判斷返回的狀態碼是200pm.test("Status code is 200", function () {pm.response.to.have.status(200)});將200改爲500,則判斷的是狀態碼爲500固然還有其餘的直接判斷狀態碼的pm.response.to.be.info; //檢查響應碼是否爲1xxpm.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二、判斷post請求返回的狀態正確pm.test("Successful POST request", function () {pm.expect(pm.response.code).to.be.oneOf([201,202]);});
三、代碼名稱包含一個字符串pm.test("Status code name has string", function () { pm.response.to.have.status("Created");});
一、判斷返回的body中包含某些內容pm.test("Body matches string", function () {pm.expect(pm.response.text()).to.include("string_you_want_to_search");});
二、判斷響應主體是否等於一個字符串pm.test("Body is correct", function () {pm.response.to.have.body("response_body_string");});
三、將XML正文轉換爲Json對象var jsonObject = xml2Json(responseBody);
四、對於json數據使用TinyValidatorvar 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;});
Postman中獲取返回的json內容:var jsonData = pm.response.json();
var JsonData = {"employees": [{ "firstName":"Bill" , "lastName":"Gates" },{ "firstName":"George" , "lastName":"Bush" },{ "firstName":"Thomas" , "lastName":"Carter" }]}
1 、打印json內容console.log(JsonData);
打開View-->>Show Postman Console,查看打印內容二、 獲取字段的值獲取某個Json 字段的值:var firstName1 = JsonData.employees[0].firstName;判斷某個Json 字段的值與預期一致:pm.test("firstName1 is OK", function () { pm.expect(firstName1).to.eql("Bill");});三、獲取json子項/數組的個數/長度獲取json子項/數組的個數/長度:var dataLength = JsonData.employees.length;判斷json子項/數組的個數/長度與預期一致:pm.expect(JsonData.employees).to.have.lengthOf(3);
四、判斷返回字段值的類型tests["firstName1 is str type"] = typeof(JsonData.employees[0].firstName) === "string";常見的類型有:number 、string 、object 、array 、boolean 、undefind
一、內容類型存在的判斷大小寫不敏感:pm.test("Content-Type is present", function () {pm.response.to.have.header("Content-Type");});大小寫敏感:tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
6、判斷響應時間web
一、以下,判斷響應時間不超過200mspm.test("Response time is less than 200ms", function () {pm.expect(pm.response.responseTime).to.be.below(200);});
7、接口請求json
一、發送請求pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(response.json());});
二、接口請求失敗tests["Request Failed"] = false;tests["Request sucess"] = true; //接口請求成功