1

I have 2 responses :

response1 = {a: 1, b: "1"}
response2 = {a: 2, b: "2"}

I want to compare number/integer values like:

Then assert (response1.a + 1) == response2.a

Hope this is possible. But how ? I need solution to both type of data types, in case the parameter value is String or Number/Integer.

I tried 2 approaches, with no success:

  1. Then assert response1.a < response2.a
  2. Then assert response1.a+1 == response2.a

Actual Code:

   Scenario: Validate openCase count
        * def preResponse = call read('@CaseCount')
        * def createCaseResponse = karate.callSingle('CreateCase.feature@Create');
        * def postResponse = call read('@CaseCount')
        Then assert preResponse.responseData.openCases < postResponse.responseData.openCases

Actual Response:

{
        "responseData": {
            "openCases": 1,
            "closedCases": 0,
            "inProgressCases": 0,
            "onHoldCases": 0,
            "invalidCases": 0,
            "overDueCases": 0,
            "dueTodayCases": 0
        },
        "resultInfo": {
            "resultCodeId": "200",
            "resultCode": "SUCCESS",
            "resultStatus": "SUCCESS",
            "resultMsg": "S"
        },
        "message": "SUCCESS"
    }
R Singh
  • 11
  • 3

1 Answers1

0

There are many ways, here's one:

* match response2 == { a: '#(response1.a + 1)', b: "#((response1.a + 1) + '')" }

It might be much easier to create a temp variable:

* def a1 = response1.a
* match response2.a  == a1 + 1

For type conversions, refer: https://github.com/karatelabs/karate#type-conversion

Personally - I highly discourage such "clever" tests. Good tests should know exactly what is expected and you just check one "hard coded" JSON. More explanation here: https://stackoverflow.com/a/54126724/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248