如何使用空手道遍历动态数组? [英] How to traverse a dynamic array using karate?

查看:74
本文介绍了如何使用空手道遍历动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在点击一个api-> http://127.0.0.1:5000/api/library/?book_id=2,5,13 api的响应为:

I am hitting an api -> http://127.0.0.1:5000/api/library/?book_id=2,5,13 The response of the api is:

{
    "data": {
        "2":{
                "rack": 219,
                "price": 360,
                "title": "book1"
            },
        "5":{
                "rack": 309,
                "price": 230,
                "title": "book2"
            },
        "13":{
                "rack": 112,
                "price": 200,
                "title": "book3"
            }
    },
    "status_code": 200
}

键2、5、13根据api参数是可变的.

the keys 2,5,13 are variable depending on the api param.

我写了以下代码=>

Feature: Verify Library API
    Scenario: Verify book prices 
        * def id1 = 2 
        * def id2 = 5 
        * def id3 = 13 
        Given url 'http://127.0.0.1:5000/api/library/?book_id='+ id1 +',' + id2 + ',' + id3 
        When method get 
        Then status 200 
        * def id1_price = $.data.#(id1).price 
        * assert id1_price == 360

我无法在变量中获取book1的价格值,这导致断言失败.如何使用变量"id1"访问价格?

I wasn't able to get the value of the price of book1 in the variable, which resulted in the assertion failure. How do I access the price using the variable "id1"?

推荐答案

当您具有动态JsonPath表达式时,请使用

When you have dynamic JsonPath expressions, use the karate.jsonPath() API. try this:

* def id1_price = karate.jsonPath(response, "$.data." + id1 + ".price")

这篇关于如何使用空手道遍历动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆