- Postman功能(https://www.getpostman.com/features)
- 主要用於模擬網絡請求包
- 快速創建請求
- 回放、管理請求
- 快速設置網絡代理
- 安裝
- 下載地址:https://www.getpostman.com/
- 直接安裝,成功后在chorme的應用程序中會多出一個Postman
- 頁面訪問請求:www.baidu.com
- 點擊應用中“Postman”,會彈出登陸頁面,完成登陸,進入Postman主頁面
- 在Get后面的輸入框中輸入:http://www.baidu.com,點擊send按鈕
- 請求參數修改
- 百度搜索內容參數化:https://www.baidu.com/s?wd=111111&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=6&rsv_sug1=5&rsv_sug2=0&inputT=1212&rsv_sug4=1212。針對請求中的wd:111111,進行修改
- 將此地址復制到Postman的地址欄中,點擊Params
- 將wd對應的值“111111”,修改為“222222”,點擊Send。請求的搜索內容變為“222222”的搜索結果
- 驗證接口請求
- 驗證返回的頁面中包括指定的字符串:頁面中包括“222222”
- 點擊地址欄下面的Tests頁卡,進入Tests腳本編寫頁面
- 點擊右側的“Response body:Contains string”,腳本編輯框中會顯示出驗證的具體腳本:tests["Body matches string"] = responseBody.has("string_you_want_to_search");
- 修改“string_you_want_to_search”為“222222”,完成驗證代碼的編寫
- 點擊Send按鈕,重新發送請求,並執行測試:Tests(1/2)
-
管理請求- 保存請求-添加“打開百度首頁請求”
- 設置請求方式為Get,地址為www.baidu.com.點擊右側保存按鈕
- 在彈出的保存請求中設置保存的內容。Collections:百度,Request name:打開百度首頁。點擊“Add to Collections”
- 此請求會被保存到Collections頁卡內
- 后期在使用時,僅需要在此Collections中找到對應的請求名,即可直接使用請求
- 多請求維護-模塊管理Folder
- 點擊“百度”后面的"...",選擇"Add Folder"
- 在彈出框中,Folder name輸入“01首頁”,保存。拖動請求“打開百度首頁”至“01首頁”文件夾中
- Collections(系統:百度)-Folder(模塊首頁)-Request(請求:打開百度首頁),三級的關系已經建立。可用此方法來做對應的擴展
- 保存請求-添加“打開百度首頁請求”
- 環境變量
- 可直接通過切換環境來實現多個環境中的參數切換。常用功能:環境地址切換、全局變量使用
- 環境請求地址切換
-
- 在"百度"下,新增”Cnblos隨筆“文件夾
-
開發環境地址 http://www.cnblogs.com/s380774061/p/4814756.html 測試環境地址 http://www.cnblogs.com1/s380774061/p/4814756.html
- 選擇“Manage Environments--Add”,在頁面中設置環境信息:Environment(開發)->key(URL)->Value(http://www.cnblogs.com),選擇Submit
- 再添加測試環境:Environment(測試)->key(URL)->Value(http://www.cnblogs.com1)
- 在“Cnblogs隨筆”文件夾內,添加請求“打開個人中心頁面”:http://www.cnblogs.com/s380774061/p/4814756.html,修改“http://cnblogs.com/”為"{{URL}}"
- 實操
- 選擇Environment中的“測試”,點擊“send”發送請求。即執行測試環境的請求
- 選擇Environment中的“開發”,點擊“send”發送請求。即執行開發環境的請求
-
- 全局變量使用
- 選擇“Manage Environments--Globals”
- 在頁面中輸入key(user):value(s380774061),點擊Submit
- 修改“打開個人中心頁面”中請求地址內"s380774061"為"{{user}}"
- 在測試“開發”、“測試”環境時,僅需要切換環境即可完成對此user的不同環境的測試。user可隨意替換成別的user名
- 環境請求地址切換
- 可直接通過切換環境來實現多個環境中的參數切換。常用功能:環境地址切換、全局變量使用
- 執行測試-
- 請求:http://www.cnblogs.com/s380774061/p/4814756.html,對此頁面的返回狀態碼為200進行驗證(具體操作,可查Postman-簡單使用)
- 選擇Postman主頁中Runner,會彈出運行測試的界面
- 選擇"Cnblogs隨筆"文件夾-"開發"環境
- 點擊"Start Test Run",查看運行測試結果
- 分享請求
- Collection
- Share鏈接:點擊Collection的'...'內的Share
- Share文件:點擊Collection的"Download",會下載一個名為"*.json.postman_collection".(此文件可直接導入到Postman中)
- Environment
- 每個Environment中有對應的Download,會下載一個名為“*.postman_environment”.(此文件可導入Postman中)
- Collection
Newman- 官方說明:Postman's command-line companion lets you do amazing things! With Newman, you can integrate Postman collections with your build system. Or you can run automated tests for your API through a cron job.
- 概述:集成Postman的API測試命令行執行工具
- 首頁:https://www.npmjs.com/package/newman
- 基於Node.js,可安裝在Linux/Windows/Mac
- 安裝命令:
npm install -g newman
- 驗證安裝是否成功
-
newman -V
出現版本號,則安裝成功
- 運行方式,執行Postman導出的Collection文件
-
newman -c *.json.postman_collection
- Newman命令行操作
- 執行指定的Collection文件
-
newman -c *.json.postman_collection
- 執行指點定的Collection文件及Environment文件
-
newman -c *.json.postman_collection -e *.postman_environment
- 執行保存到服務器上的Collection
-
newman -u https://www.getpostman.com/collections/cb208e7e64056f5294e5
- 循環執行Collection文件5次
-
newman -u https://www.getpostman.com/collections/cb208e7e64056f5294e5 -n 5
- 詳細的newman命令用法
-
Usage: newman [options] Options: -h, --help output usage information -V, --version output the version number -c, --collection [file] Specify a Postman collection as a JSON [file] -u, --url [url] Specify a Postman collection as a [url] -f, --folder [folder-name] Run a single folder from a collection. To be used with -c or -u -e, --environment [file] Specify a Postman environment as a JSON [file] -E, --exportEnvironment [file] Specify an output file to dump the Postman environment before exiting [file] -d, --data [file] Specify a data file to use either json or csv -g, --global [file] Specify a Postman globals file [file] -G, --exportGlobals [file] Specify an output file to dump Globals before exiting [file] -y, --delay [number] Specify a delay (in ms) between requests -r, --requestTimeout [number] Specify a request timeout (in ms) for requests -R, --avoidRedirects Prevents Newman from automatically following redirects -s, --stopOnError Stops the runner with code=1 when a test case fails -j, --noSummary Doesn't show the summary for each iteration -n, --number [number] Define the number of iterations to run -C, --noColor Disable colored output -k, --insecure Disable strict ssl -l, --tls Use TLSv1 -N, --encoding [encoding-type] Specify an encoding for the response. Supported values are ascii,utf8,utf16le,ucs2,base64,binary,hex -x, --exitCode Continue running tests even after a failure, but exit with code=1. Incompatible with --stopOnError -o, --outputFile [file] Path to file where output should be written [file] -O, --outputFileVerbose [file] Path to file where full request and responses should be logged [file] -t, --testReportFile [file] Path to file where results should be written as JUnit XML [file] -i, --import [file] Import a Postman backup file, and save collections, environments, and globals [file] (Incompatible with any option except pretty) -p, --pretty Enable pretty-print while saving imported collections, environments, and globals -H, --html [file] Export a HTML report to a specified file [file] -W, --whiteScreen Black text for white screen -L, --recurseLimit [limit] Do not run recursive resolution more than [limit] times. Default = 10. Using 0 will prevent any varialbe resolution Newman is a command-line collection runner for Postman. You must specify a collection file or a collection URL to run newman A collection file or URL is mandatory Examples: newman -c POSTMAN_COLLECTION newman -u POSTMAN_COLLECTION -e POSTMAN_ENVIRONMENT newman -c POSTMAN_COLLECTION -f SAMPLE_FOLDER For more documentation, issues and examples head over to https://github.com/postmanlabs/newman
- Jenkins集成
- 既然Newman支持命令行運行,那么集成Jenkins就很簡單。在Jenkins的Job中添加command line,內容如下:
-
newman -c *.json.postman_collection --exitCode 1
-