我使用了兩個版本的postman:
- Postman-linux-x64-7.1.1 這是目前(2019)最新版本。這個版本也有坑:
(因為系統崩潰重裝了,所以目前只有最新版本。本文截圖都是這個版本的截圖。)
- 沒有配套的chrome postman interceptor插件
- 界面collection run遇到了上傳的request就不運行了。例如,第4個request是上傳文件,那么它只運行3個cases。
- Postman-linux-x64-5.x.x 這是之前安裝的,配套的chrome postman interceptor版本0.2.24。但是,這個版本及以下,有2個坑:
- 不能保存上傳文件的路徑。
- 如果在postman中,request中有上傳很多個文件的,那么,postman會頻繁崩潰。
在postman的使用中,遇到一些問題,記錄如下:
1. 舊版本上傳文件解決辦法(新版本上這樣做也沒有問題,但假如文件不多的話,可以使用另一種方式解決見下面2):
- 在postman中,設置好占位
Content-Type:multipart/form-data; boundary=;
- 導出collection, 保存為json文件
- 使用python等腳本,將文件路徑填進去。就是替換下面json中的src的value
"request": { "url": "https://echo.getpostman.com/post", "method": "POST", "header": [], "body": { "mode": "formdata", "formdata": [ { "key": "file", "type": "file", "enabled": true, "src": "sample-file.txt" } ] }, "description": "Uploads a file as a form data field to `https://echo.getpostman.com/post` via a `POST` request." },
如果你的collection下面沒有文件夾,那么可以使用下面的腳本。當collection下面有文件夾時,導出的json文件層級結構會改變。
import os import json ''' 必須使用python3運行,否則會在讀寫文件時報錯。 在讀寫文件時,指定了編碼,是為了對中文的支持。 ''' _target_file = 'postman_export.json' _file_dict = {'上傳xxx參數表': 'inputData.xlsx', '上傳xxx參數表2': 'inputData2.xlsx', '批量上傳': '', } def main(filepath): upload_files = ["file1","file2"] with open(filepath, 'r', encoding='utf-8') as f: json_data = json.load(f) for item in json_data['item']: # print(item['name']) if '上傳' in item['name']: if _file_dict[item['name']]: item['request']['body']['formdata'][0]['src'] = _file_dict[item['name']] else: #批量上傳文件 item['request']['body']['formdata'] = [] for f in upload_files: upload_form = {"key": "files", "type": "file", "src":""} upload_form['src'] = f item['request']['body']['formdata'].append(upload_form) with open(filepath, 'w', encoding='utf-8') as f: json.dump(json_data, f, sort_keys=True, indent=4, ensure_ascii=False) if __name__ == '__main__': main(_target_file)
- 使用newman命令行運行
# newman 安裝:npm install -g newman newman run postman_export.json -e dev_env.json
detail see: https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/
如果使用的postman版本太老,比如說5,那么,導出的json文件是不能使用newman在命令行中運行的,層級結構也不一樣。
此時,把導出的json文件,在新版本的postman中,導入,然后再導出,就可以了。
2. 在新版本postman中保存上傳文件路徑的方法:
在postman的右上角,點擊設置圖標(如下圖),選擇settings,將紅框的選項設置為on。
其實,如果要上傳的文件在設置的Working Directory里,那么,這個選項是不用開啟的。但是,為了能訪問本地的任意路徑,還是打開這個選項為好。
現在,在上傳的request中,選擇的文件路徑,就會保存在postman中了。導出json文件中,也保存了該路徑。
3. postman中的變量。
postman中有global和environment兩種變量,設置后,在request url, request body中使用方式都一樣:{{variable}}
在tests和pre-tests的腳本中這樣使用: pm.environment.get("variable")
建議使用environment變量,因為可以導出,在命令行中也方便指定,方便管理方便切換。
4. postman不能不執行某個或某些request .
注意:postman中一個collection內是不能有同名request的,即使是建了文件夾,它也是在一個collection內的。
官方文檔上說,可以使用postman.setNextRequest('request name')構建workflow,但必須指定request名字,是不能不執行某個request之后的requests的。就是說,不能stop運行。
例如:當某個request失敗后,不想執行后面的request了,只能在最后增加一個隨便的查詢request,把名字指向它。
查找網上,說在tests腳本中可以throw error, 但是這個並不能阻止request的執行。
5. collection runs
- data file : 是json或者csv文件。官網給的例子是csv文件,每一行作為一次輸入,執行整個collection一次。
data file中的變量名,在url和request body中,使用方式:{{variable}}, 在tests腳本中,data.variable
detail see: https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files/
- iterations: 連續執行collection的次數(不換數據)
目前沒有用到,只是了解。
另外比較好奇,是否可以在上傳文件的path中使用{{variable}}。在界面上似乎不行,那么修改導出的collection.json文件呢?
6. 單獨調試某個request。
這個只能在postman界面中做。
在環境變量中設置好需要的數據,然后,選擇該環境,比如說dev。寫好request和tests腳本。打開左下角的console,就可以看到輸入和輸出了。
7. 可以使用swagger的url或者文件,直接導入request
我試過使用interceptor攔截到history然后直接保存到指定collection,試過使用swagger導入request,感覺都可以。相對來講,使用swagger更好些。
使用interceptor可以把url和body都填好,取個名字就可用。但是需要一個一個操作。當然,這個也不是必須,因為在瀏覽器中按F12在network中也都能看到,只是省略一個copy過程。
因為,swagger是把所有的request都列出來,這樣就直接生成了體系。但是需要修改url, body等。
使用swagger import, 可以直接輸入link 或者swagger的api-doc直接paste raw text:
8. tests 腳本
注意:在腳本中,沒找到打印request header,body的方法。
該腳本的意思是:當request返回200時,每60秒查詢該task的status, 當它的狀態改變(不為1)時,繼續后面的request。
tests["Status code is 200"] = responseCode.code === 200; var jsonData = JSON.parse(responseBody); if (tests["Status code is 200"]) { console.log('wait for submitting successfully...'); var status = 1; var timer pm.sendRequest(pm.environment.get("server")+"/ido/api/v1/tasks/"+pm.environment.get("taskId"), function (err, response) { status = response.json().body.status; console.log(status); }); timer = setInterval(() => { if (status !== 1) { console.log('stop'); clearInterval(timer); } pm.sendRequest(pm.environment.get("server")+"/ido/api/v1/tasks/"+pm.environment.get("taskId"), function (err, response) { status = response.json().body.status; console.log(status); }); }, 60000); console.log("1:運行中 2:成功 3:失敗"); } else { console.log(responseCode); console.log(jsonData); }
另外,解析response的json時,當返回的是數組時,它是不能使用for in遍歷的。使用for in 遍歷后的結果與預期不一樣,數組返回是[0:{},1:{}]這樣的。
使用for(var i=0; i<xx.length;i++)
tests["Status code is 200"] = responseCode.code === 200; var jsonData = JSON.parse(responseBody); if (tests["Status code is 200"]) { for(var i=0; i<jsonData.body.items.length; i++) { item = jsonData.body.items[i] if (item.status == "通過"){ console.log("projectId:"+item.projectId); console.log("projectName:"+item.projectName); pm.environment.set("projectId", item.projectId); break; } } } else { console.log(responseCode); console.log(jsonData); }
也可以使用:
var item = jsonData.body.items.filter((p) => { return p.status == "通過"; }); pm.environment.set("projectId", item[0].projectId);