天天看點

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

在實作接口自動測試的時候,會經常遇到接口參數依賴的問題,例如調取登入接口的時候,需要先擷取登入的key值,而每次請求傳回的key值又是不一樣的,那麼這種情況下,要實作接口的自動化,就要用到postman中設定環境變量這個功能了;

在postman中,可以利用tests将接口傳回的response設定為環境變量,供後續接口使用(類似參數化的概念)

擷取環境變量需要具體方法如下圖所示;

var jsonData =JSON.parse(responseBody);//擷取body中傳回的所有參數
postman.setEnvironmentVariable("appKey",jsonData.data.appKey);//把傳回參數中的keys設定為環境變量      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

 如此就能把登入所需的key設定為環境變量,供後續登入接口的調用了;

同理,擷取headers值更新環境變量的方法,如下圖;

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

postman常用方法集合:

1.設定環境變量

postman.setEnvironmentVariable("key", "value");
pm.environment.get("key", "value");//postman  5.0以上版本設定環境變量的方法      

2.設定全局變量

postman.setGlobalVariable("key", "value");
pm.globals.set("variable_key", "variable_value");//postman 5.0以上版本設定全局變量方法      

3.檢查response body中是否包含某個string

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});//5.0以上版本方法      

4.檢測JSON中的某個值是否等于預期的值

var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;      

JSON.parse()方法,把json字元串轉化為對象。parse()會進行json格式的檢查是一個安全的函數。 

如:檢查json中某個數組元素的個數(這裡檢測programs的長度)

var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;      

5.轉換XML body為JSON對象

var jsonObject = xml2Json(responseBody);

tests["Body is correct"] = responseBody === "response_body_string";      

6.檢查response body是否與某個string相等

7.測試response Headers中的某個元素是否存在(如:Content-Type)

//getResponseHeader()方法會傳回header的值,如果該值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); 

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");      

上面的方法,不區分大小寫。下面的方法,要區分大小寫。 

8.驗證Status code的值

tests["Status code is 200"] = responseCode.code === 200;

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});//5.0以上版本方法      

9.驗證Response time是否小于某個值

tests["Response time is less than 200ms"] = responseTime < 200;

//5.0以上版本方法
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});      

10.name是否包含某個值

tests["Status code name has string"] = responseCode.name.has("Created");

//5.0以上版本方法
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});      

11.POST 請求的狀态響應碼是否是某個值

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

//5.0以上版本方法
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});      

12.很小的JSON資料驗證器

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
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);      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

13.擷取request.data值:

var Json = JSON.parse(request.data);       
  • data {object}:

    this is a dictionary of form data for the request. (request.data["key"]=="value")

  • headers {object}:

    this is a dictionary of headers for the request (request.headers["key"]=="value")

  • method {string}:

    GET/POST/PUT etc.

  • url {string}:

    the url for the request.

假設requestBody中有"version":"1.0";這個值,如果想擷取到version的value值,代碼如下

var Json = JSON.parse(request.data); 
var version = Json["version"];      

14.JSON.parse()和JSON.stringify()

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
JSON.parse()【從一個字元串中解析出json對象】
JSON.stringify()【從一個對象中解析出字元串】

var data={name:'goatling'}

JSON.parse(data)
結果是: '{"name":"goatling"}'

JSON.stringify(data)
結果是:name:"goatling"      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

15.判斷字段值是否為空typeof()

var Jsondata = JSON.parse(responseBody);
if( typeof(Jsondata.data) != "undefined" )      

在pre-request和tests 中擷取變量的方法:

變量可以被使用在

pre-request

test script

中。因為這些部分是通過

JavaScript

來寫的

你可以以不同的方式初始化和檢索這些變量。可以在腳本中初始化變量,并将它們放在特定的範圍内

1.定義一個變量在腳本中 

在腳本中設定一個變量可以根據變量預定的範圍通過pm.environment.set("variable_key", "variable_value");方法或者pm.globals.set("variable_key", "variable_value");方法,這方法要求提供變量的

key

value

去設定變量。當你發送請求的時候,這腳本将會執行,值将會儲存在變量中,如下圖:

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

2.取一個預定義的變量

一旦一個變量被設定,你可以使用pm.environment.set("variable_key", "variable_value");;

;

或者pm.globals.set("variable_key", "variable_value");;

;

 根據适合的範圍去擷取變量值。這方法要求提供一個變量名作為參數去檢索儲存的值,如下圖:

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

3.設定一個變量在作用域中

可以通路環境變量在相應的環境模闆。全局變量可以廣泛的通路,不管選擇的作用域

結果: 

Never give up !

var jsonData =JSON.parse(responseBody);//擷取body中傳回的所有參數
postman.setEnvironmentVariable("appKey",jsonData.data.appKey);//把傳回參數中的keys設定為環境變量      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
postman.setEnvironmentVariable("key", "value");
pm.environment.get("key", "value");//postman  5.0以上版本設定環境變量的方法      
postman.setGlobalVariable("key", "value");
pm.globals.set("variable_key", "variable_value");//postman 5.0以上版本設定全局變量方法      
tests["Body matches string"] = responseBody.has("string_you_want_to_search");

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});//5.0以上版本方法      
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;      
var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;      
var jsonObject = xml2Json(responseBody);

tests["Body is correct"] = responseBody === "response_body_string";      
//getResponseHeader()方法會傳回header的值,如果該值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); 

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");      
tests["Status code is 200"] = responseCode.code === 200;

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});//5.0以上版本方法      
tests["Response time is less than 200ms"] = responseTime < 200;

//5.0以上版本方法
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});      
tests["Status code name has string"] = responseCode.name.has("Created");

//5.0以上版本方法
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});      
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

//5.0以上版本方法
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
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);      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
var Json = JSON.parse(request.data);       
var Json = JSON.parse(request.data); 
var version = Json["version"];      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
JSON.parse()【從一個字元串中解析出json對象】
JSON.stringify()【從一個對象中解析出字元串】

var data={name:'goatling'}

JSON.parse(data)
結果是: '{"name":"goatling"}'

JSON.stringify(data)
結果是:name:"goatling"      
postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉
var Jsondata = JSON.parse(responseBody);
if( typeof(Jsondata.data) != "undefined" )      

pre-request

test script

JavaScript

key

value

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉

;

;

postman接口自動化,環境變量的用法詳解(附postman常用的方法)轉