0

I want to match JSON values with Response values in Karate. How to retrieve all field values from JSON and Response and match them? Any solution is much appreciated Below is sample JSON and Response

 JSON: {
    "Field1": 123,
    " Field2": 456,
    " Field3": "O",
    " Field4": 1000
  },
  {    "Field1": 678,
    " Field2": 234,
    " Field3": "P",
    " Field4": 2000
  }
]

Response:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Header/>
   <S:Body>
      <ns12: Response>
         <ns12: ResponseList>
            <ns7: Field1>123</ns7: Field1>
            <ns7: Field2>456</ns7: Field2>
            <ns7: Field3>O</ns7: Field3>
            <ns7: Field4>1000</ns7: Field4>

         </ns12: ResponseList >
         <ns12: ResponseList >
            <ns7: Field1>678</ns7: Field1>
            <ns7: Field2>234</ns7: Field2>
            <ns7: Field3>P</ns7: Field3>
            <ns7: Field4>2000</ns7: Field4>
         </ns12: ResponseList >
      </ns12: Response >
   </S:Body>
</S:Envelope>
  • 3
    Possible duplicate of [How to compare XML response with Json in Karate](https://stackoverflow.com/questions/53471153/how-to-compare-xml-response-with-json-in-karate) – Peter Thomas Sep 05 '19 at 21:54
  • Thank you very much Peter for proving useful links. That really helped a lot. However I still have some issue after implementing your solution. 1. I am getting an error, actual value are not present. Are Keys are case sensitive in Karate? 2. How do I match different Date and Amount format as per example below? Any help is appreciated. – karate_begineer Sep 06 '19 at 15:32
  • JSON[ { "SIN": 12345678, "SOMEID": "12345", "DATE": 2019-09-06 00:00:00.0, "SOMETATUS": "T", "AMOUNT": 100, "SOMEMETHO": "M", "OTHERNO": 50001 }, { "SIN": 12345678, "SOMEID": "56789", "DATE": 2019-09-06 00:00:00.0, "SOMETATUS": "Q", "AMOUNT": 200.12, "SOMEMETHO": "R", "OTHERNO": 40001 } – karate_begineer Sep 06 '19 at 15:32
  • no, please read the docs and ask specific questions – Peter Thomas Sep 06 '19 at 15:33

1 Answers1

0

A rough description on how it works can be found on https://intuit.github.io/karate/.

I would like to show you an example from https://www.baeldung.com/karate-rest-api-testing

4.2. Testing the Response Let's a write another scenario that tests that the REST endpoint returns a specific response:

Scenario: Testing the exact response of a GET endpoint
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ == {id:"1234",name:"John Smith"}
The match operation is used for the validation where ‘$' represents the response. So the above scenario checks that the response exactly matches ‘{id:”1234″,name:”John Smith”}'.

We can also check specifically for the value of the id field:

And match $.id == "1234"
The match operation can also be used to check if the response contains certain fields. This is helpful when only certain fields need to be checked or when not all response fields are known:

Scenario: Testing that GET response contains specific field
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ contains {id:"1234"}

And another example from https://aboullaite.me/karate-framework-rest-testing/

Let’s look at a final scenario that tests a POST endpoint and takes a request body:

  Scenario: Create and retrieve a Product
    Given path 'products'
    And request { "name": "My product", "type": "Super Type", "price": 123, "shipping": 0, "upc": "041345324016", "description": "My super nice aweome product", "manufacturer": "Feo Hero", "model": "QB2400B4Z", "url": "some.url", "image": "some.image" }
    When method POST
    Then status 201
    And def product = response

    Given path '/products/'+product.id
    When method GET
    Then status 200
    And match $ contains {id:'#(product.id)',name:'#(product.name)',type:'#(product.type)',price:#(product.price)}

Soap is a bit different, example from https://intuit.github.io/karate/#request below:

Given request read('soap-request.xml')
When soap action 'QueryUsageBalance'
Then status 200
And match response /Envelope/Body/QueryUsageBalanceResponse/Result/Error/Code == 'DAT_USAGE_1003'
And match response /Envelope/Body/QueryUsageBalanceResponse == read('expected-response.xml')
Christian
  • 4,902
  • 4
  • 24
  • 42