在postman右邊的snippets欄中,有postman內置的測試腳本,能夠輔助進行接口測試,下面就一一說明下每一個腳本的含義javascript
一、清除一個全局變量:Clear a global variable
java
對應腳本:postman.clearGlobalVariable("variable_key");
json
參數:須要清除的參數的key。
less
二、清除一個環境變量:Clear an environment variable
post
對應腳本:postman.clearEnvironmentVariable("variable_key");測試
參數:須要清除的環境變量的key。url
三、response包含內容:Response body:Contains string
spa
對應腳本:tests["Body matches string"] = responseBody.has("string_you_want_to_search");code
參數:所要包含內容。xml
四、將xml格式的response轉換成son格式:Response body:Convert XML body to a JSON Object
對應腳本:var jsonObject = xml2Json(responseBody);
參數:(默認不須要設置參數,爲接口的response)須要轉換的xml
。
五、response等於預期內容
:Response body:Is equal to a string
對應腳本:tests["Body is correct"] = responseBody === "response_body_string";
參數:預期response
。
六、json解析key的值進行校驗
:Response body:JSON value check;
對應腳本:tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args;
參數:test替換被測的值,args替換被測的key
。
七、檢查response的header信息是否有被測字段
:Response headers:Content-Type header check
對應腳本:tests["Content-Type is present"] = postman.getResponseHeader("Connection");;
參數:預期header字段
。
八、響應時間判斷:Response time is less than 200ms
對應腳本:tests["Response time is less than 200ms"] = responseTime < 200;
參數:響應時間。
九、設置全局變量:Set an global variable;
對應腳本:postman.setGlobalVariable("variable_key", "variable_value");;
參數:全局變量的鍵值對。
十、設置環境變量:Set an environment variable;
對應腳本:postman.setEnvironmentVariable("variable_key", "variable_value");;
參數:全局環境變量的鍵值對。
十一、判斷狀態碼:Status code:Code is 200;
對應腳本:tests["Status code is 200"] = responseCode.code === 200;
參數:狀態碼。
十二、檢查code name是否包含內容:Status code:Code name has string
對應腳本:tests["Status code name has string"] = responseCode.name.has("Created");
參數:預期code name包含字符串
。
1三、成功的post請求
:Status code:Successful POST request
對應腳本:tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
1四、微小驗證器:
Use Tiny Validator 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); 參數:能夠修改items裏面的鍵值對來對應驗證json的參數