Postman的使用之构建工作流

Postman是什么

Postman是一个有强大功能的Web API和HTTP请求的测试工具,它能够发送任何类型的HTTP请求(GET、POST、PUT、DELETE…),并且能附带任何数量的参数和Headers。不仅如此,它还提供测试数据和环境配置数据的导入导出。

发送一个请求

1、在URL输入栏中输入地址: postman-echo.com/get。

2、点击Send按钮,发送请求;接着我们就可以看到服务器返回的JSON响应报文。

提示:postman-echo.com/get是Postman官方提供的一个测试接口。

Collection

在Postman中,集合表示将请求进行分组、分模块管理;对含义相近、对功能相近的请求保存到一个集合中,方便后期的维护、管理和导出。

如何创建集合?

1、点击URL输入栏右侧的Save按钮;

2、在弹出的保存请求对话框中,按照提示输入对应的保存信息;

3、保存请求至一个已经存在的集合,或者创建一个新集合,将请求保存至这个创建的新集合。

Collection Runner

在这里插入图片描述

Collection/Folder: 这是我们要运行的集合或文件夹。

Environment: 选择运行集合中的请求时对应使用的环境。

Iterations: 配置我们的集合将被运行的次数。

Delay: 配置依次运行每个请求之间间隔时间,单位为毫秒。

Log Responses: 用于限制集合运行时的响应日志记录。

Data: 提供集合运行时需要的数据文件,比如一些测试数据等。

构建工作流

在使用“Collection Runner”的时候,集合中的请求执行顺序就是请求在Collection中的显示排列顺序。

在这里插入图片描述

那么在“Collection Runner”中如何去构建不同的执行顺序呢?

我们可以使用名为setNextRequest()的内置函数来覆盖以上行为。

setNextRequest()是一个带有一个参数的函数,参数是接下来要运行的请求的名称或ID。

在这里插入图片描述
注意:1、setNextRequest()仅适用于运行集合,而不是发送单个请求。

           2、setNextRequest()总是在当前脚本所有语句执行完成后才执行。

学习更多

附件:

1、setNextRequest test.json文件内容:

{
“variables”: [],
“info”: {
“name”: “Using setNextRequest-collection runner test”,
“_postman_id”: “558e6b17-3094-3faa-0429-198c621a1791”,
“description”: “”,
“schema”: “https://schema.getpostman.com/json/collection/v2.0.0/collection.json
},
“item”: [
{
“name”: “Request jd”,
“event”: [
{
“listen”: “test”,
“script”: {
“type”: “text/javascript”,
“exec”: [
“// Some code here”,
“”,
“postman.setNextRequest(‘Request pinduoduo’)”,
“”,
“// Some code here”
]
}
}
],
“request”: {
“url”: “http://wwww.jd.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request vip”,
“request”: {
“url”: “http://www.vip.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request taobao”,
“request”: {
“url”: “http://www.taobao.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request pinduoduo”,
“request”: {
“url”: “http://www.pinduoduo.com”,
“method”: “POST”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
}
]
}

2、nosetNextRequest.json文件

{
“variables”: [],
“info”: {
“name”: “noUsing setNextRequest-collection runner test”,
“_postman_id”: “558e6b17-3094-3faa-0429-198c621a1791”,
“description”: “”,
“schema”: “https://schema.getpostman.com/json/collection/v2.0.0/collection.json
},
“item”: [
{
“name”: “Request jd”,
// 无event
“request”: {
“url”: “http://wwww.jd.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request vip”,
“request”: {
“url”: “http://www.vip.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request taobao”,
“request”: {
“url”: “http://www.taobao.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request pinduoduo”,
“request”: {
“url”: “http://www.pinduoduo.com”,
“method”: “POST”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
}
]
}

3、resetNextRequest test.json文件

{
“variables”: [],
“info”: {
“name”: “reUsing setNextRequest-collection runner test”,
“_postman_id”: “558e6b17-3094-3faa-0429-198c621a1791”,
“description”: “”,
“schema”: “https://schema.getpostman.com/json/collection/v2.0.0/collection.json
},
“item”: [
{
“name”: “Request jd”,
“event”: [
{
“listen”: “test”,
“script”: {
“type”: “text/javascript”,
“exec”: [
“// Some code here”,
“”,
“postman.setNextRequest(‘Request pinduoduo’)”,
“”,
“// Some code here”
]
}
}
],
“request”: {
“url”: “http://wwww.jd.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request vip”,
“request”: {
“url”: “http://www.vip.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request taobao”,
“request”: {
“url”: “http://www.taobao.com”,
“method”: “GET”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
},
{
“name”: “Request pinduoduo”,
“event”: [
{
“listen”: “test”,
“script”: {
“type”: “text/javascript”,
“exec”: [
“// Some code here”,
“”,
“postman.setNextRequest(‘Request jd’)”,
“”,
“// Some code here”
]
}
}
],
“request”: {
“url”: “http://www.pinduoduo.com”,
“method”: “POST”,
“header”: [],
“body”: {},
“description”: “”
},
“response”: []
}
]
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值