Postman+Newman 簡介和簡單使用(二)
內置腳本說明:
1. 清除一個全局變量 Clear a global variable
對應腳本: postman.clearGlobalVariable("variable_key");
參數:需要清除的變量的key
2.清除一個環境變量 Clear an environment variable
對應腳本: postman.clearEnvironmentVariable("variable_key");
參數:需要清除的環境變量的key
3.response包含內容 Response body:Contains string
對應腳本: tests["Body matches string"] =responseBody.has("string_you_want_to_search");
參數:預期內容
4.將xml格式的response轉換成son格式 Response body:Convert XML body to a JSON Object
對應腳本: var jsonObject = xml2Json(responseBody);
參數:(默認不需要設置參數,爲接口的response)需要轉換的xml
5.response等於預期內容 Response body:Is equal to a string
對應腳本: tests["Body is correct"] = responseBody === "response_body_string";
參數:預期response
6.json解析key的值進行校驗 Response body:JSON value check
對應腳本: tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args
參數:test替換被測的值,args替換被測的key
7.檢查response的header信息是否有被測字段 Response headers:Content-Type header check
對應腳本: tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
參數:預期header
8.響應時間判斷 Response time is less than 200ms
對應腳本: tests["Response time is less than 200ms"] = responseTime < 200;
參數:響應時間
9.設置全局變量 Set an global variable
對應腳本: postman.setGlobalVariable("variable_key", "variable_value");
參數:全局變量的鍵值
10.設置環境變量 Set an environment variable
對應腳本: postman.setEnvironmentVariable("variable_key", "variable_value");
參數:環境變量的鍵值
11.判斷狀態碼 Status code:Code is 200
對應腳本: tests["Status code is 200"] = responseCode.code != 400;
參數:狀態碼
12.檢查code name 是否包含內容 Status code:Code name has string
對應腳本: tests["Status code name has string"] = responseCode.name.has("Created");
參數:預期code name包含字符串
13.成功的post請求 Status code:Successful POST request
對應腳本: tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
14.微小驗證器 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的參數
使用Runner功能和外部數據
Postman 工具自帶了Runner功能,用於批量運行腳本。在運行時還可以使用外部的CSV或者json文件來指定數據。
例如現在新建瞭如下兩個外部數據,第一個保存爲.json文件,第二個保存爲.csv文件。
新建如下GET請求API,並放於單獨一個文件夾中管理。接口請求中{{host}}便是用來獲取上步新建的兩個文件夾中的數據,{{}}中的名字對應json文件的key值,對應csv文件中的第一行值。
點擊Runner按鈕,打開Runner界面:
在新打開的窗口中,選着你要剛新建的文件夾名,選擇你要運行的環境,運行的次數和在Data File中選擇剛新建的外部json或者csv文件,並選取文件類型,點擊Start Test,變開始逐條讀取外部文件中的數據,進行運行。
Postman Interceptor
Interceptor 可以直接從瀏覽器中獲取請求,並保存在Postman的History中。 這個插件可以大大縮短API配置的時間,同樣Interceptor還有一個功能可以讓Postman和Chrome瀏覽器共用Chrome的Cookies。
安裝 Interceptor
Interceptor 同樣是Chrome的一個插件,所以也可以從Chrome網上商店找到該插件: https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo/support?hl=cn ,直接點擊安裝。安裝完後會在Chrome 插件中找到下圖標識。
使用 Interceptor
開啓Interceptor插件,並設置你要抓取的網站請求
在Postman 上同樣打開Interceptor
這時在Chrome瀏覽器上訪問一些baidu相關域名就會自動被Postman抓取,並在Postman的History顯示
Newman
官方幫助文檔地址:https://www.npmjs.com/package/newman
Newman 安裝
嗯,它需要安裝,因爲它不是音樂播放器!Newman是爲Postman而生,專門用來運行Postman編寫好的腳本。Newman安裝步驟:
- 需要安裝nodejs,並配置好環境
- 打開控制檯,運行:npm install -g newman
- 校驗是否安裝成功,運行:newman --version
Newman 執行腳本
Newman在3版本後做了比較大的改動,但是運行命令越來越簡單如下:
newman run <collection-file-source> [options]
run 後面跟上要執行的json文件或者URL(json 和 URL 都由postman導出生成),再後面跟一些參數,例如環境變量,測試報告,接口請求超時時間等等。最後給兩個完整的例子做參考:
例子1,通過newman 運行postman導出的test1.json文件,並生成多種測試報告(json,junit的xml,html):
newman run c:\test1.json --reporters cli,html,json,junit --reporter-json-export jsonOut.json --reporter-junit-export xmlOut.xml--reporter-html-export htmlOut.html
例子2,運行https://www.getpostman.com/collections/cb0cea0af1467c8008fb(postman生成的)中的所有api,並使用env.json作爲環境變量和globals.json作爲全局變量,並使用外部data.csv作爲外部數據,最後設置了接口請求超時時間爲5S 。
newman run https://www.getpostman.com/collections/cb0cea0af1467c8008fb --environment env.json --iteration-data data.csv --globals globals.json --timeout-request 5000
Jenkins 結合
平時做接口自動化,避免不了最後通過Jenkins做構建。既然Newman提供了控制檯命令執行方式,那麼像通過Jenkins來構建也就容易多了。
步驟一:在Jenkins 機器上安裝Newman
步驟二:搭建Jenkins環境,並新建個自由風格的Job
步驟三:構建選擇Execute Windows batch command,並輸入newman 運行命令
步驟四:因爲上面命令中構建會生成junit的xml報告,所以可以在構建後用Publish JUnit test result report 插件來生成測試報告。