postman 筆記1:入門

1. Bulk Edit(x-www-form-urlencoded)

key-value edit樣式,點(diǎn)擊右側(cè)Bulk Edit樣式修改

bulk edit樣式,點(diǎn)擊Key-Value Edit樣式修改

2. 控制臺(tái)

postman中控制臺(tái)位置

console.log("url2 = " + url2) # 控制臺(tái)輸出


3. pm

https://blog.csdn.net/qq_39314932/article/details/103037976

4. Pre-request Script 常用代碼

4.1 pm.environment

  • 獲取環(huán)境變量
    注:若無該變量則返回undefined
    pm.environment.get("variable_key"); # 注意變量前后的雙引號(hào)
  • 設(shè)置環(huán)境變量
    pm.environment.set("variable_key", "variable_value");
  • 清除環(huán)境變量
    pm.environment.unset("variable_key");

4.2 pm.globals

全局變量
pm.globals.get("variable_key");
pm.globals.set("variable_key", "variable_value");
pm.globals.unset("variable_key");

4.3 pm.variables

變量
pm.variables.get("variable_key");
pm.environment.set("variable_key", "variable_value");
pm.environment.unset("variable_key");

4.4 pm.collectionVariables

collection變量
pm.collectionVariables.get("variable_key");
pm.collectionVariables.set("variable_key", "variable_value");
pm.collectionVariables.unset("variable_key");
注:collections

collections


5. Test 常用代碼

基于nodejs,如JSON.parse

5.1 pm.test

pm.test函數(shù)定義測(cè)試,提供一個(gè)名稱和函數(shù),該函數(shù)返回一個(gè)布爾值(falsetrue),指示測(cè)試時(shí)通過還是失敗

  • 第一個(gè)參數(shù),是字符串類型,表示該斷言的內(nèi)容
  • 第2個(gè)參數(shù),回調(diào)函數(shù)?;卣{(diào)函數(shù)內(nèi)部運(yùn)行斷言語句。
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);  //判斷響應(yīng)結(jié)果是否包含指定狀態(tài)碼
});
//
pm.response.to.have.status(code:Number):判斷響應(yīng)結(jié)果是否包含指定的狀態(tài)碼;
pm.response.to.have.status(reason:String):判斷響應(yīng)結(jié)果是否包含指定的狀態(tài)描述;
  • 請(qǐng)求后狀態(tài)碼描述
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
狀態(tài)碼描述

5.2 pm.expect

通用斷言,傳遞給expect函數(shù)的值是否符合預(yù)期;經(jīng)常與pm.test結(jié)合使用

  • 請(qǐng)求成功后狀態(tài)碼
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
  • 響應(yīng)體中是否包含某字符串
pm.test("Body matches string", function () {
  pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
  • 檢查響應(yīng)體是否等于字符串
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
//
pm.response.to.have.body():判斷響應(yīng)結(jié)果是否包含消息體;
pm.response.to.have.body(optionalValue:String):判斷響應(yīng)結(jié)果是否等于指定的字符串;
pm.response.to.have.body(optionalValue:RegExp):判斷響應(yīng)結(jié)果是否包含指定的符合正則表達(dá)式規(guī)則的字符串;
  • 使用json格式來判斷值
pm.test("Your test name", function () {
    var jsonData = pm.response.json();  //獲取返回body
    pm.expect(jsonData.value).to.eql(100);
});
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;
});
  • 內(nèi)容類型存在
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
//
pm.response.to.have.header(key:String):判斷響應(yīng)結(jié)果是否包含指定的頭部字段;
pm.response.to.have.header(key:String, optionalValue:String):判斷響應(yīng)結(jié)果是否包含指定的頭部字段和值;
  • 響應(yīng)時(shí)間小于200毫秒
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
  • 轉(zhuǎn)換XML body為 JSON body
var jsonObject = xml2Json(responseBody);
  • 發(fā)送請(qǐng)求
    也可以放置在pre-requests中
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});
pm.except()接收一個(gè)斷言內(nèi)容
.to是連接符,如 be、been、is、that、which、and、hashave、withat、ofsame
.include()用于指定斷言方式和預(yù)期結(jié)果
  • .equal(value) 相等,即===
  • .not 取非,對(duì)之后的斷言取反
  • .include(value)包含,value可以是Object|String|Number
  • .contains(value) 包含
  • .ok 判斷是否為真;進(jìn)行類型轉(zhuǎn)換
  • .true 判斷是否為真;不進(jìn)行類型轉(zhuǎn)換,只能是布爾值true

5.3 pm.response

pm.response.to.not.be.error;
pm.response.to.have.jsonBody('');
pm.response.to.not.have.jsonBody('error');
pm.response.text()
pm.response.json();
pm.response.responseTime// 獲取響應(yīng)時(shí)間
pm.response.code //獲取狀態(tài)碼

注:上述使用Postman for Windows(Version 8.11.1)
參考:

  1. Postman之Pre-request Script 使用詳解
  2. Postman中Tests腳本總結(jié)
  3. Postman pm對(duì)象-測(cè)試相關(guān)功能
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容