【问题标题】:How to display the sum of values ​of this response body no Postman如何显示此响应正文的值总和 no Postman
【发布时间】:2021-08-06 10:14:48
【问题描述】:

我需要一个显示值字段总和结果的测试 对于下面的响应正文并将其显示在邮递员的控制台上, 响应体中的值的数量是动态的:

以下数值之和的结果: "value": 108.45 "value": 147.28 “价值”:87.70 “价值”:20.00 “价值”:75.40 “价值”:87.70 “价值”:147.28 “价值”:87.70**

 {
    "detalhes": {
        "respostaDetalhe": [
            {
                "origem": "PAY",
                "path": "BITO",
                "nivel": 1,
                "chaveFonte": "VendaCanceladaDebito",
                "status": {
                    "codigo": "S00001",
                    "mensagem": "Operação realizada com sucesso",
                    "origem": "PAY",
                    "detalhes": {
                        "detalhe": []
                    }
                },
                "valuees": {
                    "detalhe": [
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8211184686-8241102617"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 108.45
                                    }
                                ]
                            }
                        },
                        
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8211184688-8241102917"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 147.28
                                    }
                                ]
                            }
                        },
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8881016351-9041105200"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 87.70
                                    }
                                ]
                            }
                        },
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8881016193-9041103402"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 20.00
                                    }
                                ]
                            }
                        },
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8881015351-9041096206"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 75.40
                                    }
                                ]
                            }
                        },
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8881015000-9041093300"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 87.70
                                    }
                                ]
                            }
                        },
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8211184688-8241102919"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 147.28
                                    }
                                ]
                            }
                        },
                        {
                            "niveisAnteriores": {
                                "nivel": []
                            },
                            "conteudos": {
                                "conteudo": [
                                    {
                                        "identificadores": {
                                            "identificador": [
                                                {
                                                    "nome": "CUPOM",
                                                    "x": "8881015351-9041096207"
                                                },
                                                {
                                                    "nome": "CHAVE_LINHA",
                                                    "x": "TOTAL_VENDA_CANC_DEBITO"
                                                }
                                            ]
                                        },
                                        "value": 87.70
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
}

【问题讨论】:

  • 到目前为止您尝试过什么?什么不起作用?
  • 我试图识别第一个值字段但没有成功,下面的代码返回 undefined: const response = pm.response.json(); const respostadetalhe = response.detalhes["respostaDetalhe"]; const nivel=respostadetalhe["1"]; console.log(nivel);

标签: json testing postman


【解决方案1】:

可能有很多更有效的方法,但你可以使用类似这样的 hacky 方法:

let detalheArray = _.get(pm.response.json(), 'detalhes.respostaDetalhe.0.valuees.detalhe'),
    valuesToSum = [];

_.each(detalheArray, (item) => {
    valuesToSum.push(item.conteudos.conteudo[0].value)
})
console.log(valuesToSum)
console.log(_.sum(valuesToSum))

就像我说的,这不是最好的解决方案,但应该获取值并将它们加在一起。

【讨论】:

  • Danny Dainton,非常感谢您的帮助,效果很好。
猜你喜欢
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 2021-04-18
  • 2017-07-05
  • 1970-01-01
  • 2021-04-22
相关资源
最近更新 更多