当前位置:网站首页>Postman斷言對應脚本的解釋
Postman斷言對應脚本的解釋
2022-06-26 01:38:00 【NitefullSand】
轉自:http://www.51testing.com/html/01/n-3724901.html
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的參數
舉例說明
發送一個get請求
Postman安裝完成後,我們來用它向百度發送一個搜索請求。比如搜“Postman”吧。
我們先在百度搜索框輸入“Postman”,點擊“百度一下”,然後將瀏覽器地址欄的內容複制到Postman的請求地址欄,點擊Send。這樣,我們就向百度首頁發送了一個搜索請求,這個請求是GET請求,如下圖所示。從圖中,我們可以看到本次請求的狀態碼Status是200,錶示此次請求發送成功。本次的請求響應時間是321ms,另外還可以響應的HTML文檔。
修改請求的參數
在上圖中點擊Params,Postman將會把url中的所有參數解析成一個一個的key-vaule對,如下圖所示。其中wd這個key對應的value是postman。我們將其改成“Chrome”,再次點擊Send。請求的結果將變成搜索“Chrome”的頁面HTML。
驗證請求結果
驗證返回的頁面中包括指定的字符串:頁面中包括“Chrome”。
點擊地址欄下面的Tests選項卡,進入Tests脚本編寫頁面。點擊“Response body: Contains string”,將“string_you_want_to_search”替換成“Chrome”。點擊Send發送請求,執行測試。在下方Response區域的Test選項卡裏,可以看到Pass “Body matches string”,錶示該請求的響應體重包含“Chrome”字符串,測試通過。
不通過測試顯示如下
边栏推荐
- CityJSON
- 2022 documenter general basic (documenter) exam simulation 100 questions and online simulation exam
- 2022 Anhui province safety officer C certificate examination practice questions simulated examination platform operation
- Oracle数据库开启备份准备工作
- RT thread project engineering construction and configuration - (Env kconfig)
- Complete review (including syntax) -- MySQL regular expressions
- Simple making of master seal
- Maze walking
- JQ获取对象的几种方式
- 《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
猜你喜欢

Etcd database source code analysis cluster communication initialization

浅谈接口测试(二)

从在浏览器的输入框输入一个网址,到看到网页的内容,这个过程中发生了什么?

Simple making of master seal

《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)

Cross validation -- a story that cannot be explained clearly

Camera - 02 image sensor

I2C protocol

100ask seven day IOT training camp learning notes - bare metal program framework design

Postman断言对应脚本的解释
随机推荐
Cross validation -- a story that cannot be explained clearly
Native DOM vs. virtual DOM
Can bus transceiver principle
JSON实例(一)
网上开通证券账户安全吗 证券网上开户
Accumulation and summary of activation function
From query database performance optimization to redis cache - talk about cache penetration, avalanche and breakdown
JSON introduction
【Visual Studio Code】vscode快捷键大全
Embedded c learning notes
A sharp tool for information collection, Google hacker syntax
"Hot post" Statistics
Model integration and cascading
Summary of informer's paper
Using redis database as cache in Django
远程增量同步神器rsync
--都市修炼手册之SQL-- 第一章 基础复习
--SQL of urban cultivation manual -- Chapter 1 basic review
2022 Anhui province safety officer C certificate examination practice questions simulated examination platform operation
Set set!! Review quickly -- MySQL addition, deletion, modification and query, internal, left and right connection review notes



