空手道API:在附加的json响应中如何动态更新json属性数量 [英] Karate API : In attached json response how to update json attribute quantity dynamically

查看:35
本文介绍了空手道API:在附加的json响应中如何动态更新json属性数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的json响应中,我想更新所有productNumbers的数量,响应中的项目数可能有所不同,可能为1或大于1,这取决于输入.我该如何在空手道中做到这一点.我尝试了无法使用的方法,因此请提供解决方案.(我在下面提供了我的方法,如果是错误的方法,请忽略)

In below json response I want to update quantity of all productNumbers, Item count in response varies it could be 1 or more than 1, it depends on the input. how can I do that in Karate. I tried my way it did not work, so please provide a solution.(I provided my approach below please ignore if it is wrong approach)

  {
"userProfileId": "12313123123",
"items": {
    "47961": {
        "products": {
            "productNumber": "0000",
            "productSummary": {
                "productSubTotal": "$68.64",
                "quantity": 3,
                "productrice": "$22.88"
            }
        }
    },
    "47962": {
        "products": {
            "productNumber": "12345",
            "productSummary": {
                "productSubTotal": "$68.64",
                "quantity": 3,
                "productPrice": "$22.88"
            }
        }
    },
    "47963": {
        "products": {
            "productNumber": "1111",
            "productSummary": {
                "productSubTotal": "$68.64",
                "quantity": 3,
                "productPrice": "$22.88"
            }
        }
    },
    "47964": {
        "products": {
            "productNumber": "2222",
            "productSummary": {
                "productSubTotal": "$68.64",
                "quantity": 3,
                "productPrice": "$22.88"
            }
        }
    }
}
}

我通过创建JS文件并向其传递所需的值来进行如下尝试,但是当我尝试使用Java脚本调用功能文件时失败(可能是我调用的方式不正确)

I tried with like below by creating JS file and passing required values to it but it failing when I trying to call a feature file withing java script.(may be the way i am calling is incorrect)

Feature: Update
Scenario: Update all items in cart
* print 'config in called function '+upConfig
* print 'in called function '+orderItemIDs
* def updateAttempt =
"""
  function(productNumbers,upConfig,firstOrderID){
    for(i=0;i<orderItemIDs.length;i++){
      karate.log('Run test round: '+(i+1));
      var itemID = productNumbers[i];
      karate.log('Order Item IDs :'+productNumbers[i]);
      karate.log('Config log-'+upConfig);
       karate.log('firstOrderItemID-'+firstOrderID);
       karate.call('UpdateProductQuantity.feature') upConfig;
      }
    java.lang.Thread.sleep(1*1000);
  }
"""
* def itemPrice = call updateAttempt(orderItemIDs,upConfig,firstOrderID)


   Feature: test update

 Scenario Outline: Update with all values
* def encodedURL = ''
* def gID = ''
* def upConfig = ''
* def firstOrderItemID = [47961]
* json productNumbers= orderItemIDs
* print 'productNumbers--'+orderItemIDs
* def list = call read('Update.feature') upConfig
* def result = call list productNumbers
* def result = call result firstOrderItemID
* print 'Result -'+result.response

推荐答案

在这里:

* def response = 
"""
{
   "userProfileId":"12313123123",
   "items": {
      "47961": {
         "products": {
            "productNumber":"0000",
            "productSummary": {
               "productSubTotal":"$68.64",
               "quantity":3,
               "productPrice":"$22.88"
            }
         }
      },
      "47962": {
         "products": {
            "productNumber":"12345",
            "productSummary": {
               "productSubTotal":"$68.64",
               "quantity":3,
               "productPrice":"$22.88"
            }
         }
      }
   }
}
"""
* def fun = function(k, v){ response.items[k].products.productSummary.quantity = 100 }
* eval karate.forEach(response.items, fun)
* match each response..quantity == 100

这篇关于空手道API:在附加的json响应中如何动态更新json属性数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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