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

Multi tool use
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
karate
add a comment |
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
karate
add a comment |
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
karate
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
karate
karate
asked Dec 27 '18 at 16:42
sam
827
827
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
1
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
1
@sam read the docs forcall
- only takes one arg. since JS you can do this directly:* def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948202%2fkarate-api-in-attached-json-response-how-to-update-json-attribute-quantity-dyn%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
1
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
1
@sam read the docs forcall
- only takes one arg. since JS you can do this directly:* def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
add a comment |
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
1
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
1
@sam read the docs forcall
- only takes one arg. since JS you can do this directly:* def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
add a comment |
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
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
answered Dec 27 '18 at 17:31


Peter Thomas
12.4k31842
12.4k31842
1
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
1
@sam read the docs forcall
- only takes one arg. since JS you can do this directly:* def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
add a comment |
1
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
1
@sam read the docs forcall
- only takes one arg. since JS you can do this directly:* def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
1
1
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
Thank you for you quick response. Karate is simply superb. Just curios to know why my approach did not work for me and can we call a feature in doc string with params. ?
– sam
Dec 27 '18 at 19:17
1
1
@sam read the docs for
call
- only takes one arg. since JS you can do this directly: * def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
@sam read the docs for
call
- only takes one arg. since JS you can do this directly: * def itemPrice = updateAttempt(orderItemIDs,upConfig,firstOrderID)
– Peter Thomas
Dec 28 '18 at 0:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948202%2fkarate-api-in-attached-json-response-how-to-update-json-attribute-quantity-dyn%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7TzcpGwJ,JtGl FJ3QhyQhq,nwJIsRP e 2cE3j 9,I6 m