1

I have a test-case like this in which I'm verifying the total number of rows - I have referred the below page and tried this solution, but seems that it is not working. I want to check whether my noOfRows is greater than 2.

This is the response I'm getting for printing

Total number of rows [ { "1": 27 } ]

    * def noOfRows = result
    * print 'Total number of rows ',result
    * assert noOfRows == { "1": '#? _ > 2' }
patel
  • 99
  • 2
  • 6

1 Answers1

0

If result is an array, you can do this:

* def result = [1, 2, 3]
* assert result.length > 2

What the readRows returns has nothing to do with Karate, so please work with someone who knows Java well if required: https://stackoverflow.com/a/52078427/143475

Also it looks like you are on an old version of Karate, so upgrade. And if still stuck, follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks for the answer, I want to check whether my value is more than 2, as you can see it is 27 right now.. Correcting my question for better clarity. – patel Sep 28 '21 at 07:29
  • @SapanaPatel I will not be able to help you with what your original object is, and it looks wrong to me. I have given you an answer for arrays. the rest is up to you – Peter Thomas Sep 28 '21 at 07:30
  • @SapanaPatel Your question is different and what you are looking for is different. Your response shows noOfRows which is 27. Now you want to assert on this value, that it should be greater than 2. Let me know if my understanding is correct. I – QualityMatters Sep 29 '21 at 07:35
  • Yes, that's correct, wanna check whether 27 is greater than 2. – patel Sep 30 '21 at 09:44
  • 1
    @SapanaPatel that's very easy, just do `* assert noOfRows['1'] > 2` - but for the last time, let me say that the result of that db.query looks completely messed up to me. please talk to some java developers on your team if possible. – Peter Thomas Sep 30 '21 at 10:16
  • 1
    @SapanaPatel I can understand that you are learning. So follow this approach: Bring your response to "https://jsonpath.com/". Now try to reach that field. (This is simple JSON Parsing). In your case, it is : $[*].1 -> $ represents the entire response, [] represents the array, [*]-> represents the complete array, .1 -> Is your first element. Just consider it as key value. Once you get that value, just put a assertions, that it should be greater than or whatever to some value. – QualityMatters Oct 01 '21 at 05:20
  • 1
    @SapanaPatel And you can change the question title as well. It should be how to put assertion on a response value present in JSON? (This is not specific to Karate)? – QualityMatters Oct 01 '21 at 05:31