1. 程式人生 > >postman自動化測試

postman自動化測試

測試工具主要包括三部分:(1)發起請求前執行的Pre-request script;(2)收到應答之後執行的Test;(3)一次執行所有請求的Collection Runner。

postman是Postman提供的全域性物件,environment和global同名時,優先用environment;global只建議用在1種場景:定義公共函式

  1. Pre-request script

    適用於request header中包含時間戳或者在url引數中傳送一個隨機字串

  2. 設定環境變數

postman.setEnvironmentVariable('timestampHeader'
,new Date());
  1. 獲取環境變數
postman.getEnvironmentVariable('variableKey')
  1. 清除環境變數
postman.clearEnvironmentVariable('variableKey')
  1. 設定全域性變數
postman.setGlobalVariable("key", "value");
  1. 獲取全域性變數
postman.getGlobalVariable("key");
  1. 檢查response的body中是否包含字串
tests["Body matches string"] = responseBody.has("string_you_want_to_search"
);
  1. 把XML的body轉換成JSON物件
var jsonObject = xml2Json(responseBody);
  1. 檢查response的body是都為一個字串
tests["Body is correct"] = responseBody === "response_body_string";
  1. 檢查JSON的值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
  1. 內容型別存在

    • 檢查不區分大小寫
tests["Content-Type is present"
] = postman.getResponseHeader("Content-Type"); //返回的是這個請求頭的值
  • 區分大小寫
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
  1. response的響應時間小於200ms:
tests["Response time is less than 200ms"] = responseTime < 200;
  1. 狀態碼為200:
tests["Status code is 200"] = responseCode.code === 200;
  1. Code name contains a string:
tests["Status code name has string"] = responseCode.name.has("Created");
  1. 成功的POST request狀態碼
tests[Successful POST request] = responseCode.code === 201 || responseCode.code === 202;
  1. Use TinyValidator for JSON data
var schema = {
 "items":{
  "type": "boolean"
 }
};
var data1 = [true, false];
var data2 = [true, 123];

console.log(tv4.error);
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
  1. Tests常用函式
assertNotTimeout: var hasResponse = postman.getResponseHeader('Content-Type') ? true : false;
if (!hasResponse) tests['服務端在超時前沒返回任何資料,請檢查相關服務、網路或反向代理設定(以下跳過其他斷言)'] = false;
logParams: if (hasResponse) tests[` [INFO]請求引數(僅限POST,超時沒返回時不解析):$ {
    JSON.stringify(request.data)
}`] = true;
getResponseJson: try {
    if (hasResponse) var json = JSON.parse(responseBody);
} catch (err) {
    tests['服務端沒返回合法的JSON格式,請檢查相關服務、網路或反向代理設定(以下跳過其他斷言)'] = false;
    tests[` [INFO]返回:$ {
        responseBody
    }`] = true;
    console.error(err);
}
assertType: var assertType = (name, value, type) = > {
    let isType = (type === 'array') ? Array.isArray(value) : typeof value === type;
    tests[`$ {
        name
    }為$ {
        type
    }(實際值:$ {
        value
    })`] = isType;
};
assertEqual: var assertEqual = (name, actual, expected) = > {
    tests[`$ {
        name
    }等於$ {
        expected
    }(實際值:$ {
        actual
    })`] = actual === expected;
};
assertNotEqual: var assertNotEqual = (name, actual, expected) = > {
    tests[`$ {
        name
    }不等於$ {
        expected
    }(實際值:$ {
        actual
    })`] = actual !== expected;
};
  1. Console除錯:

console.log();如何通過chrome的除錯模式檢視console的輸出內容;在Chrome瀏覽器中輸入chrome://flags/
搜尋packed,找到如下擴充套件,點選“啟用”