Karate - Select a random element from json - karate

I need to select any random value from the below sample json every time i run the code. How can i achieve this in Karate? I need to get any random value and use in another feature file.
def myJson =
"""
{
"sampleJson": {
"random1": "1",
"random2": "2",
"random3": "3",
"random4": "4",
"random5": "5"
}
}
"""

Also refer: https://github.com/intuit/karate#commonly-needed-utilities
* def random = function(max){ return Math.floor(Math.random() * max) + 1 }
* def myJson =
"""
{
"sampleJson": {
"random1": "1",
"random2": "2",
"random3": "3",
"random4": "4",
"random5": "5"
}
}
"""
* def key = 'random' + random(5)
* def result = myJson.sampleJson[key]
* print result
Also see: https://stackoverflow.com/a/70376593/143475

Related

Karate - How change key name in JSON

I have the following JSON. I want to change the keyName 'freeDelivery' to 'isFreeDelivery' but I can't figure out how to do it.
{
"result": [
{
"deliverySlots": [
{
"id": "2DNN",
"date": "2022-04-05",
"freeDelivery": false,
"label": "All day delivery 08:30am to 5pm",
"price": "£5.00",
"fullSlotId": "2DNN"
},
{
"id": "2DPM",
"date": "2022-04-05",
"freeDelivery": false,
"label": "Afternoon 12pm to 5pm",
"price": "£10.00",
"fullSlotId": "2DPM"
}
]
},
{
"deliverySlots": [
{
"id": "2DNN",
"date": "2022-04-06",
"freeDelivery": false,
"label": "All day delivery 08:30am to 5pm",
"price": "£5.00",
"fullSlotId": "2DNN"
},
{
"id": "2DPM",
"date": "2022-04-06",
"freeDelivery": false,
"label": "Afternoon 12pm to 5pm",
"price": "£10.00",
"fullSlotId": "2DPM"
}
]
}
]
}
I've looked at the following pages but still can't figure out how to do it. Do I have to do a transorm or is there an easier way?
https://github.com/karatelabs/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature
https://github.com/karatelabs/karate#json-transforms
Here you go:
* def payload = { before: 'foo' }
* remove payload.before
* payload.after = 'bar'
* match payload == { after: 'bar' }
Instead of remove this will also work (using pure JS):
* eval delete payload.before
EDIT: after seeing the comments, I would treat this as a JSON transform.
* def payload = { before: 'foo' }
* def fun = function(x){ var res = {}; res.after = x.before; return res }
* def result = fun(payload)
* match result == { after: 'foo' }
I'm sure now you'll want to "retain" all the existing data. Fine, here you go:
* def payload = { before: 'foo' }
* def fun = function(x){ var res = x; res.after = x.before; delete res.before; return res }
* def result = fun(payload)
* match result == { after: 'foo' }
And you already know that you can run a transform on all array elements like this:
* def result = karate.map(someArray, fun)
Please note that you can create 2 or 3 transforms - and "nest" them.

Trying to assert 2 dynamic responses

I have 2 different responses:
{
"id": "U204204",
"title": "Safety kit",
"categoryPath": "/equipment/accessories/null",
"keyFeature": false,
"description": "test",
"price": 24.5,
"availability": "optional-extra",
"technologyItems": [],
"bespoke": false
}
or/and
{
"id": "GWW1WW1",
"title": "Winter pack",
"categoryPath": "/comfort & convenience/packs/null",
"keyFeature": false,
"description": "test",
"price": 410,
"availability": "optional-extra",
"technologyItems": [],
"bespoke": false
}
Now what I'm trying to assert is as long as the key price in ANY of the 2 responses above has a value ending in '.5' pass it.
I have tried the following and similar things but not working:
Given path 'endpoint'
And multipart file pdbData = { read: 'json/PostRequest_201_3.json', filename: 'PostRequest_201_3.json', contentType: 'application/json'}
When method post
And status 201
* def NewpdbId = response.id
And path 'endpoint'+NewpdbId+'/V3FOY3DG'
And method GET
And status 200
* string aa = response.options[13].price
* string bb = response.options[16].price
* def expected = aa contains "#redgex .+[.5]+" ? { pass: true }
* def expected = bb contains "#regex .+[.5]+" ? { pass: true }
* string expected = expected
And match expected == { pass: true }
So if the key price ends in '.5' in any of the responses it should be a pass. If key price is a whole number in all of the responses then it should fail.
Any ideas? I have tried so many different ways
There are a few ways. Read the docs for match each also:
First extract only the price values into a list, convert to strings (read the docs for JSON transforms):
* def prices = $response.options[*].price
* def fun = function(x){ return x + '' }
* def prices = karate.map(prices, fun)
* match prices contains '#regex .+[.5]+'

Un-named JSON array field validation in Karate

I have a un-named JSON array like this from the response and would like to check whether it contains "confirmationNumber": "pqrs" or not. May I know how can I check that in Karate?
[
{
"id": 145,
"confirmationNumber": "abcd"
},{
"id": 723
"confirmationNumber": "pqrs"
}
,{
"id": 7342
"confirmationNumber": "sfeq"
}
]
karate.filter() is good for these situations:
* def response =
"""
[
{
"id":145,
"confirmationNumber":"abcd"
},
{
"id":723,
"confirmationNumber":"pqrs"
},
{
"id":7342,
"confirmationNumber":"sfeq"
}
]
"""
* def fun = function(x){ return x.confirmationNumber == 'pqrs' }
* def found = karate.filter(response, fun)
* match found == '#[1]'
Also see examples of JsonPath: https://github.com/intuit/karate#jsonpath-filters
EDIT: apologies, there is a much simpler way, please read the docs !
* match response contains { id: '#number', confirmationNumber: 'pqrs' }
* def item = { confirmationNumber: 'pqrs' }
* match response contains '#(^item)'

Karate: when I want to set value to $..somewhereInJsonPath I get Path must not end with a '

I want to update a value of somewhereInJsonPath field in my JSON file.
I am using for this: * set myBody $..someWhereInJsonPath = 'AAA'. And when I run test I get: Path must not end with a '.'
But when I am using * set myBody $..firstHere.someWhereInJsonPath = 'AAA' it is working.
I think in first case, where we want to update first value in $.., it must working too.
To clarify:
For example we have JSON:
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"something": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
And when I do: $.store.book[0].something = 13 it is working.
BUT when I do: $..something = 13 it is not working.
Why? And how can I update this?
At http://jsonpath.com/ when I want to find $..something it is find this value. But when I use $..something in karate it is not working.
Realted with https://stackoverflow.com/questions/54928387/karate-jsonpath-wildcards-didnt-work-or-partly-didnt-work
Actually the set command is not really designed for JsonPath wildcards. For example $[*].foo or $..foo are examples of wildcards. And $[0].foo, $.foo or response.foo are pure JS expressions.
So please stick to pure JS expressions like this. Here below, set is interchangeable with eval which is more useful in case you want to use dynamic / variables for e.g. * eval response[foo] where foo is a string.
* def response = { foo: { somePath: 'abc' } }
* eval response.foo.somePath = 'xyz'
* match response == { foo: { somePath: 'xyz' } }
If you really do need to do "bulk" updates, use a transform function:
* def response = [{}, {}]
* def fun = function(x, i){ return { foo: 'bar', index: ~~(i + 1) } }
* def res = karate.map(response, fun)
* match res == [{ foo: 'bar', index: 1 }, { foo: 'bar', index: 2 }]

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