【问题标题】:how to get whole html or json repsonse of an URL using Newman API如何使用 Newman API 获取 URL 的整个 html 或 json 响应
【发布时间】:2017-07-21 12:01:16
【问题描述】:

每当我从命令行运行以下命令时

newman run https://www.getpostman.com/collections/abcd1234

我得到显示失败和执行统计信息的输出。

但我正在寻找来自 URL 的完整 HTML 或 JSON 响应,以便在执行上述 Newman 查询后打印在终端上。我该如何实现这一点?

【问题讨论】:

    标签: api postman newman


    【解决方案1】:

    我正在使用 newman 进行 Web 服务和微服务测试。这对我来说很好。

    summary.run.executions[0].response.text().toString()
    

    完成事件后,您应该能够得到响应。

    d 是从 Postman 导出的集合。

    newman.run({
                collection: d,
                // reporters: 'cli',
                iterationCount: 1,
                timeoutRequest: 10000,
                timeoutScript: 5000,
                delayRequest: 0,
                insecure: false, 
            }).on('done', (err, summary) => {
                if (err || summary.error) {
                    console.error('\ncollection run encountered an error.');
                    reject(summary.error);
                }
                else {
                    var xml = summary.run.executions[0].response.text().toString();
                    console.log(xml)
                }
            })
        })
    

    【讨论】:

      【解决方案2】:

      您需要使用 Postman API。

      所以你需要运行这样的东西

      newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey
      

      (见http://blog.getpostman.com/2018/06/21/newman-run-and-test-your-collections-from-the-command-line/) 您可以在 Postman Cloud 中获取 ApiKey。您需要进入工作区 -> 集成 -> 浏览集成 -> Postman API 查看详细信息 -> 详细获取 API 密钥/现有 API 密钥

      如果您还需要添加环境(如果您使用变量),您需要使用 -e 参数 'newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey -e dev_environment.json' 运行相同的命令

      但是,如果您的环境也在云中呢?根据此文档https://www.getpostman.com/docs/v6/postman/collection_runs/command_line_integration_with_newman,您可以将 URL 作为值传递。所以你可以运行这样的东西

      newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey -e environments/{{environment_uid}}?apikey=myPostmanApiKey
      

      它对我有用,希望这会有所帮助

      【讨论】:

        【解决方案3】:

        您必须在请求中添加一些日志输出。

        对于您希望查看响应输出的请求,在 Postman 测试选项卡中添加以下内容:

        console.log(responseBody); // full response body
        

        如果要记录特定部分,则必须将响应正文解析为 JSON 对象:

        let response = JSON.parse(responseBody);
        console.log(reponse.myprop); // part of the full response body
        

        现在,如果您使用 newman 运行此集合,CLI 报告器也会打印控制台日志部分。

        【讨论】:

        • 你好,我收到 ReferenceError: responseBody is not defined 是因为我在本地加载集合,而不是使用 url?
        猜你喜欢
        • 2020-01-24
        • 2021-10-28
        • 1970-01-01
        • 2019-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-03
        • 2018-10-25
        相关资源
        最近更新 更多