如何在POSTMAN中的2个请求之间添加暂停

时间:2016-11-08 02:19:13

标签: http testing postman

我在POSTMAN中有一组请求。我想在两个请求之间添加暂停,但我无法通过阅读他们的文档找到这样做的方法。有什么想法吗?

UPDATE 我只想在一个请求之后暂停,而不是在集合中的每个请求之后暂停。

9 个答案:

答案 0 :(得分:11)

万一有人还在寻找它-您可以在集合中的多个测试之一之后/之前添加延迟,可以使用:

setTimeout(function(){}, [number]);

其中“数字”是毫秒。如果将其添加到“测试”,它将在发送请求后执行。如果是在“请求前测试”中添加的,它将在发送请求之前执行。

答案 1 :(得分:7)

我知道有两种方法可以做到这一点

方法I

将您的请求作为集合运行。 (https://www.getpostman.com/docs/collections) 使用Newman(Postman的命令行中的收集程序)使用--delay标志运行您的集合。延迟输入值以毫秒为单位。

方法II

这是一个很好的黑客,我在这里https://github.com/postmanlabs/postman-app-support/issues/1038找到了。您可以在Postman中为测试脚本添加延迟功能。

答案 2 :(得分:3)

使用javascript忙碌的等待是一个很好的黑客,但它会使你的CPU热,应用程序无响应。我使用 postman-echo 找出了这个解决方案。

假设您要在Request_A和Request_B之间添加长延迟。

首先,在Request_A的测试脚本中设置一个env var来标记开始。

environment.delayTimerStart = new Date();

然后,在创建中创建一个GET请求(此处称为“Delay 10s”)。它在https://postman-echo.com/delay/10上进行GET(它在10秒后返回)

在其测试脚本中,添加

var curDate = new Date();
if (curDate - environment.delayTimerStart < delay_time_in_sec*1000) {
    postman.setNextRequest('Delay 10s');
} else {
    postman.setNextRequest("Request_B");
}

通过这种方式,您可以添加任意长度的延迟。

注意:10秒是postman-echo接受的最大值。如果您只需要短暂的延迟,只需获取https://postman-echo.com/delay/[1~10]

答案 3 :(得分:2)

只是一个简单的示例,我相信您会理解的。

setTimeout(() => {}, 15000); 

15000,以毫秒为单位的值

答案 4 :(得分:1)

我更喜欢使用在线服务Postman Echo的延迟端点(文档为here)。只需创建一个使用此服务的请求,并在您希望在其间添加延迟的其他两个请求之间调用它。

如果要在继续之前检查某些内容的状态,可以在请求的postman.setNextRequest()中使用Tests进行循环,直到某些内容完成为止。只需执行以下操作:

1)创建一个结构为:

的集合
  • 延迟10秒
  • 状态检查
  • 继续处理

2)在Status Check请求测试中:

if(responseBody.has("Success")) //or any other success condition
{
    postman.setNextRequest('Continue Processing');
    tests["Success found"] = true;
}
else
{
    postman.setNextRequest('Delay For 10 Seconds');
    tests["No success found"] = true;
}

答案 5 :(得分:1)

如果您拥有独立的Postman App(支持ES7),并且打算仅在Postman上进行测试,而不是在newman(不支持ES7)上进行测试,则可以在Pre-Request脚本中使用类似的内容您要延迟的请求:

function foo() {
    return (new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("done!");   // passing argument is optional, can just use resolve()
        }, 10000)   // specify the delay time in ms here..
    }))
}

async function waitForMe() {
    await foo().then((val) => {
        console.log(val);  // not required, you can just do an await without then
    })
}

waitForMe();

答案 6 :(得分:1)

尝试这样的事情-

N

答案 7 :(得分:0)

如果您正在使用Postman Runner

,请查看当前文档
  

<强>延迟

     

这是集合运行中每个请求之间的间隔(以毫秒为单位)。

https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run

如果你正在使用纽曼

--delay-request [number] Specify a delay (in ms) between requests [number]

https://www.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman

答案 8 :(得分:0)

您可以使用JavaScript setTimeout方法。这将确保您的方法将在给定的延迟后执行。

enter image description here

  setTimeout(checkStatusCode, 500);

    function checkStatusCode() {

        pm.sendRequest('https://postman-echo.com/get', function (err, res) {

            tests['status code should be 200']= res.code ===200;

        });
    }