i have set continueOnStepFailure to true before match step, i am using karate core . i am still unbale to get desired result of each attribute validation of json. After the first failure next values are not getting compared. Do we need to take care of any additional settings apart of karate version 1.0 and above?
* def response1 =
"""
{
"name": "Tom",
"gender": "male",
"age": "20"
}
"""
* def response2 =
"""
{
"name": "Jon",
"gender": "female",
"age": "21"
}
"""
* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
* match response1 == response2
* configure continueOnStepFailure = false
Output of above code is ---------->
match failed: EQUALS
$ | not equal | match failed for name: 'name' (MAP:MAP)
{"name":"Tom","gender":"male","age":"20"}
{"name":"Jon","gender":"female","age":"21"}
$.name | not equal (STRING:STRING)
'Tom'
'Jon'
classpath:LIB_API/SampleTest.feature:51
Related
I have the following case and need to use firstKeyStr as a key, my code now is seeing "#firstKeyStr" as the key not the one stored in it
Code
Given path '/api/v1/sites'
When method get
Then status 200
And match response.success == true
And match response.data == "#object"
* def keys = karate.keysOf(response.data)
* def firstKeyStr = keys[0]
And match response.data."#firstKeyStr" == "#object"
Json Response Body
{
"success": true,
"data": {
"5ef34d0ca5a3c56ae14d2a23": {
"devices": [
"5f03192010a47f3e5b714193"
],
"groups": [
"5f0d9f30ef89e22778a2d230"
],
"users": [],
"triggers": [],
"alerts": [
"5f0d92b967bac60b84d3989b"
],
"name": "test",
"country": "US",
]
}
}
I'm looking for a way to pass this dynamic key (5ef34d0ca5a3c56ae14d2a23) in this line (And match response.data."#firstKeyStr" == "#object")
Since you are new to stack overflow, please read the following to best get help:
https://stackoverflow.com/help/how-to-ask
https://stackoverflow.com/help/someone-answers
As Peter said, your JSON is not well formed and the order of keys is not guaranteed (unless you have just a single key). The following code should get you going.
Sample Code:
Feature: firstKey
Scenario: firstKey
* def resp =
"""
{
"success": true,
"data": {
"5ef34d0ca5a3c56ae14d2a23": {
"devices": [
"5f03192010a47f3e5b714193"
],
"groups": [
"5f0d9f30ef89e22778a2d230"
],
"users": [],
"triggers": [],
"alerts": [
"5f0d92b967bac60b84d3989b"
],
"name": "test",
"country": "US"
}
}
}
"""
And match resp.data == "#object"
* def keys = karate.keysOf(resp.data)
* def firstKeyStr = keys[0]
* print firstKeyStr
* print (resp.data[firstKeyStr])
And match (resp.data[firstKeyStr]) == "#object"
Use round brackets so that JS is used instead of JsonPath:
* def response = { data: { a: 1, b: 2 } }
* def keys = karate.keysOf(response.data)
* def first = keys[0]
* match (response.data[first]) == 1
I am hitting an API end point and getting something like below.
{
"emp": {
"id": "123",
"firstNm": "test",
"lastNm": "last",
"dob": "200-01-01",
"gender": {
"code": "F",
"name": "Female",
"description": "Female"
},
"test1": [
{
"tes2": "F50045A3B994FB2BDF4E3D3FC906F592",
"t2": "a23",
"test3": {
"code": "432",
},
"ind": [
"ABC",
"BCD",
]
}
]
}
}
I want to match the elements in the array
"ind": [
"ABC",
"BCD",
]
I have tried the below:
Feature: test
Background:
* url BaseUrl
* configure headers = read('classpath:headers.js')
Scenario Outline: test
Given path '/path'
And param id = <id>
When method get
Then status 200
* def json = response
* def has = {ind:['#string'] }
* def indicator = { ind: '#[] has' }
* match json.member == '#[] indicator'
Examples:
| id |
| '1234' |
But observed the below exception:
expected: '#[] ind', reason: not an array or list
Can someone please help?
I am not really following your logic since indicators is not in the json response or defined variable but to get to the ind array the path is emp.test1[0].ind
if you want to match that the array has ABC and BCD you would do the following
* match response.emp.test1[0].ind == ['ABC', 'BCD']
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]+'
I am trying to filter my API response using JSON Path filter using Karate framework to extract a specific value that meets one of the condition using a value from a variable but I am not able to map variable properly, so my filter not working properly. I looked at the documentation and tried multiple ways but couldn't resolve this issue.
Sample response JSON:
"slices": [
{
"id": 7591164138534052,
"duration": {
"value": 1675,
"unit": "MINUTE"
},
"segments": [
{
"id": 1,
"segmentRefId": 23783268357325705
},
{
"id": 2,
"segmentRefId": 7591164138531002
},
{
"id": 3,
"segmentRefId": 7591164138532394
}
]
}
],
"segments": [
{
"id": 23783268357325705,
"departDateTime": "2019-10-05T19:50:00",
"arrivalDateTime": "2019-10-06T14:25:00",
"originAirport": "LAX",
"destinationAirport": "LHR",
"duration": {
"value": 635,
"unit": "MINUTE"
},
"marketingAirline": "BA",
"operatingAirline": "AA",
"flightNumber": "1509",
"equipmentCode": "77W",
"subjectToGovtApproval": false,
"numOfStops": 0,
"stops": []
}
]
}```
I am using the below script where I am using variable 'originRefId':
* def originRefId = response.slices[0].segments[0].segmentRefId
* def origin = karate.jsonPath(response, "$.segments[?(#.id=='originRefId')]")
* print 'the value of Origin is:', origin
Expected results LAX but I am getting an empty array.
17:42:15.119 [main] INFO com.intuit.karate - [print] the value of Origin is: [
]
Here is your solution:
* def originRefId = response.slices[0].segments[0].segmentRefId
* def segment = karate.jsonPath(response, "$.segments[?(#.id==" + originRefId + ")]")[0]
* def origin = segment.originAirport
* match origin == 'LAX'
I am trying to assert that a key is not in the JSON response. This is the response
{
"_type": "UserAccount",
"accountName": "Account_Name",
"accountType": "regular",
"whiteList": true,
"sfAccountId": "1",
"preferredLanguage": "english",
"imageSize": "highRes",
"_id": "775a8451-6a07-42da-a199-fe98f44bdc06"
}
I have tried both of these steps according to the documentation karate#null-and-notpresent and this answer from Peter Thomas
And match response.appClientId == '#notpresent'
And match response == {appClientId:'#notpresent'}
However this is the error I keep getting for the first step above
com.jayway.jsonpath.PathNotFoundException: No results for path: $['appClientId']
and for the second step
com.intuit.karate.KarateException: path: $, actual
Thanks in advance
Maybe your version is out of date, the below works for me in 0.9.1:
* def response =
"""
{
"_type": "UserAccount",
"accountName": "Account_Name",
"accountType": "regular",
"whiteList": true,
"sfAccountId": "1",
"preferredLanguage": "english",
"imageSize": "highRes",
"_id": "775a8451-6a07-42da-a199-fe98f44bdc06"
}
"""
* match response.appClientId == '#notpresent'
* match response contains { appClientId: '#notpresent' }