【问题标题】:Visualize multi-level response object as Table in Postman在 Postman 中将多级响应对象可视化为表
【发布时间】:2020-07-28 03:12:16
【问题描述】:

我想将 Postman 测试中的以下数据可视化为表格,其中 productpricequantity 在列中,Items 在行中。可能有多个 shippingGroups。

{
   ...
   "companyGroups": [
        {
            ...
            "shippingGroups": [
                {
                    "id": 1,
                    "items": [
                        {
                            "product": "Product A",
                            "price": 2,
                            "quantity": 1,
                        },
                          {
                            "product": "Product B",
                            "price": 4,
                            "quantity": 4,
                        }

                    ],
                    ...
            ]
        }
    ],

我在使用 {{#each response???}} 引用多个级别对象中的项目时遇到问题。预期的格式应该是这样的:

   <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>

        {{#each response???}}
            <tr>
                <td>{{???product}}</td>
                <td>{{???price}}</td>
                <td>{{???quantity}}
            </tr>
        {{/each}}
    </table>

有关邮递员表可视化响应here 的更多信息

【问题讨论】:

    标签: javascript html json postman postman-testcase


    【解决方案1】:

    鉴于您的响应示例,您可以使用如下内容:

    const template = `
       <table>
            <tr>
                <th>Product</th>
                <th>Price</th>
                <th>Quantity</th>
            </tr>
    
            {{#each responseData}}
            {{#each items}}
                <tr>
                    <td>{{product}}</td>
                    <td>{{price}}</td>
                    <td>{{quantity}}
                </tr>
            {{/each}}
            {{/each}}
        </table>
    `;
    
    let responseData = []
    
    _.each(pm.response.json().companyGroups, (item) => {
        _.each(item.shippingGroups, (nestedItem) => {
            responseData.push(nestedItem)
        })
    })
    
    pm.visualizer.set(template, { responseData })
    

    这只是一个粗略的示例,需要重构,但它表明您可以在表格中显示响应数据。

    【讨论】:

    • 您好,想引起您对我刚刚发布的邮递员问题的注意,如果我强加了,对不起。问候。 stackoverflow.com/questions/63263561/…
    • 我尝试使用您的方法进行重构,但没有成功。我特别选择了我的示例,因为它返回一个非常简单的结构,不会重复,因此不需要循环。您的解决方案更适合重复数据集。
    • 你在说这个吗? stackoverflow.com/a/63264138/6028443,如果是这样,让我们​​在这种情况下讨论这个问题:D
    • 丹尼,我用大写字母道歉。我很困惑,因为我工作了很多。谢谢,我接受了你对我的问题的回答。 :)
    猜你喜欢
    • 2020-02-15
    • 2022-10-15
    • 2019-01-07
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多