sam
sam

Reputation: 289

Karate API : In attached json response how to update json attribute quantity dynamically

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"
            }
        }
    }
}
}

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

Upvotes: 1

Views: 1162

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Here you go:

* 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

Upvotes: 2

Related Questions